Repository navigation
Expand file tree
/
Copy pathcommon.sh
More file actions
629 lines (578 loc) · 15.1 KB
/
Copy pathcommon.sh
File metadata and controls
629 lines (578 loc) · 15.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
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
#!/usr/bin/env bash
# Shared helpers for archive-tools scripts. This file is meant to be sourced.
if [[ -n ${ARCHIVE_TOOLS_COMMON_SOURCED:-} ]]; then
return 0
fi
ARCHIVE_TOOLS_COMMON_SOURCED=1
# shellcheck shell=bash
die() {
printf 'Error: %s\n' "$1" >&2
exit 2
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Required tool '$1' is not on PATH."
}
log() {
[[ "${QUIET:-0}" -eq 1 ]] && return 0
printf '%s\n' "$*" >&2
}
# Files staged via mktemp are mode 0600; give them the permissions a
# normally-created file would have under the current umask (0666 & ~umask).
chmod_like_new_file() {
chmod -- "$(printf '%03o' $((0666 & ~8#$(umask))))" "$1"
}
prepare_file_for_write() {
local file="$1" append_flag="${2:-0}"
[[ -z "$file" ]] && return 0
mkdir -p -- "$(dirname -- "$file")" 2>/dev/null || true
if [[ "$append_flag" -eq 1 ]]; then
: >>"$file"
else
: >"$file"
fi
}
# Manifest records are "hash path size" on one line, so control characters
# in a path must be escaped to keep records parseable: \ -> \\, tab -> \t,
# newline -> \n, CR -> \r. Consumers treat the field opaquely, so identical
# real names escape identically and still compare equal.
escape_manifest_path() {
local p="$1"
p="${p//\\/\\\\}"
p="${p//$'\n'/\\n}"
p="${p//$'\t'/\\t}"
p="${p//$'\r'/\\r}"
printf '%s' "$p"
}
write_sha256_manifest() {
local root="$1" dest="$2" file rel esc hash size ex
shift 2
local -a excludes=("$@")
[[ -z "$dest" ]] && return 0
root="${root%"${root##*[!/]}"}" # strip trailing slashes
[[ -z "$root" ]] && root="/"
# Buffer the listing first: a find that fails mid-scan (e.g. unreadable
# subdirectory) would otherwise produce a silently incomplete manifest.
local tmp_list
tmp_list="$(mktemp)"
if ! LC_ALL=C find "$root" -type f -print0 | LC_ALL=C sort -z >"$tmp_list"; then
rm -f "$tmp_list"
printf 'Error: failed to enumerate files under %s\n' "$root" >&2
return 1
fi
while IFS= read -r -d '' file; do
# Literal prefix strip: ${file#$root/} would treat glob characters in
# the root as patterns; a positional substring does not.
if [[ "$root" == "/" ]]; then
rel="${file#/}"
elif [[ "${file:0:${#root}+1}" == "$root/" ]]; then
rel="${file:${#root}+1}"
else
rel="$(basename -- "$file")"
fi
for ex in "${excludes[@]}"; do
[[ -n "$ex" && "$rel" == "$ex" ]] && continue 2
done
# sha256sum prefixes the line (and thus $1) with '\' when the name
# needs escaping; strip it so the hash field stays 64 hex chars.
hash="$(sha256sum -- "$file" | awk '{sub(/^\\/, ""); print $1}')"
size="$(stat -c '%s' -- "$file")"
esc="$(escape_manifest_path "$rel")"
printf '%s %s %s\n' "$hash" "$esc" "$size" >>"$dest"
done <"$tmp_list"
rm -f "$tmp_list"
}
# Archive type detection
is_split_archive() {
local archive="$1"
local lowered="${archive,,}"
[[ "$lowered" =~ \.(7z|zip|rar)\.[0-9]+$ ]] ||
[[ "$lowered" =~ \.part[0-9]+\.rar$ ]] ||
[[ "$lowered" =~ \.r[0-9]+$ ]] ||
[[ "$lowered" =~ \.z[0-9]+$ ]]
}
is_first_chunk() {
local archive="$1"
local lowered="${archive,,}"
case "$lowered" in
*.001 | *.part1.rar | *.part01.rar | *.part001.rar) return 0 ;;
*) return 1 ;;
esac
}
detect_archive_type() {
local archive="$1"
local lowered="${archive,,}"
case "$lowered" in
*.7z | *.7z.001)
printf '7z'
;;
*.zip | *.zip.001 | *.z01)
printf 'zip'
;;
*.rar | *.rar.001 | *.part[0-9].rar | *.part[0-9][0-9].rar | *.part[0-9][0-9][0-9].rar)
printf 'rar'
;;
*.tar.gz | *.tgz | *.taz)
printf 'tar.gz'
;;
*.tar.xz | *.txz | *.tlz)
printf 'tar.xz'
;;
*.tar.bz2 | *.tbz | *.tbz2)
printf 'tar.bz2'
;;
*.tar.zst | *.tzst)
printf 'tar.zst'
;;
*.tar | *.tar.*)
printf 'tar'
;;
*)
printf 'unknown'
;;
esac
}
detect_tar_compression() {
local archive="$1"
local lowered="${archive,,}"
case "$lowered" in
*.tar.gz | *.tgz) echo "gz" ;;
*.tar.bz2 | *.tbz | *.tbz2) echo "bz2" ;;
*.tar.xz | *.txz) echo "xz" ;;
*.tar.zst | *.tzst) echo "zst" ;;
*.tar.lzma | *.tlz) echo "lzma" ;;
*.tar.z | *.taz) echo "Z" ;;
*) echo "none" ;;
esac
}
get_compression_extension() {
local algo="$1"
case "$algo" in
xz | pixz) echo "xz" ;;
zstd | pzstd) echo "zst" ;;
*) echo "unknown" ;;
esac
}
get_decompression_command() {
local compressor="$1"
case "$compressor" in
gz)
echo "pigz -dc"
;;
bz2)
echo "pbzip2 -dc"
;;
xz)
echo "pixz -d"
;;
lzma)
echo "xz -dc"
;;
Z)
echo "gzip -dc"
;;
zst)
echo "pzstd -d -q -c"
;;
cat | none)
echo "cat"
;;
*)
echo "unknown"
;;
esac
}
get_compressor_command() {
local compressor="$1"
case "$compressor" in
xz)
echo "xz $XZ_LEVEL -T1 -c --"
;;
zstd)
echo "zstd $ZSTD_LEVEL -T1 -q -c --"
;;
pixz)
echo "pixz $XZ_LEVEL"
;;
xz_big)
echo "xz $XZ_LEVEL -T0 -c"
;;
pzstd)
echo "pzstd $PZSTD_LEVEL"
;;
zstd_big)
echo "zstd $ZSTD_LEVEL -T0 -q -c"
;;
*)
echo "unknown"
;;
esac
}
confirm_action() {
local prompt="$1" auto_confirm="$2"
shift 2
local -a items=("$@")
if ((auto_confirm)); then
printf 'Auto-confirmed: %s\n' "$prompt"
for item in "${items[@]}"; do
printf ' - %s\n' "$item"
done
return 0
fi
printf '%s\n' "$prompt"
printf 'The following %d item(s) will be affected:\n' "${#items[@]}"
for item in "${items[@]}"; do
printf ' - %s\n' "$item"
done
printf 'Proceed? [y/N]: '
local reply
if ! read -r reply; then
return 1
fi
case "${reply,,}" in
y | yes)
return 0
;;
*)
printf 'Action cancelled.\n'
return 1
;;
esac
}
detect_actual_format() {
local file="$1" file_output
file_output="$(file --brief --mime-type "$file" 2>/dev/null || file --brief "$file" 2>/dev/null || echo "unknown")"
case "$file_output" in
*gzip* | *application/gzip*)
echo "gz"
;;
*bzip2* | *application/bzip2*)
echo "bz2"
;;
*xz* | *application/x-xz*)
echo "xz"
;;
*zstd* | *application/zstd*)
echo "zst"
;;
*lzma* | *application/x-lzma*)
echo "lzma"
;;
*lz4* | *application/x-lz4*)
echo "lz4"
;;
*"POSIX tar archive"* | *"tar archive"* | *application/x-tar*)
echo "tar"
;;
*"compress'd data"* | *application/x-compress*)
echo "Z"
;;
*"Zip archive data"* | *application/zip*)
echo "zip"
;;
*"RAR archive data"* | *application/x-rar*)
echo "rar"
;;
*"7-zip archive data"* | *application/x-7z-compressed*)
echo "7z"
;;
*)
echo "unknown"
;;
esac
}
get_expected_extension() {
local file="${1,,}"
case "$file" in
*.gz) echo "gz" ;;
*.bz2) echo "bz2" ;;
*.xz) echo "xz" ;;
*.zst) echo "zst" ;;
*.tgz) echo "gz" ;;
*.txz) echo "xz" ;;
*.tzst) echo "zst" ;;
*.tbz | *.tbz2) echo "bz2" ;;
*.lzma | *.tlz) echo "lzma" ;;
*.taz | *.tar.z | *.z) echo "Z" ;;
*.zip | *.zip.001 | *.z01) echo "zip" ;;
*.rar | *.rar.001 | *.part[0-9].rar | *.part[0-9][0-9].rar | *.part[0-9][0-9][0-9].rar) echo "rar" ;;
*.7z | *.7z.001) echo "7z" ;;
*) echo "unknown" ;;
esac
}
rename_misnamed_file() {
local file="$1" actual_ext expected_ext new_name
actual_ext="$(detect_actual_format "$file")"
expected_ext="$(get_expected_extension "$file")"
# Skip if actual format matches expected or is unknown
if [[ "$actual_ext" == "unknown" || "$actual_ext" == "$expected_ext" ]]; then
return 0
fi
# Handle special case: file is actually a tar archive but has compression extension
if [[ "$actual_ext" == "tar" ]]; then
# Handle compound extensions like .tar.gz, .tgz, etc.
case "$file" in
*.tar.gz | *.tar.xz | *.tar.bz2 | *.tar.zst | *.tar.zip | *.tar.rar | *.tar.7z)
new_name="${file%.*.*}.tar" # Remove both extensions and add .tar
;;
*.tgz | *.txz | *.tbz | *.tbz2 | *.tzst)
new_name="${file%.*}.tar" # Remove compound extension and add .tar
;;
*.gz | *.xz | *.bz2 | *.zst | *.zip | *.rar | *.7z)
new_name="${file%.*}.tar" # Remove compression extension and add .tar
;;
*)
new_name="${file}.tar" # Just add .tar
;;
esac
# Check if target file already exists
if [[ -e "$new_name" ]]; then
log "magic: skipping rename $file -> $new_name (target already exists)"
return 1
fi
log "magic: renaming misnamed file $file -> $new_name (actual format: tar archive)"
mv -- "$file" "$new_name"
echo "$new_name"
return 0
fi
# Rename to correct compression extension
case "$file" in
*.tgz | *.txz | *.tzst | *.tbz | *.tbz2)
# For compound extensions, replace the compression part
new_name="${file%.*}.$actual_ext"
;;
*)
# For simple extensions, just replace
new_name="${file%.*}.$actual_ext"
;;
esac
# Check if target file already exists
if [[ -e "$new_name" ]]; then
log "magic: skipping rename $file -> $new_name (target already exists)"
return 1
fi
log "magic: renaming misnamed file $file -> $new_name (actual format: $actual_ext)"
mv -- "$file" "$new_name"
echo "$new_name"
}
# File path manipulation
strip_archive_suffixes() {
local name="$1" lowered
while :; do
lowered="${name,,}"
case "$lowered" in
*.tar.gz | *.tar.bz2 | *.tar.xz | *.tar.zst | *.tar.lzma | *.tar.z)
name="${name%.*}"
name="${name%.*}"
;;
*.tar)
name="${name%.tar}"
;;
*.tgz | *.txz | *.tbz | *.tbz2 | *.tzst | *.tlz | *.taz)
name="${name%.*}"
;;
*.7z.0[0-9][0-9])
name="${name%.*}"
;;
*.zip.0[0-9][0-9])
name="${name%.*}"
;;
*.z[0-9][0-9])
name="${name%.*}"
;;
*.rar.0[0-9][0-9])
name="${name%.*}"
;;
*.r[0-9][0-9])
name="${name%.*}"
;;
*.part[0-9].rar | *.part[0-9][0-9].rar | *.part[0-9][0-9][0-9].rar)
name="${name%.part[0-9].rar}"
name="${name%.part[0-9][0-9].rar}"
name="${name%.part[0-9][0-9][0-9].rar}"
;;
*.7z | *.zip | *.rar)
name="${name%.*}"
;;
*)
break
;;
esac
done
printf '%s\n' "$name"
}
default_basename_path() {
local archive="$1" dir base
dir="$(dirname -- "$archive")"
base="$(basename -- "$archive")"
base="$(strip_archive_suffixes "$base")"
printf '%s/%s\n' "$dir" "$base"
}
default_sha256_path() {
local archive="$1"
printf '%s.sha256\n' "$archive"
}
# Compression level defaults
DEFAULT_XZ_LEVEL="-5"
DEFAULT_ZSTD_LEVEL="-6"
DEFAULT_PZSTD_LEVEL="-10"
# Common compressor mappings. lzma/lzw are format names, not binaries —
# decompress.sh maps them to xz/gzip for the tool check and dispatch.
declare -A COMPRESSOR_EXTS=(
[pixz]="xz txz"
[pzstd]="zst tzst"
[pigz]="gz tgz"
[pbzip2]="bz2 tbz tbz2"
[lzma]="tlz lzma"
[lzw]="taz z"
)
# Temp directory management
create_temp_dir() {
local prefix="${1:-archive-tools}"
local parent="${2:-}"
if [[ -n "$parent" ]]; then
mkdir -p -- "$parent" || die "Failed to create temp directory parent: $parent"
mktemp -d -p "$parent" "${prefix}.XXXXXX" || die "Failed to create temporary directory under $parent"
else
mktemp -d "${prefix}.XXXXXX" || die "Failed to create temporary directory"
fi
}
validate_dir_exists() {
local dir="$1"
[[ -d "$dir" ]] || die "Directory not found: $dir"
}
# Stream a (possibly compressed) tar archive's raw tar data to stdout.
# compression: gz | bz2 | xz | lzma | Z | zst | none (see detect_tar_compression)
tar_decompress_stream() {
local archive="$1" compression="${2:-none}"
case "$compression" in
gz)
pigz -dc -- "$archive"
;;
bz2)
pbzip2 -dc -- "$archive"
;;
xz)
# pixz requires a seekable input file and an explicit output path;
# "-c" is ignored and "-d in.xz" alone would overwrite/remove inputs.
pixz -dk -- "$archive" /dev/stdout
;;
lzma)
# .tlz / .tar.lzma: lzma-alone format; xz decodes it, pixz cannot.
xz -dc -- "$archive"
;;
Z)
# .taz / .tar.Z: LZW "compress" format; gzip decodes it, pigz cannot.
gzip -dc -- "$archive"
;;
zst)
pzstd -d -q -c -- "$archive"
;;
none | "")
cat -- "$archive"
;;
*)
die "Unknown tar compression: $compression"
;;
esac
}
# Resolve the compression format of a tar stream: start from the file-name
# mapping, then cross-check the actual content with file(1). Misnamed inputs
# (e.g. a gzip stream named "*.tar") are corrected instead of being piped
# through the wrong decompressor — or, for "none", wrapped raw into the output.
resolve_tar_compression() {
local archive="$1" named actual mapped
named="$(detect_tar_compression "$archive")"
command -v file >/dev/null 2>&1 || {
printf '%s\n' "$named"
return 0
}
actual="$(detect_actual_format "$archive")"
case "$actual" in
tar) mapped="none" ;;
gz | bz2 | xz | zst | lzma | Z) mapped="$actual" ;;
zip | rar | 7z | lz4)
die "Archive '$archive' contains '$actual' data, not a tar stream"
;;
*) mapped="" ;;
esac
if [[ -z "$mapped" && "$named" == "none" && "${2:-0}" == "1" ]]; then
die "Archive '$archive' has unrecognized content; refusing to treat it as a tar stream"
fi
if [[ -n "$mapped" && "$mapped" != "$named" ]]; then
log "magic: $archive content detected as '$actual'; overriding extension-derived '$named'"
printf '%s\n' "$mapped"
else
printf '%s\n' "$named"
fi
}
# List all member names of a tar archive (one per line) into $2.
tar_list_entries() {
local archive="$1" dest="$2" compression="${3:-none}"
if ! tar_decompress_stream "$archive" "$compression" |
tar --quoting-style=literal --show-transformed-names -tf - >"$dest" 2>&1; then
cat "$dest" >&2
rm -f "$dest"
die "Failed to list tar entries for $archive"
fi
}
# Write the helper script used with tar --to-command: hashes each member's
# content (SHA-256 + byte count), appends "hash<TAB>path<TAB>size" to
# $TMP_MANIFEST and echoes "hash path size" to stdout for live progress.
# Set TAR_CMD_VERBOSE=1 to also log "processing: <path>" to stderr.
write_tar_checksum_command() {
local dest="$1"
cat >"$dest" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
f="${TAR_FILENAME:-}"
# TAR_FILETYPE is exported by GNU tar's --to-command: 'f' = regular file.
# Symlinks/hardlinks/directories carry no member data; drain stdin anyway.
if [[ -n "${TAR_FILETYPE:-}" && "${TAR_FILETYPE:-}" != "f" ]]; then
cat >/dev/null 2>&1 || true
exit 0
fi
if [[ -z "$f" || "$f" == */ ]]; then
cat >/dev/null 2>&1 || true
exit 0
fi
f="${f#./}"
# Escape control chars so each member stays a single parseable manifest record.
f="${f//\\/\\\\}"
f="${f//$'\n'/\\n}"
f="${f//$'\t'/\\t}"
f="${f//$'\r'/\\r}"
if [[ -n "${TAR_CMD_VERBOSE:-}" ]]; then
printf 'processing: %s\n' "$f" >&2
fi
hash="$(sha256sum | awk '{print $1}')"
size="${TAR_SIZE:-0}"
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 "$dest"
}
# Hash every regular file inside a tar archive in a single decompression pass.
# Appends sorted "hash path size" lines to $3. Progress lines stay on stdout.
# Non-regular members (dirs, links) are skipped by the --to-command helper.
write_tar_manifest() {
local archive="$1" compression="${2:-none}" dest="$3"
[[ -z "$dest" ]] && return 0
local tmp_command tmp_manifest tmp_extract
tmp_command="$(mktemp)"
tmp_manifest="$(mktemp)"
tmp_extract="$(mktemp -d)"
write_tar_checksum_command "$tmp_command"
# -C tmp_extract keeps directory/link members (which --to-command does
# not consume) off the caller's working directory.
local rc=0
TMP_MANIFEST="$tmp_manifest" \
tar -C "$tmp_extract" --to-command="$tmp_command" -xf - \
< <(tar_decompress_stream "$archive" "$compression") || rc=$?
LC_ALL=C sort -t $'\t' -k2,2 "$tmp_manifest" |
awk -F $'\t' '{printf "%s %s %s\n", $1, $2, $3}' >>"$dest"
rm -f "$tmp_command" "$tmp_manifest"
rm -rf -- "$tmp_extract"
return "$rc"
}