-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze-archive.sh
More file actions
executable file
·486 lines (428 loc) · 12.1 KB
/
Copy pathanalyze-archive.sh
File metadata and controls
executable file
·486 lines (428 loc) · 12.1 KB
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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" && pwd)"
# shellcheck source=common.sh
source "${SCRIPT_DIR}/common.sh"
tmp_manifest=""
tmp_tar_entries=""
tmp_tar_command=""
cleanup() {
if [[ -n "$tmp_manifest" && -f "$tmp_manifest" ]]; then
rm -f "$tmp_manifest"
fi
if [[ -n "$tmp_tar_entries" && -f "$tmp_tar_entries" ]]; then
rm -f "$tmp_tar_entries"
fi
if [[ -n "$tmp_tar_command" && -f "$tmp_tar_command" ]]; then
rm -f "$tmp_tar_command"
fi
}
usage() {
cat <<'EOF'
Usage:
analyze-archive.sh [options] ARCHIVE
Description:
Streams every regular file contained in the provided archive (7z, rar, tar, or
zip), computes its SHA-256 digest without writing the extracted data to disk,
and saves the list of hashes to an output file sorted by path.
Options:
-o, --output FILE Target SHA-256 manifest (default: ARCHIVE basename + .sha256)
-q, --quiet Suppress progress logs
--overwrite Overwrite existing manifest if present
-p, --password PWD Password for encrypted archives (default: INVALID_PASSWORD)
-h, --help Show this help message
EOF
}
parse_7z_listing() {
local listing_tmp="$1"
local line path attrs started=0
flush_entry() {
if [[ -n "$path" && "$attrs" != *D* ]]; then
printf '%s\0' "$path"
fi
path=""
attrs=""
}
while IFS= read -r line; do
line="${line%$'\r'}"
if [[ "$line" == "----------" ]]; then
if [[ "$started" -eq 0 ]]; then
started=1
else
flush_entry
fi
continue
fi
[[ "$started" -eq 0 ]] && continue
if [[ -z "$line" ]]; then
flush_entry
continue
fi
if [[ "$line" == Path\ =\ * ]]; then
path="${line#Path = }"
elif [[ "$line" == Attributes\ =\ * ]]; then
attrs="${line#Attributes = }"
fi
done <"$listing_tmp"
rm -f "$listing_tmp"
flush_entry
}
list_archive_files_7z() {
local archive="$1"
local listing_tmp listing_output
listing_tmp="$(mktemp)"
if ! "$SEVENZ_BIN" l -slt -- "$archive" >"$listing_tmp" 2>&1; then
listing_output="$(cat "$listing_tmp")"
rm -f "$listing_tmp"
if [[ -n "$listing_output" ]]; then
printf '%s\n' "$listing_output" >&2
fi
die "Failed to list entries for $archive"
fi
parse_7z_listing "$listing_tmp"
}
process_tar_archive() {
local archive="$1" entries_file="$2" command_file="$3"
local compression_cmd
compression_cmd="$(get_decompression_command "$TAR_COMPRESSION")"
if [[ "$compression_cmd" == "unknown" ]]; then
compression_cmd="cat"
fi
if [[ "$TAR_COMPRESSION" == "none" ]]; then
if ! tar --null --files-from="$entries_file" --to-command="$command_file" -xf "$archive"; then
die "Failed to process tar entries"
fi
else
if ! cat "$archive" | $compression_cmd | tar --null --files-from="$entries_file" --to-command="$command_file" -xf -; then
die "Failed to process tar entries"
fi
fi
}
tar_list_entries() {
local archive="$1" dest="$2"
run_tar_list() {
local tar_base=(tar --quoting-style=literal --show-transformed-names -tf)
case "$TAR_COMPRESSION" in
gz)
pigz -dc -- "$archive" | "${tar_base[@]}" -
;;
bz2)
pbzip2 -dc -- "$archive" | "${tar_base[@]}" -
;;
xz)
pixz -d -c -- "$archive" | "${tar_base[@]}" -
;;
zst)
pzstd -d -q -c -- "$archive" | "${tar_base[@]}" -
;;
none)
"${tar_base[@]}" "$archive"
;;
esac
}
if ! run_tar_list >"$dest" 2>&1; then
cat "$dest" >&2
rm -f "$dest"
die "Failed to list tar entries for $archive"
fi
}
tar_extract_entry() {
local archive="$1" entry="$2"
case "$TAR_COMPRESSION" in
gz) pigz -dc -- "$archive" | tar -xOf - -- "$entry" ;;
bz2) pbzip2 -dc -- "$archive" | tar -xOf - -- "$entry" ;;
xz) pixz -d -c -- "$archive" | tar -xOf - -- "$entry" ;;
zst) pzstd -d -q -c -- "$archive" | tar -xOf - -- "$entry" ;;
none) tar -xOf "$archive" -- "$entry" ;;
esac
}
list_archive_files_tar() {
local archive="$1" entry listing_tmp
listing_tmp="$(mktemp)"
tar_list_entries "$archive" "$listing_tmp"
while IFS= read -r entry; do
[[ -z "$entry" ]] && continue
[[ "$entry" == */ ]] && continue
printf '%s\0' "$entry"
done <"$listing_tmp"
rm -f "$listing_tmp"
}
list_archive_files_zip() {
local archive="$1"
local listing_tmp
listing_tmp="$(mktemp)"
if ! "$SEVENZ_BIN" l -slt -- "$archive" >"$listing_tmp" 2>&1; then
cat "$listing_tmp" >&2
rm -f "$listing_tmp"
die "Failed to list zip entries for $archive"
fi
parse_7z_listing "$listing_tmp"
}
list_archive_files_rar() {
local archive="$1" entry line entry_type
local listing_tmp
listing_tmp="$(mktemp)"
if ! unrar lt -- "$archive" >"$listing_tmp" 2>&1; then
cat "$listing_tmp" >&2
rm -f "$listing_tmp"
die "Failed to list rar entries for $archive"
fi
# Check if the output contains an error message about not being a RAR archive
if grep -q "is not RAR archive" "$listing_tmp"; then
cat "$listing_tmp" >&2
rm -f "$listing_tmp"
die "Failed to list rar entries for $archive"
fi
# Parse unrar lt output which has a clean key: value format
# We need to track both Name and Type fields for each entry
while IFS= read -r line; do
# Look for lines that start with "Name:" and extract the filename
if [[ "$line" =~ ^[[:space:]]*Name:[[:space:]]*(.+)$ ]]; then
entry="${BASH_REMATCH[1]}"
continue
fi
# Look for lines that start with "Type:" to determine if it's a file
if [[ "$line" =~ ^[[:space:]]*Type:[[:space:]]*(.+)$ ]]; then
entry_type="${BASH_REMATCH[1]}"
# Only process if we have a name and it's a file (not directory)
if [[ -n "$entry" && "$entry_type" == "File" ]]; then
printf '%s\0' "$entry"
fi
# Reset entry for next iteration
entry=""
fi
done <"$listing_tmp"
rm -f "$listing_tmp"
}
list_archive_files() {
case "$ARCHIVE_TYPE" in
7z) list_archive_files_7z "$ARCHIVE" ;;
tar|tar.gz|tar.xz|tar.bz2) list_archive_files_tar "$ARCHIVE" ;;
zip) list_archive_files_zip "$ARCHIVE" ;;
rar) list_archive_files_rar "$ARCHIVE" ;;
*)
die "Unsupported archive type for $ARCHIVE"
;;
esac
}
stream_entry() {
local archive="$1" entry="$2"
case "$ARCHIVE_TYPE" in
7z)
"$SEVENZ_BIN" x -so -p"$ARCHIVE_PASSWORD" -- "$archive" "$entry"
;;
tar|tar.gz|tar.xz|tar.bz2)
tar_extract_entry "$archive" "$entry"
;;
zip)
"$SEVENZ_BIN" x -so -p"$ARCHIVE_PASSWORD" -- "$archive" "$entry"
;;
rar)
unrar p -idq -p"$ARCHIVE_PASSWORD" -- "$archive" "$entry"
;;
*)
die "Unsupported archive type for streaming: $ARCHIVE_TYPE"
;;
esac
}
compute_sha256_and_size() {
local archive="$1" entry="$2"
local tmp_size hash size
tmp_size="$(mktemp)"
if ! hash="$(stream_entry "$archive" "$entry" | tee >(wc -c | awk '{print $1}' >"$tmp_size") | sha256sum | awk '{print $1}')"; then
rm -f "$tmp_size"
return 1
fi
if ! read -r size <"$tmp_size"; then
rm -f "$tmp_size"
return 1
fi
rm -f "$tmp_size"
printf '%s\t%s\n' "$hash" "$size"
}
ARCHIVE=""
OUTPUT_FILE=""
QUIET=0
OVERWRITE=0
ARCHIVE_TYPE="7z"
SEVENZ_BIN="7z"
TAR_COMPRESSION="none"
ARCHIVE_PASSWORD="INVALID_PASSWORD"
setup_archive_tools() {
case "$ARCHIVE_TYPE" in
zip|7z)
select_7z_tool
;;
rar)
require_cmd unrar
;;
tar|tar.gz|tar.xz|tar.bz2)
require_cmd tar
TAR_COMPRESSION="$(detect_tar_compression "$ARCHIVE")"
local filter_tool
filter_tool="$(get_decompression_command "$TAR_COMPRESSION")"
if [[ "$filter_tool" == "unknown" ]]; then
filter_tool="cat"
fi
filter_tool="$(echo "$filter_tool" | awk '{print $1}')"
if [[ "$filter_tool" != "cat" ]]; then
require_cmd "$filter_tool"
fi
;;
*)
die "Archive type '$ARCHIVE_TYPE' is not supported."
;;
esac
}
select_7z_tool() {
if command -v 7z >/dev/null 2>&1; then
SEVENZ_BIN="7z"
return 0
elif command -v 7zr >/dev/null 2>&1; then
SEVENZ_BIN="7zr"
return 0
else
die "Neither '7z' nor '7zr' is available on PATH."
fi
}
while [[ $# -gt 0 ]]; do
case "$1" in
-o|--output)
[[ $# -lt 2 ]] && die "Missing value for $1"
OUTPUT_FILE="$2"
shift 2
;;
-q|--quiet)
QUIET=1
shift
;;
--overwrite)
OVERWRITE=1
shift
;;
-p|--password)
[[ $# -lt 2 ]] && die "Missing value for $1"
ARCHIVE_PASSWORD="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-*)
die "Unknown option: $1"
;;
*)
if [[ -z "$ARCHIVE" ]]; then
ARCHIVE="$1"
shift
else
die "Only one archive can be processed at a time."
fi
;;
esac
done
if [[ -z "$ARCHIVE" ]]; then
die "Archive path is required."
fi
if [[ ! -f "$ARCHIVE" ]]; then
die "Archive not found: $ARCHIVE"
fi
if is_split_archive "$ARCHIVE" && ! is_first_chunk "$ARCHIVE"; then
die "Failed to list entries for $ARCHIVE"
fi
ARCHIVE_TYPE="$(detect_archive_type "$ARCHIVE")"
if [[ "$ARCHIVE_TYPE" == "unknown" ]]; then
log "Archive type not recognized; defaulting to 7z handlers."
ARCHIVE_TYPE="7z"
fi
setup_archive_tools
require_cmd sha256sum
if [[ -z "$OUTPUT_FILE" ]]; then
OUTPUT_FILE="$(default_sha256_path "$ARCHIVE")"
fi
if [[ -e "$OUTPUT_FILE" && "$OVERWRITE" -ne 1 ]]; then
log "skip (manifest exists): $OUTPUT_FILE"
exit 0
fi
mkdir -p -- "$(dirname -- "$OUTPUT_FILE")"
tmp_manifest="$(mktemp)"
TMP_MANIFEST="$tmp_manifest"
export TMP_MANIFEST
trap cleanup EXIT
log "Writing SHA-256 manifest to $OUTPUT_FILE"
# Optimization for tar files - process all entries in a single decompression pass
if [[ "$ARCHIVE_TYPE" == "tar" || "$ARCHIVE_TYPE" == "tar.gz" || "$ARCHIVE_TYPE" == "tar.xz" || "$ARCHIVE_TYPE" == "tar.bz2" ]]; then
files_processed=0
log "Using optimized tar processing"
tmp_tar_entries="$(mktemp)"
if ! list_archive_files "$ARCHIVE" >"$tmp_tar_entries"; then
die "Failed to enumerate archive entries for $ARCHIVE"
fi
while IFS= read -r -d '' entry; do
files_processed=$((files_processed + 1))
done <"$tmp_tar_entries"
if [[ "$files_processed" -gt 0 ]]; then
tmp_tar_command="$(mktemp)"
cat >"$tmp_tar_command" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
f="${TAR_FILENAME:-}"
if [[ -z "$f" || "$f" == */ ]]; then
exit 0
fi
printf 'processing: %s\n' "$f" >&2
tmp_size="$(mktemp)"
if ! hash="$(tee >(wc -c | awk '{print $1}' >"$tmp_size") | sha256sum | awk '{print $1}')"; then
rm -f "$tmp_size"
exit 1
fi
if ! read -r size <"$tmp_size"; then
size=0
fi
rm -f "$tmp_size"
if [[ -n "${TMP_MANIFEST:-}" ]]; then
printf '%s\t%s\t%s\n' "$hash" "$f" "$size" >>"$TMP_MANIFEST"
fi
printf '%s %s %s\n' "$hash" "$f" "$size"
EOF
chmod +x "$tmp_tar_command"
log "Processing $files_processed file(s) in a single pass"
process_tar_archive "$ARCHIVE" "$tmp_tar_entries" "$tmp_tar_command"
fi
else
# Fallback processing for archive types without streaming optimizations
files_processed=0
tmp_entries_list="$(mktemp)"
if ! list_archive_files "$ARCHIVE" >"$tmp_entries_list"; then
rm -f "$tmp_entries_list"
die "Failed to enumerate archive entries for $ARCHIVE"
fi
while IFS= read -r -d '' entry; do
files_processed=$((files_processed + 1))
log "processing: $entry"
if ! result="$(compute_sha256_and_size "$ARCHIVE" "$entry")"; then
rm -f "$tmp_entries_list"
die "Failed to compute SHA-256 for $entry"
fi
hash="${result%%$'\t'*}"
size="${result#*$'\t'}"
if [[ "$size" == "$hash" ]]; then
size=""
fi
printf '%s\t%s\t%s\n' "$hash" "$entry" "$size" >>"$tmp_manifest"
printf '%s %s %s\n' "$hash" "$entry" "$size"
done <"$tmp_entries_list"
rm -f "$tmp_entries_list"
fi
if [[ "$files_processed" -eq 0 ]]; then
log "No files found inside archive."
rm -f -- "$OUTPUT_FILE"
else
LC_ALL=C sort -t $'\t' -k2,2 "$tmp_manifest" | awk -F $'\t' '{printf "%s %s %s\n", $1, $2, $3}' >"$OUTPUT_FILE"
log "Processed $files_processed file(s)."
fi