summaryrefslogtreecommitdiffstats
path: root/scripts/lib/ptxd_make_xpkg_pkg.sh
blob: f094cb13048a814c659f41abf959cddc145d0e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
#!/bin/bash
#
# Copyright (C) 2010 by Michael Olbrich <m.olbrich@pengutronix.de>
#
# For further information about the PTXdist project and license conditions
# see the README file.
#

ptxd_exist() {
    for file in "${@}"; do
	if [ ! -e "${file}" ]; then
	    echo -e "\nError: file not found: ${file}\n"
	    return 1
	fi
    done
}
export -f ptxd_exist

ptxd_install_error() {
    echo Error: "$@"
    exit 1
}
export -f ptxd_install_error

#
# ptxd_install_getent_id
#
# convert usr or grp into numeric value
#
# $1: "usr", "grp"
#
ptxd_install_getent_id() {
    local key="${1}"
    local db id

    case "${key}" in
	usr)
	    id="user id"
	    db="/etc/passwd"
	    ;;
	grp)
	    id="group id"
	    db="/etc/group"
	    ;;
    esac

    eval ${key}_name='\(${!key}\)'

    if ! ptxd_get_alternative "projectroot" "${db}"; then
	ptxd_bailout "

  Unable to find '${db}'.

"
    fi

    local line
    if ! line="$(grep -e "^${!key}" "${ptxd_reply}")"; then
	ptxd_bailout "

${id} '${!key}' not found in '$(ptxd_print_path ${ptxd_reply})' for:
  '${dst:-${dir}}'

"
    fi
    local orig_IFS="${IFS}"
    local IFS=":"
    set -- $line
    IFS="${orig_IFS}"
    eval ${key}="${3}"
}
export -f ptxd_install_getent_id

#
# ptxd_install_resolve_usr_grp
#
# convert usr/grp that contain names into numeric values
#
ptxd_install_resolve_usr_grp() {
    if [ -n "$usr" ] && ! [ 0 -le "$usr" ] 2>/dev/null; then
	ptxd_install_getent_id usr || return
    else
	unset usr_name
    fi
    if [ -n "$grp" ] && ! [ 0 -le "$grp" ] 2>/dev/null; then
	ptxd_install_getent_id grp || return
    else
	unset grp_name
    fi
}
export -f ptxd_install_resolve_usr_grp

