Small update
[PyCatFile.git] / catfile.sh
blob4224ed1ef7de2ba74ce5f18e046beaee573cb261
1 #!/usr/bin/env bash
3 function PackCatFile {
4 shopt -s globstar
5 curinode=0
6 numfiles=$(find "${1}" -mindepth 1 -print | wc -l)
7 echo -n -e 'CatFile1\x00${numfiles}\x00' > ${2}
8 for file in ${1}/**; do
9 fname="${file%/}"
10 echo "${fname}"
11 flinkname=""
12 fcurinode=${curinode}
13 finode=$(stat -c %i ${fname})
14 ftype=0
15 if [ -f ${fname} ]; then
16 ftype=0
17 fsize=$(printf "%x" $(stat -c %s ${fname}))
19 if [ -L ${fname} ]; then
20 ftype=2
21 fsize=0
22 flinkname="$(readlink -f ${fname})"
24 if [ -c ${fname} ]; then
25 ftype=3
26 fsize=0
28 if [ -b ${fname} ]; then
29 ftype=4
30 fsize=0
32 if [ -d ${fname} ]; then
33 ftype=5
34 fsize=0
36 if [ -p ${fname} ]; then
37 ftype=6
38 fsize=0
40 if [ -f ${fname} ]; then
41 if [[ ${inodetofile[${finode}]} ]]; then
42 ftype=1
43 flinkname=${inodetofile[${finode}]}
44 else
45 inodetofile[${finode}]=${fname}
46 curinode=$[curinode + 1]
49 fdev_minor=$(printf "%x" $(stat -c %T ${fname}))
50 fdev_major=$(printf "%x" $(stat -c %t ${fname}))
51 fatime=$(printf "%x" $(stat -c %X ${fname}))
52 fmtime=$(printf "%x" $(stat -c %Y ${fname}))
53 fctime=$(printf "%x" $(stat -c %Z ${fname}))
54 fbtime=$(printf "%x" $(stat -c %Z ${fname}))
55 fmode=$(stat -c %f ${fname})
56 fchmode=$(printf "%x" 0$(stat -c %a ${fname}))
57 fuid=$(printf "%x" $(stat -c %u ${fname}))
58 fgid=$(printf "%x" $(stat -c %g ${fname}))
59 funame=$(stat -c %U ${fname})
60 fgname=$(stat -c %G ${fname})
61 flinkcount=$(printf "%x" $(stat -c %h ${fname}))
62 finodehex=$(printf "%x" ${finode})
63 ftypehex=$(printf "%x" ${ftype})
64 tmpfile=$(mktemp);
65 echo -n "${ftypehex}" > ${tmpfile}
66 echo -n -e '\x00' >> ${tmpfile}
67 echo -n "${fname}" >> ${tmpfile}
68 echo -n -e '\x00' >> ${tmpfile}
69 echo -n "${flinkname}" >> ${tmpfile}
70 echo -n -e '\x00' >> ${tmpfile}
71 echo -n "${fsize}" >> ${tmpfile}
72 echo -n -e '\x00' >> ${tmpfile}
73 echo -n "${fatime}" >> ${tmpfile}
74 echo -n -e '\x00' >> ${tmpfile}
75 echo -n "${fmtime}" >> ${tmpfile}
76 echo -n -e '\x00' >> ${tmpfile}
77 echo -n "${fctime}" >> ${tmpfile}
78 echo -n -e '\x00' >> ${tmpfile}
79 echo -n "${fbtime}" >> ${tmpfile}
80 echo -n -e '\x00' >> ${tmpfile}
81 echo -n "${fmode}" >> ${tmpfile}
82 echo -n -e '\x00' >> ${tmpfile}
83 echo -n "${fuid}" >> ${tmpfile}
84 echo -n -e '\x00' >> ${tmpfile}
85 echo -n "${funame}" >> ${tmpfile}
86 echo -n -e '\x00' >> ${tmpfile}
87 echo -n "${fgid}" >> ${tmpfile}
88 echo -n -e '\x00' >> ${tmpfile}
89 echo -n "${fgname}" >> ${tmpfile}
90 echo -n -e '\x00' >> ${tmpfile}
91 echo -n "${fcurinode}" >> ${tmpfile}
92 echo -n -e '\x00' >> ${tmpfile}
93 echo -n "${finodehex}" >> ${tmpfile}
94 echo -n -e '\x00' >> ${tmpfile}
95 echo -n "${flinkcount}" >> ${tmpfile}
96 echo -n -e '\x00' >> ${tmpfile}
97 echo -n "${fdev_minor}" >> ${tmpfile}
98 echo -n -e '\x00' >> ${tmpfile}
99 echo -n "${fdev_major}" >> ${tmpfile}
100 echo -n -e '\x00' >> ${tmpfile}
101 echo -n "${fdev_minor}" >> ${tmpfile}
102 echo -n -e '\x00' >> ${tmpfile}
103 echo -n "${fdev_major}" >> ${tmpfile}
104 echo -n -e '\x00' >> ${tmpfile}
105 echo -n "0" >> ${tmpfile}
106 echo -n -e '\x00' >> ${tmpfile}
107 if [ "${4}" == "none" ]; then
108 echo -n "${4}" >> ${tmpfile}
109 echo -n -e '\x00' >> ${tmpfile}
110 catfileheadercshex="0"
111 catfilecontentcshex="0"
112 elif [ "${4}" == "crc32" ] || [ "${4}" == "" ]; then
113 echo -n "" >> ${tmpfile}
114 echo -n -e '\x00' >> ${tmpfile}
115 catfileheadercshex=$(crc32 ${tmpfile} | cut -d ' ' -f 1)
116 if [ -f ${fname} ]; then
117 catfilecontentcshex=$(crc32 ${fname} | cut -d ' ' -f 1)
118 else
119 catfilecontentcshex=$(crc32 /dev/null | cut -d ' ' -f 1)
121 elif [ "${4}" == "md5" ]; then
122 echo -n "md5" >> ${tmpfile}
123 echo -n -e '\x00' >> ${tmpfile}
124 catfileheadercshex=$(md5sum ${tmpfile} | cut -d ' ' -f 1)
125 if [ -f ${fname} ]; then
126 catfilecontentcshex=$(md5sum ${fname} | cut -d ' ' -f 1)
127 else
128 catfilecontentcshex=$(md5sum /dev/null | cut -d ' ' -f 1)
130 elif [ "${4}" == "sha1" ]; then
131 echo -n "${4}" >> ${tmpfile}
132 echo -n -e '\x00' >> ${tmpfile}
133 catfileheadercshex=$(sha1sum ${tmpfile} | cut -d ' ' -f 1)
134 if [ -f ${fname} ]; then
135 catfilecontentcshex=$(sha1sum ${fname} | cut -d ' ' -f 1)
136 else
137 catfilecontentcshex=$(sha1sum /dev/null | cut -d ' ' -f 1)
139 elif [ "${4}" == "sha224" ]; then
140 echo -n "${4}" >> ${tmpfile}
141 echo -n -e '\x00' >> ${tmpfile}
142 catfileheadercshex=$(sha224sum ${tmpfile} | cut -d ' ' -f 1)
143 if [ -f ${fname} ]; then
144 catfilecontentcshex=$(sha224sum ${fname} | cut -d ' ' -f 1)
145 else
146 catfilecontentcshex=$(sha224sum /dev/null | cut -d ' ' -f 1)
148 elif [ "${4}" == "sha256" ]; then
149 echo -n "${4}" >> ${tmpfile}
150 echo -n -e '\x00' >> ${tmpfile}
151 catfileheadercshex=$(sha256sum ${tmpfile} | cut -d ' ' -f 1)
152 if [ -f ${fname} ]; then
153 catfilecontentcshex=$(sha256sum ${fname} | cut -d ' ' -f 1)
154 else
155 catfilecontentcshex=$(sha256sum /dev/null | cut -d ' ' -f 1)
157 elif [ "${4}" == "sha384" ]; then
158 echo -n "${4}" >> ${tmpfile}
159 echo -n -e '\x00' >> ${tmpfile}
160 catfileheadercshex=$(sha384sum ${tmpfile} | cut -d ' ' -f 1)
161 if [ -f ${fname} ]; then
162 catfilecontentcshex=$(sha384sum ${fname} | cut -d ' ' -f 1)
163 else
164 catfilecontentcshex=$(sha384sum /dev/null | cut -d ' ' -f 1)
166 elif [ "${4}" == "sha512" ]; then
167 echo -n "${4}" >> ${tmpfile}
168 echo -n -e '\x00' >> ${tmpfile}
169 catfileheadercshex=$(sha512sum ${tmpfile} | cut -d ' ' -f 1)
170 if [ -f ${fname} ]; then
171 catfilecontentcshex=$(sha512sum ${fname} | cut -d ' ' -f 1)
172 else
173 catfilecontentcshex=$(sha512sum /dev/null | cut -d ' ' -f 1)
175 else
176 echo -n "crc32" >> ${tmpfile}
177 echo -n -e '\x00' >> ${tmpfile}
178 catfileheadercshex=$(crc32 ${tmpfile} | cut -d ' ' -f 1)
179 if [ -f ${fname} ]; then
180 catfilecontentcshex=$(crc32 ${fname} | cut -d ' ' -f 1)
181 else
182 catfilecontentcshex=$(crc32 /dev/null | cut -d ' ' -f 1)
185 cat ${tmpfile} >> ${2}
186 rm -rf ${tmpfile}
187 echo -n "${catfileheadercshex}" >> ${2}
188 echo -n -e '\x00' >> ${2}
189 echo -n "${catfilecontentcshex}" >> ${2}
190 echo -n -e '\x00' >> ${2}
191 if [ -f ${fname} ]; then
192 cat ${fname} >> ${2}
194 echo -n -e '\x00' >> ${2}
195 done
196 if [ "${3}" == "gzip" ]; then
197 gzip --quiet --best ${2}
198 elif [ "${3}" == "bzip2" ]; then
199 gzip --compress --quiet --best ${2}
200 elif [ "${3}" == "zstd" ]; then
201 zstd -19 --rm -qq --format=zstd ${2}
202 elif [ "${3}" == "lz4" ]; then
203 lz4 -9 -z --rm -qq ${2}
204 elif [ "${3}" == "lzo" ]; then
205 lzop -9 -U -q ${2}
206 elif [ "${3}" == "lzma" ]; then
207 lzma --compress --quiet -9 --extreme ${2}
208 elif [ "${3}" == "xz" ]; then
209 xz --compress --quiet -9 --extreme ${2}
210 elif [ "${3}" == "brotli" ]; then
211 brotli --rm --best ${2}
215 PackCatFile "${1}" "${2}" "${3}" "${4}"