7 numfiles
=$
(find "${1}" -mindepth 1 -type f
,l
,c
,b
,d
,p
-print |
wc -l)
8 printf 'CatFile1\x00%s\x00' "${numfiles}" > "${2}"
11 for file in "${1}"/**; do
12 [ -e "$file" ] ||
continue # Skip if file doesn't exist
15 finfo
=( $
(stat
-c "%i %f %X %Y %Z %T %t %u %g %U %G %h %s" "${fname}") )
16 ftype
=0; flinkname
=""; fsize
=0
20 fatime
=$
(printf "%x" ${finfo[2]})
21 fmtime
=$
(printf "%x" ${finfo[3]})
22 fctime
=$
(printf "%x" ${finfo[4]})
23 fbtime
=$
(printf "%x" ${finfo[4]}) # Same as ctime in this script's context
24 fdev_minor
=$
(printf "%x" ${finfo[5]})
25 fdev_major
=$
(printf "%x" ${finfo[6]})
26 fuid
=$
(printf "%x" ${finfo[7]})
27 fgid
=$
(printf "%x" ${finfo[8]})
30 flinkcount
=$
(printf "%x" ${finfo[11]})
31 fsizehex
=$
(printf "%x" ${finfo[12]})
33 case $
(stat
-c %F
"${fname}") in
34 'regular file') ftype
=0 ;;
35 'symbolic link') ftype
=2; flinkname
=$
(readlink
-f "${fname}") ;;
36 'character special file') ftype
=3 ;;
37 'block special file') ftype
=4 ;;
38 'directory') ftype
=5 ;;
42 if [ $ftype -eq 0 ] && [ -n "${inodetofile[$finode]}" ]; then
44 flinkname
=${inodetofile[$finode]}
46 inodetofile
[$finode]=$fname
50 ftypehex
=$
(printf "%x" $ftype)
51 finodehex
=$
(printf "%x" $finode)
52 printf "%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00%s\x00" "$ftypehex" "$fname" "$flinkname" "$fsizehex" "$fatime" "$fmtime" "$fctime" "$fbtime" "$fmode" "$fuid" "$funame" "$fgid" "$fgname" "$fcurinode" "$finodehex" "$flinkcount" "$fdev_minor" "$fdev_major" "$fdev_minor" "$fdev_major" "0" > "$tmpfile"
55 if [ "${4}" == "none" ]; then
56 echo -n "none\x00" > "$tmpfile" # Writing directly to temporary file
57 catfileheadercshex
="0"
58 catfilecontentcshex
="0"
59 elif [ -z "${4}" ] ||
[ "${4}" == "crc32" ]; then
60 catfileheadercshex
=$
(crc32
"$tmpfile" | cut
-d ' ' -f 1)
61 catfilecontentcshex
=$
(if [ -f "$fname" ]; then crc32
"$fname"; else crc32
/dev
/null
; fi | cut
-d ' ' -f 1)
63 checksum_command
="${4}sum"
64 catfileheadercshex
=$
($checksum_command "$tmpfile" | cut
-d ' ' -f 1)
65 catfilecontentcshex
=$
(if [ -f "$fname" ]; then $checksum_command "$fname"; else $checksum_command /dev
/null
; fi | cut
-d ' ' -f 1)
68 cat "$tmpfile" >> "${2}"
69 printf "%s\x00%s\x00" "$catfileheadercshex" "$catfilecontentcshex" >> "${2}"
70 [ -f "$fname" ] && cat "$fname" >> "${2}"
71 printf '\x00' >> "${2}"
77 gzip) gzip --quiet --best "${2}" ;;
78 bzip2) bzip2 --compress --quiet --best "${2}" ;;
79 zstd
) zstd
-19 --rm -qq --format=zstd
"${2}" ;;
80 lz4
) lz4
-9 -z --rm -qq "${2}" ;;
81 lzo
) lzop
-9 -U -q "${2}" ;;
82 lzma
) lzma
--compress --quiet -9 --extreme "${2}" ;;
83 xz
) xz
--compress --quiet -9 --extreme "${2}" ;;
84 brotli
) brotli
--rm --best "${2}" ;;
85 *) echo "Unsupported compression method: ${3}" >&2 ;;
89 PackCatFile
"${1}" "${2}" "${3}" "${4}"