ptxd_install_setup_global() {
    local -a nfsroot_dirs

    nfsroot_dirs=("${ptx_nfsroot}" ${pkg_nfsroot_dirs})

    # all dirs
    dirs=("${nfsroot_dirs[@]}" "${pkg_xpkg_tmp}")

    # nfs root dirs
    # no setuid/setguid bit here
    ndirs=("${nfsroot_dirs[@]}")

    # package dirs
    # this goes into the ipkg, thus full file modes here
    pdirs=("${pkg_xpkg_tmp}")

    # dirs with separate debug files
    ddirs=()
    if [ "$(ptxd_get_ptxconf PTXCONF_TARGET_DEBUG_OFF)" != "y" ]; then
	ddirs=("${nfsroot_dirs[@]}")
	if [ "$(ptxd_get_ptxconf PTXCONF_DEBUG_PACKAGES)" = "y" ]; then
	    ddirs[${#ddirs[@]}]="${pkg_xpkg_dbg_tmp}"
	fi
    fi
}
export -f ptxd_install_setup_global

ptxd_install_lock() {
    local lockfile
    mkdir -p "${PTXDIST_TEMPDIR}/locks"

    if [ -n "${dst}" ]; then
	lockfile="${PTXDIST_TEMPDIR}/locks/${dst//\//-}"
    else
	lockfile="${PTXDIST_TEMPDIR}/locks/other"
    fi
    if [ -n "${dst_lock}" ]; then
	ptxd_bailout "dst_lock must not be set when ptxd_install_lock() is called"
    fi
    exec {dst_lock}>"${lockfile}" &&
    flock "${dst_lock}"
}
export -f ptxd_install_lock

ptxd_install_unlock() {
    local ret=$?

    if [ -z "${dst_lock}" ]; then
	ptxd_bailout "dst_lock must be set when ptxd_install_unlock() is called"
    fi
    exec {dst_lock}>&-
    unset dst_lock
    # propagate the last error after unlocking
    return "${ret}"
}
export -f ptxd_install_unlock

ptxd_install_setup() {
    case "${dst}" in
	/bin/*|/sbin/*|/lib/*) dst="/usr${dst}" ;;
	/*|"") ;;
	*) ptxd_bailout "${FUNCNAME[${#FUNCNAME[@]}-5]}: 'dst' must be an absolute path!" ;;
    esac

    mod_nfs="$(printf "0%o" $(( 0${mod} & ~06000 )))" &&
    mod_rw="$(printf "0%o" $(( 0${mod} | 0200 )))" &&

    #
    # mangle user/group
    #
    ptxd_install_resolve_usr_grp &&

    #
    # lock destination to avoid parallel building issues
    #
    ptxd_install_lock
}
export -f ptxd_install_setup

ptxd_install_setup_src_list() {
    if [ "${cmd}" = "alternative" -o "${cmd}" = "config" ]; then
	#
	# if pkg_dir is empty we'll have some some empty entries in
	# the array, but that's no problem for the "-e" below.
	#
	local -a ptxd_reply
	ptxd_get_alternative_list projectroot "${1}"
	list=( \
	    "${ptxd_reply[@]}" \
	    "${pkg_pkg_dir:+${pkg_pkg_dir}${1}}" \
	    "${pkg_dir:+${pkg_dir}${1}}" \
	    )
    else
	list=( \
	    "${1}${PTXDIST_PLATFORMSUFFIX}" \
	    "${1}" \
	    )
    fi
}
export -f ptxd_install_setup_src_list

ptxd_install_setup_src() {
    local -a list
    local legacy_src

    if [ "${src}" = "-" -a -n "${dst}" ]; then
	src="${pkg_pkg_dir}${dst}"
	gdb_src="${pkg_pkg_dir}/usr/share/gdb/auto-load${dst}-gdb"
    elif [ "${src#${pkg_pkg_dir}}" != "${src}" ]; then
	gdb_src="${pkg_pkg_dir}/usr/share/gdb/auto-load${src#${pkg_pkg_dir}}-gdb"
    fi

    ptxd_install_setup || return

    legacy_src="${src#/usr}"
    if [ \( "${cmd}" = "alternative" -o "${cmd}" = "config" \) -a "${legacy_src}" != "${src}" ]; then
	pkg_dir= ptxd_install_setup_src_list "${legacy_src}"
	if ptxd_get_path "${list[@]}"; then
	    local tmp
	    echo -e "\nFound file for '${dst}' in these legacy locations:\n"
	    for tmp in "${ptxd_reply[@]}"; do
		echo "$(ptxd_print_path "${tmp}")"
	    done
	    echo
	    ptxd_bailout "They must be moved to the corresponding locations in '/usr'"
	fi
    fi

    ptxd_install_setup_src_list "${src}"

    # Since the dependency to the source files is dynamic we store
    # the dependency information in a dependency file that can be
    # included in the make files itself.
    # We depend on the first available file, which is the one that will
    # be used. If one with a higher priority is created, the dependency
    # will cause the package to be recreated.
    # Make cannot handle filenames that contain a ':'. So don't generate
    # dependecies for those files.
    local deprule=""
    if ! [[ "${src}" =~ : ]]; then
	for src in "${list[@]}"; do
	    # don't provide dependencies for files in PTXDIST_PLATFORMDIR.
	    if [ "${src}" == "${src#${PTXDIST_PLATFORMDIR}}" -a -n "${src}" ]; then
		    deprule="${deprule} ${src}"
	    fi
	done
    fi
    if [ -n "${deprule}" ]; then
	deprule="${ptx_state_dir}/${pkg_stamp}: \$(firstword \$(wildcard ${deprule}))"
	# Make the deps rule robust for varying installation paths, and
	# make the deps rules file more readable.
	deprule=${deprule//${PTXDIST_TOPDIR}/\$(PTXDIST_TOPDIR)}
	deprule=${deprule//${PTXDIST_WORKSPACE}/\$(PTXDIST_WORKSPACE)}
	echo "${deprule}" >> ${pkg_xpkg_install_deps}
    fi
    if ptxd_get_path "${list[@]}"; then
	src="${ptxd_reply}"
	return
    fi

    echo -e "\nNo suitable file '${dst}' could be found in any of these locations:"
    local orig_IFS="${IFS}"
    local IFS="
"
    echo -e "${list[*]}\n"
    IFS="${orig_IFS}"
    return 1
}
export -f ptxd_install_setup_src

ptxd_install_virtfs() {
    local mod_virtfs="$(( 0${mod} | ${mod_type} ))"
    local d dir file

    if [ "${PTXCONF_SETUP_NFS_VIRTFS}" != "y" ]; then
	return
    fi

    for d in "${ndirs[@]/%/${dst}}"; do
	dir="${d%/*}/.virtfs_metadata"
	file="${dir}/${d##*/}"&&
	mkdir -p "${dir}" &&
	cat <<- EOF > "${file}"
	virtfs.uid=${usr}
	virtfs.gid=${grp}
	virtfs.mode=${mod_virtfs}
	EOF
	if [ -n "${major}" -a -n "${minor}" ]; then
	    local rdev=$[ ${major} << 8 | ${minor} ] &&
	    echo "virtfs.rdev=${rdev}" >> "${file}"
	fi || break
    done
}
export -f ptxd_install_virtfs

ptxd_install_dir_impl() {
    local mod_type=0040000

    if [ "${dst}" != "" ]; then
	ptxd_ensure_dir "${dst%/*}"
    fi &&

    if [ "${1}" != "keep" ]; then
	install -m "${mod_nfs}" -d "${ndirs[@]/%/${dst}}" &&
	install -m "${mod}" -o "${usr}" -g "${grp}" -d "${pdirs[@]/%/${dst}}"
    else
	# don't overwrite existing permissions
	mkdir -p "${dirs[@]/%/${dst}}"
    fi &&

    ptxd_install_virtfs
}
export -f ptxd_install_dir_impl

ptxd_ensure_dir() {
    local dst="$1"
    local usr="0"
    local grp="0"
    local mod="0755"
    local mod_nfs="0755"
    local mod_rw="0755"
    local dir no_skip dst_lock

    for dir in "${ndirs[@]/%/${dst}}"; do
	if [ ! -d "${dir}" -o ! -e "${dir%/*}/.virtfs_metadata/${dir##*/}" ]; then
	    no_skip=1
	    break
	fi
    done
    if [ "${no_skip}" != 1 ]; then
	# just create the rest and continue if virtfs data already exists
	#  but don't overwrite existing permissions
	mkdir -p "${dirs[@]/%/${dst}}" &&
	return
    fi &&
    ptxd_install_lock &&
    ptxd_install_dir_impl keep
    ptxd_install_unlock
}
export -f ptxd_ensure_dir

ptxd_install_dir() {
    local sep="$(echo -e "\x1F")"
    local dst="${1%/}"
    local usr="$2"
    local grp="$3"
    local mod="$4"
    local mod_nfs mod_rw dst_lock

    ptxd_install_setup &&
    echo "\
install directory:
  dir=${dst}
  owner=${usr} ${usr_name}
  group=${grp} ${grp_name}
  permissions=${mod}
" &&

    ptxd_install_dir_impl &&
    echo "d${sep}${dst}${sep}${usr}${sep}${grp}${sep}${mod}" >> "${pkg_xpkg_perms}" ||
    ptxd_install_error "install_dir failed!"
    ptxd_install_unlock
}
export -f ptxd_install_dir

ptxd_extract_build_id() {
    # accept only the first build-id
    ${CROSS_READELF} -n ${src} | awk 'BEGIN { FS=":\\s*" } /Build ID/{ print $2; exit }'
}
export -f ptxd_extract_build_id

export ptxd_install_file_objcopy_args="--only-keep-debug --compress-debug-sections"

ptxd_install_file_extract_debug() {
    local dst="${1}"
    local tmp="$(mktemp -u "${PTXDIST_TEMPDIR}/debug.XXXXX")"
    local dbg dir
    local bid=$(ptxd_extract_build_id)

    if [ "${#ddirs[*]}" -eq 0 ]; then
	return
    fi

    if [ -z "${bid}" ]; then
	dbg="${dst%/*}/.debug/.${dst##*/}.dbg"
    else
	local path_component=${bid::-38}
	local name_component=${bid:2:38}
	dbg="/usr/lib/debug/.build-id/${path_component}/${name_component}.debug"
    fi

    # this can fail if objcopy does not support compressing debug sections or
    # is compiled without zlib support
    "${CROSS_OBJCOPY}" ${ptxd_install_file_objcopy_args} "${src}" "${tmp}" |&
	grep -q "\(unrecognized option\|unable to initialize com*press status\)"
    local -a status=( "${PIPESTATUS[@]}" )
    if [ ${status[0]} -ne 0 ]; then
	if [ ${status[1]} -eq 0 ]; then
	    ptxd_install_file_objcopy_args="--only-keep-debug"
	    "${CROSS_OBJCOPY}" ${ptxd_install_file_objcopy_args} "${src}" "${tmp}"
	else
	    # do it again to see the error message
	    "${CROSS_OBJCOPY}" ${ptxd_install_file_objcopy_args} "${src}" "${tmp}"
	fi
    fi &&
    echo "  debug file: ${dbg}" &&
    for dir in "${ddirs[@]}"; do
	if [ -n "${bid}" -o -e "${dir}${dst}" ]; then
	    install -D -m 644 "${tmp}" "${dir}/${dbg}"
	fi &&
	if [ -z "${bid}" -a -e "${dir}${dst}" ]; then
	    "${CROSS_OBJCOPY}" --add-gnu-debuglink "${dir}/${dbg}" "${dir}${dst}"
	fi
    done &&
    rm "${tmp}"
}
export -f ptxd_install_file_extract_debug

#
# $1: file to strip
#
# $strip: k for kernel modules
#         y for normal executables and libraries
#
#
ptxd_install_file_strip() {
    local -a strip_cmd files
    local dst="${1}"
    local i target_mini_debuginfo

    case "${strip:-y}" in
	k) strip_cmd=( "${CROSS_STRIP}" --strip-debug -R .GCC.command.line ) ;;
	y)
	    strip_cmd=( "${CROSS_STRIP}" -R .note -R .comment -R .GCC.command.line )
	    target_mini_debuginfo="$(ptxd_get_ptxconf PTXCONF_TARGET_MINI_DEBUGINFO)"
	;;
    esac

    files=( "${dirs[@]/%/${dst}}" )
    install -d "${files[0]%/*}" &&
    if [ "${target_mini_debuginfo}" = "y" ]; then
	local keep_symbols="$(mktemp -u "${PTXDIST_TEMPDIR}/keep_symbols.XXXXX")"
	local debug="$(mktemp -u "${PTXDIST_TEMPDIR}/debug.XXXXX")"
	local mini_debug="$(mktemp -u "${PTXDIST_TEMPDIR}/mini_debug.XXXXX")"

	comm -13 \
	    <("${CROSS_NM}" -D "${src}" --format=posix --defined-only \
		| awk '{ print $1 }' | sort) \
	    <("${CROSS_NM}" "${src}" --format=posix --defined-only \
		| awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' | sort) \
	    > "${keep_symbols}" &&
	# only create the section if there are symbols to add
	if [ -s "${keep_symbols}" ]; then
	    "${CROSS_OBJCOPY}" --only-keep-debug "${src}" "${debug}" &&
	    "${CROSS_OBJCOPY}" -S --remove-section .gdb_index --remove-section .comment \
		--keep-symbols="${keep_symbols}" "${debug}" "${mini_debug}" &&
	    rm "${debug}" &&
	    xz "${mini_debug}"
	fi &&
	rm "${keep_symbols}"
    fi &&
    "${strip_cmd[@]}" -o "${files[0]}" "${src}" &&
    if [ "${target_mini_debuginfo}" = "y" -a -e "${mini_debug}.xz" ]; then
	"${CROSS_OBJCOPY}" --add-section .gnu_debugdata="${mini_debug}.xz" "${files[0]}" &&
	rm "${mini_debug}.xz"
    fi &&
    for (( i=1 ; ${i} < ${#files[@]} ; i=$[i+1] )); do
	install -m "${mod_rw}" -D "${files[0]}" "${files[${i}]}" || return
    done &&

    if [ "${strip}" != "k" ]; then
	ptxd_install_file_extract_debug "${dst}" || return
    fi
}
export -f ptxd_install_file_strip


ptxd_install_file_impl() {
    local sep="$(echo -e "\x1F")"
    local src="$1"
    local dst="$2"
    local usr="$3"
    local grp="$4"
    local mod="$5"
    local strip="$6"
    local mod_type=0100000
    local mod_nfs mod_rw dst_lock
    local gdb_src

    ptxd_install_setup_src &&
    echo "\
install ${cmd}:
  src=$(ptxd_print_path "${src}")
  dst=${dst}
  owner=${usr} ${usr_name}
  group=${grp} ${grp_name}
  permissions=${mod}" &&

    ptxd_exist "${src}" &&
    rm -f "${dirs[@]/%/${dst}}" &&

    # check if src is a link
    if [ -L "${src}" ]; then
	local old="${src}"
	src="$(readlink -f "${src}")" &&
	if [ "${src}" = /dev/null ]; then
	    echo "'${old}' is a link to '/dev/null', skipping file."
	    echo
	    return
	fi &&
	ptxd_pedantic "file '$(ptxd_print_path "${old}")' is a link" &&
	echo "using '$(ptxd_print_path "${src}")' instead"
    fi &&

    if [ -z "${strip}" ]; then
	if ! "${CROSS_READELF}" -h "${src}" &> /dev/null; then
	    strip="n"
	else
	    strip="y"
	fi
    fi &&

    ptxd_ensure_dir "${dst%/*}" &&

    case "${strip}" in
	0|n|no|N|NO)
	    for d in "${dirs[@]/%/${dst}}"; do
		install -m "${mod_rw}" -D "${src}" "${d}" || return
	    done
	    ;;
	y|k)
	    ptxd_install_file_strip "${dst}"
	    ;;
	*)
	    if [ "${strip:0:1}" = "/" ] && \
		[ "${cmd}" = "alternative" -o "${cmd}" = "config" ]; then
		ptxd_bailout "
the 6th parameter of 'install_${cmd}' is strip, not the destination.
Usually, just remove the 6th parameter and everything works fine.
"
	    fi
	    ptxd_bailout "${FUNCNAME}: invalid value for strip ('${strip}')"
	    ;;
    esac &&

    if [ "${#ddirs[*]}" -gt 0 -a -n "${gdb_src}" ]; then
	local gdb_file
	local ddir="${dst%/*}"
	for gdb_file in $(ls "${gdb_src}".* 2>/dev/null); do
	    local gdb_dst="${ddir}/${gdb_file##*/}"
	    echo "  debug file: ${gdb_dst}" &&
	    for d in "${ddirs[@]/%/${gdb_dst}}"; do
		install -m 644 -D "${gdb_file}" "${d}" || break
	    done
	done
    fi &&
    echo "" &&

    # now change to requested permissions
    chmod "${mod_nfs}" "${ndirs[@]/%/${dst}}" &&
    chmod "${mod}"     "${pdirs[@]/%/${dst}}" &&

    # now change to requested user and group
    chown "${usr}:${grp}" "${pdirs[@]/%/${dst}}" &&

    ptxd_install_virtfs &&

    echo "f${sep}${dst}${sep}${usr}${sep}${grp}${sep}${mod}" >> "${pkg_xpkg_perms}"
    ptxd_install_unlock
}
export -f ptxd_install_file_impl

ptxd_install_ln() {
    local src="$1"
    local dst="$2"
    local usr="${3:-0}"
    local grp="${4:-0}"
    local mod="0777"
    local mod_type=0120000
    local mod_nfs mod_rw rel dst_lock

    ptxd_install_setup &&
    echo "\
install link:
  src=${src}
  dst=${dst}
" &&

    case "${src}" in
	/*)
	    if [ "${PTXCONF_SETUP_NFS_REL_SYMLINK}" = "y" ]; then
		rel="$(dirname "${dst}" | sed -e 's,/[^/]*,/..,g' -e 's,^/,,')"
	    fi
	    ;;
	*)  ;;
    esac &&

    rm -f "${dirs[@]/%/${dst}}" &&
    ptxd_ensure_dir "${dst%/*}" &&
    for d in "${ndirs[@]/%/${dst}}"; do
	ln -s "${rel}${src}" "${d}" || return
    done &&
    for d in "${pdirs[@]/%/${dst}}"; do
	ln -s "${src}" "${d}" || return
    done &&

    ptxd_install_virtfs &&

    chown --no-dereference "${usr}:${grp}" "${dirs[@]/%/${dst}}"
    ptxd_install_unlock
}
export -f ptxd_install_ln

ptxd_install_mknod() {
    local sep="$(echo -e "\x1F")"
    local dst="$1"
    local usr="$2"
    local grp="$3"
    local mod="$4"
    local type="$5"
    local major="$6"
    local minor="$7"
    local mod_nfs mod_rw mod_type dst_lock

    case "${type}" in
	c) mod_type=0020000 ;;
	b) mod_type=0060000 ;;
	p) mod_type=0010000 ;;
    esac &&

    ptxd_install_setup &&
    echo "\
install device node:
  owner=${usr}
  group=${grp}
  permissions=${mod}
  type=${type}
  major=${major}
  minor=${minor}
  name=${dst}
" &&

    rm -f "${pdirs[@]/%/${dst}}" &&
    ptxd_ensure_dir "${dst%/*}" &&
    for d in "${pdirs[@]/%/${dst}}"; do
	mknod -m "${mod}" "${d}" "${type}" ${major} ${minor} || return
    done &&
    touch "${ndirs[@]/%/${dst}}" &&
    chown "${usr}:${grp}" "${pdirs[@]/%/${dst}}" &&

    ptxd_install_virtfs &&

    echo "n${sep}${dst}${sep}${usr}${sep}${grp}${sep}${mod}${sep}${type}${sep}${major}${sep}${minor}" >> "${pkg_xpkg_perms}"
    ptxd_install_unlock
}
export -f ptxd_install_mknod

ptxd_install_alternative() {
    local cmd="alternative"
    local src="${1}"
    local dst="${2}"
    shift 2
    ptxd_install_file_impl "${src}" "${dst:-${src}}" "${@}" ||
    ptxd_install_error "install_alternative failed!"
}
export -f ptxd_install_alternative

ptxd_install_config() {
    local cmd="config"
    local src="${1}"
    local dst="${2}"
    shift 2
    ptxd_install_file_impl "${src}" "${dst:-${src}}" "${@}" &&
    # this is a config file add it to conffiles so it's handled correctly
    echo "${dst:-${src}}" >> "${pkg_xpkg_conffiles}" &&
    chown 0:0 "${pkg_xpkg_conffiles}" ||
    ptxd_install_error "install_config failed!"
}
export -f ptxd_install_config

ptxd_install_file() {
    local cmd="file"
    ptxd_install_file_impl "$@" ||
    ptxd_install_error "install_file failed!"
}
export -f ptxd_install_file

ptxd_install_link() {
    ptxd_install_ln "$@" ||
    ptxd_install_error "install_link failed!"
}
export -f ptxd_install_link

ptxd_install_node() {
    ptxd_install_mknod "$@" ||
    ptxd_install_error "install_node failed!"
}
export -f ptxd_install_node

ptxd_install_replace() {
    local dst="$1"
    local placeholder="$2"
    local value="$3"
    local mod_nfs mod_rw dst_lock

    ptxd_install_setup &&
    echo "\
install replace:
  file=${dst}
  '${placeholder}' -> '${value}'
" &&

    ptxd_exist "${dirs[@]/%/${dst}}" &&
    sed -i -e "s,${placeholder//,/\\,},${value//,/\\,},g" "${dirs[@]/%/${dst}}" ||

    ptxd_install_error "install_replace failed!"
    ptxd_install_unlock
}
export -f ptxd_install_replace

ptxd_install_script_replace() {
    local dst="$1"
    local placeholder="$2"
    local value="$3"

    ptxd_install_setup &&
    echo "\
install script replace:
  script=${dst}
  '${placeholder}' -> '${value}'
" &&

    ptxd_exist "${pkg_xpkg_control_dir}/${dst}" &&
    sed -i -e "s,${placeholder},${value},g" "${pkg_xpkg_control_dir}/${dst}" ||

    ptxd_install_error "install_script_replace failed!"
    ptxd_install_unlock
}
export -f ptxd_install_script_replace

ptxd_install_replace_figlet() {
    local dst="$1"
    local placeholder="$2"
    local value="$3"
    local escapemode="$4"
    local mod_nfs mod_rw dst_lock

    ptxd_install_setup &&
    echo "\
install replace figlet:
  file=${dst}
  '${placeholder}' -> '\$(figlet ${value})'
" &&

    ptxd_exist "${dirs[@]/%/${dst}}" &&
    ptxd_figlet_helper() {
	local value="$1"
	local escapemode="$2"
	figlet -d "${PTXDIST_SYSROOT_HOST}/share/figlet" -- "${value}" | \
	case "$escapemode" in
	    # /etc/issue needs each backslash quoted by another backslash. As
	    # the string is interpreted by the shell once more below, another
	    # level of quoting is needed such that every \ in the output of
	    # figlet needs to be replaced by \\\\. As a \ in sed needs to be
	    # quoted, too, this results in eight backslashes in the replacement
	    # string.
	    etcissue)	sed 's,\\,\\\\\\\\,g';;
	    *)		;;
	esac | \
	awk '{ if ($0 !~ "^ *$") printf("%s\\n", $0) }'  # newlines for sed
    } &&
    figlet="$(ptxd_figlet_helper "$value" "$escapemode")" &&
    sed -i -e "s#${placeholder}#${figlet}#g" "${dirs[@]/%/${dst}}" ||

    ptxd_install_error "install_replace failed!"
    ptxd_install_unlock
}
export -f ptxd_install_replace_figlet

ptxd_install_generic() {
    local file="$1"
    local dst="$2"
    local usr="$3"
    local grp="$4"
    local strip="$5"

    local -a stat
    local orig_IFS="${IFS}"
    local IFS=":"
    stat=( $(stat -c "%u:%g:0%a:0x%t:0x%T:%F" "${file}") ) &&
    IFS="${orig_IFS}"
    local usr="${usr:-${stat[0]}}" &&
    local grp="${grp:-${stat[1]}}" &&
    local mod="$(printf "0%o" $(( 0${stat[2]} & ~${umask} )))" &&
    local major="${stat[3]}" &&
    local minor="${stat[4]}" &&
    local type="${stat[5]}" &&

    case "${type}" in
	"directory")
	    ptxd_install_dir "${dst}" "${usr}" "${grp}" "${mod}"
	    ;;
	"character special file")
	    ptxd_install_mknod "${dst}" "${usr}" "${grp}" "${mod}" c "${major}" "${minor}"
	    ;;
	"block special file")
	    ptxd_install_mknod "${dst}" "${usr}" "${grp}" "${mod}" b "${major}" "${minor}"
	    ;;
	"symbolic link")
	    local src="$(readlink "${file}")" &&
	    ptxd_install_ln "${src}" "${dst}" "${usr}" "${grp}"
	    ;;
	"regular file"|"regular empty file")
	    ptxd_install_file "${file}" "${dst}" "${usr}" "${grp}" "${mod}" "${strip}"
	    ;;
	*)
	    echo "Error: File type '${type}' unknown!"
	    return 1
	    ;;
    esac
}
export -f ptxd_install_generic

ptxd_install_find() {
    local src="${1%/}"
    local dst="${2%/}"
    dst=${dst:-/}
    local usr="${3#-}"
    local grp="${4#-}"
    local strip="${5}"
    local mod_nfs mod_rw dst_lock
    local gdb_src
    if [ -z "${glob}" ]; then
	local -a glob=( "-o" "-print" )
    fi

    ptxd_install_setup_src &&
    if [ -L "${src}" -a "$(readlink -f "${src}")" = /dev/null ]; then
	echo "'${src}' is a link to '/dev/null', skipping."
	echo
	return
    fi &&
    test -d "${src}"
    ptxd_install_unlock &&

    find "${src}" ! -path "${src}" -a \( \
		-path "*/.svn" -prune -o -path "*/.git" -prune -o \
		-path "*/.pc" -prune -o -path "*/CVS" -prune \
		"${glob[@]}" \) | while read file; do
	local dst_file="${dst}${file#${src}}"
	ptxd_install_generic "${file}" "${dst_file}" "${usr}" "${grp}" "${strip}" || return
    done
    check_pipe_status
}
export -f ptxd_install_find

ptxd_install_tree() {
    local cmd="file"
    local src="${1}"
    local dst="${2}"
    local umask=0000
    shift 2
    ptxd_install_find "${src}" "${dst}" "$@" ||
    ptxd_install_error "install_tree failed!"
}
export -f ptxd_install_tree

ptxd_install_glob() {
    local cmd="file"
    local src="${1}"
    local dst="${2}"
    set +B -f
    local yglob=( ${3} )
    local nglob=( ${4} )
    set -B +f
    local umask=0000
    local -a glob

    if [ -n "${3}" ]; then
	local pattern
	for pattern in "${yglob[@]}"; do
	    glob=( "${glob[@]}" "-o" "-path" "${pattern}" "-print" )
	done
    else
	glob=( "-o" "-print" )
    fi
    if [ -n "${4}" ]; then
	local pattern
	for pattern in "${nglob[@]}"; do
	    glob=( "-o" "-path" "${pattern}" "-prune" "${glob[@]}" )
	done
    fi
    shift 4

    ptxd_install_find "${src}" "${dst}" "$@" ||
    ptxd_install_error "install_glob failed!"
}
export -f ptxd_install_glob

ptxd_install_alternative_tree() {
    local cmd="alternative"
    local src="${1}"
    local dst="${2}"
    local umask=0022
    shift 2
    ptxd_install_find "${src}" "${dst:-${src}}" "$@" ||
    ptxd_install_error "install_alternative_tree failed!"
}
export -f ptxd_install_alternative_tree

ptxd_install_archive() {
    local archive="$1"
    local umask=0000
    shift

    if [ -L "${archive}" -a "$(readlink -f "${archive}")" = /dev/null ]; then
	echo "'${archive}' is a link to '/dev/null', skipping."
	echo
	return
    fi &&

    local dir="$(mktemp -d "${PTXDIST_TEMPDIR}/install_archive.XXXXXX")" &&

    ptxd_make_extract_archive "${archive}" "${dir}" &&
    ptxd_install_find "${dir}" "$@" &&

    rm -rf "${dir}" ||

    ptxd_install_error "install_archive failed!"
}
export -f ptxd_install_archive

#
# $1: path to spec file
#
# From linux/Documentation/filesystems/ramfs-rootfs-initramfs.txt:
#
#   file  <name> <location> <mode> <uid> <gid> [<hard links>]
#   dir   <name> <mode> <uid> <gid>
#   nod   <name> <mode> <uid> <gid> <dev_type> <maj> <min>
#   slink <name> <target> <mode> <uid> <gid>
#   pipe  <name> <mode> <uid> <gid>
#   sock  <name> <mode> <uid> <gid>
#
ptxd_install_spec() {
    local specfile="${1}"
    local type args
    local orig_IFS="${IFS}"

    ptxd_exist "${specfile}"

    while read type args; do
	set -- ${args}
	case "${type}" in
	    "file")
		local name="$1"
		local location="$2"
		local mode="$3"
		local uid="$4"
		local gid="$5"

		case "${location}" in
		    /*)
			ptxd_install_file "${location}" "${name}" "${uid}" "${gid}" "${mode}"
			;;
		    *)
			ptxd_install_alternative "/${location}" "${name}" "${uid}" "${gid}" "${mode}"
			;;
		esac
		;;

	    "dir")
		local name="$1"
		local mode="$2"
		local uid="$3"
		local gid="$4"

		ptxd_install_dir "${name}" "${uid}" "${gid}" "${mode}"
		;;

	    "nod")
		local name="$1"
		local mode="$2"
		local uid="$3"
		local gid="$4"
		local dev_type="$5"
		local maj="$6"
		local min="$7"

		ptxd_install_node "${name}" "${uid}" "${gid}" "${mode}" "${dev_type}" "${maj}" "${min}"
		;;

	    "slink")
		local name="$1"
		local target="$2"
		local mode="$3"
		local uid="$4"
		local gid="$5"

		ptxd_install_link "${name}" "${target}" "${uid}" "${gid}"
		;;

	    "pipe"|"sock")
		ptxd_install_error "${type} not supported: ${type} ${args}"
		;;

	    \#*|"")
		;;
	    *)
		ptxd_install_error "Unknown type ${type}"
		;;
	esac
    done < "${specfile}"
}
export -f ptxd_install_spec

ptxd_install_package() {
    local lib_dir=$(ptxd_get_lib_dir)
    local umask=0000

    for dir in "${pkg_pkg_dir}/"{,usr/}{bin,sbin,libexec}; do
	find "${dir}" \( -type f -o -type l \) \
		    -perm /111 2>/dev/null | while read file; do
	    ptxd_install_generic - "${file#${pkg_pkg_dir}}" ||
	    ptxd_install_error "install_package failed!"
	done
    done

    for dir in "${pkg_pkg_dir}/"{,usr/}${lib_dir}; do
	find "${dir}" \( -type f -o -type l \) \
		    -a -name "*.so*" 2>/dev/null | while read file; do
	    ptxd_install_generic - "${file#${pkg_pkg_dir}}" ||
	    ptxd_install_error "install_package failed!"
	done
    done
}
export -f ptxd_install_package

ptxd_install_shared() {
    local src="$1"
    local dst="$2"
    local usr="$3"
    local grp="$4"
    local mod="$5"
    local strip="${6:-y}"
    local filename="${src##*/}"

    ptxd_install_file "${src}" "${dst}/${filename}" "${usr}" "${grp}" "${mod}" "${strip}" &&

    find -H "${src%/*}" -maxdepth 1 -type l ! -name "*.so" | while read file; do
	if [ "$(basename "$(readlink -f "${file}")")" = "${filename}" ]; then
	    local link="${dst}/${file##*/}"
	    ptxd_install_ln "${filename}" "${link}" "${usr}" "${grp}" || return
	fi
    done
}
export -f ptxd_install_shared

ptxd_install_lib() {
    local lib_dir=$(ptxd_get_lib_dir)
    local lib="$1"
    local root_dir="${2}"
    shift 2

    if [ -n "${root_dir}" ]; then
	root_dir="${root_dir%/}"

	if [[ "${root_dir}" != /* ]]; then
	    ptxd_install_error "ptxd_lib_install: root_dir '${root_dir}' must be absolute"
	fi
    fi

    local file="$(
	for dir in "${pkg_pkg_dir}"${root_dir}{/,/usr/}${lib_dir}; do
	    if [ -d "${dir}" ]; then
		find "${dir}" -type f -path "${dir}/${lib}.so*" ! -name "*.debug"
	    fi
	done | while read f; do
		grep -q '^INPUT(' "${f}" || echo "${f}"
	    done)"

    if [ ! -f "${file}" ]; then
	ptxd_install_error "ptxd_lib_install: cannot find library '${lib}'!"
    fi

    local dst="$(dirname "${file#${pkg_pkg_dir}}")" &&
    ptxd_install_shared "${file}" "${dst}" "${@}" ||
    ptxd_install_error "ptxd_install_lib failed!"
}
export -f ptxd_install_lib

ptxd_install_run() {
    local script="${pkg_xpkg_control_dir}/${1}"
    local dir
    local mod_nfs mod_rw dst_lock

    if [ -e "${script}" ]; then
	ptxd_install_setup &&
	echo "\
executing '${pkg_label}.${1}'
" &&
	for dir in "${ndirs[@]}"; do
	    DESTDIR="${dir}" /bin/sh "${script}"
	done
	ptxd_install_unlock
    fi ||
    ptxd_install_error "running '${1}' script failed!"
}
export -f ptxd_install_run

ptxd_install_fixup_timestamps() {
    local timestamp="@${SOURCE_DATE_EPOCH}"
    local touch_args
    if touch --help | grep -q -- --no-dereference &> /dev/null; then
	touch_args="--no-dereference"
    fi

    find "${1}" -print0 | xargs -0 touch ${touch_args} -c -d "${timestamp}"
}
export -f ptxd_install_fixup_timestamps

ptxd_make_xpkg_pkg() {
    local pkg_xpkg_tmp="$1"
    local pkg_xpkg_dbg_tmp="$2"
    local pkg_xpkg_cmds="$3"
    local pkg_xpkg_perms="$4"
    local -a dirs ndirs pdirs ddirs

    ptxd_install_setup_global &&

    . "${pkg_xpkg_cmds}" &&

    ptxd_install_fixup_timestamps "${pkg_xpkg_tmp}"
}
export -f ptxd_make_xpkg_pkg