-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckdir
More file actions
executable file
·363 lines (311 loc) · 9.4 KB
/
Copy pathcheckdir
File metadata and controls
executable file
·363 lines (311 loc) · 9.4 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
#!/bin/bash
# Author: Christopher Lovejoy <c@lovejoy.io>
SCRIPT_NAME=checkdir
SCRIPT_VERSION=0.3.2
SCRIPT_MODIFIED=2026-09-10
## PLUMBING:
yell() { echo "$@" >&2; }
die() { yell "$@"; exit 111; }
must() { "$@" || die "cannot $*"; }
loglevel() { [[ $opt_verbose -ge $1 ]]; }
feedback() { loglevel $1 || return; shift; yell "$@"; }
toLowerCase() {
#perl -C -lne 'print uc';
tr '[:upper:]' '[:lower:]';
}
toUpperCase() {
#perl -C -lne 'print uc';
tr '[:lower:]' '[:upper:]';
}
## CONST/CONFIGURATION:
if [[ -z "$TMPDIR" || ! -d "$TMPDIR" ]]; then
if [[ -d '/dev/shm' ]]; then
export TMPDIR='/dev/shm';
else
export TMPDIR="$PWD";
fi
fi
## DOCUMENTATION:
SCRIPT_USAGE="${0##*/} [OPTIONS] [SUMFILE…]"
SCRIPT_OPTIONS=(
"--help, help, -h\tShow this help."
"--version\tShow the script version."
"-a ALG\tSet encryption algorithm: 'sha256', 'b2', 'sha256e' (alias 'annex')."
"-e\t\tOnly check if files in SUMFILE exist, and output missing filenames."
"-l\t\tOnly output the SUMFILE path found/selected."
"-I END\t(Only with -a sha256e) Ignore END and anything after it on each line. If omitted, this option obeys a header describing such a separator."
"-v\t\tShow more verbose output."
)
SCRIPT_DESCRIPTION_LINES=(
"If no SUMFILE is specified, .*SHA256SUM* and *SHA256SUM* is globbed in the current directory, and the newest one is used."
)
## METHODS:
showHelp() {
echo "USAGE: $SCRIPT_USAGE";
echo "";
echo "OPTIONS:";
local opt;
for opt in "${SCRIPT_OPTIONS[@]}"; do
echo -e " ${opt}"
done
echo "";
echo "DESCRIPTION:";
for line in "${SCRIPT_DESCRIPTION_LINES[@]}"; do
echo " ${line}"
done
echo
echo "${SCRIPT_NAME} version ${SCRIPT_VERSION} (${SCRIPT_MODIFIED})"
}
confirm() {
local prompt="$1";
local answer="$2";
local REPLY;
local defaultYes=true
if [[ $answer =~ [Nn] ]]; then
defaultYes=false
options='y/N'
else
options='Y/n'
fi
read -n1 -p "${prompt} (${options}) ";
if [[ $defaultYes == true ]]; then
[[ -z $REPLY || $REPLY =~ [Yy] ]];
return
else
[[ $REPLY =~ [Yy] ]];
return
fi
}
if ! command -v sha256sum &>/dev/null; then
if command -v shasum &>/dev/null; then
sha256sum() { shasum -a 256 "$@"; }
fi
fi
initializeSumCommand() {
# NOTE: Each command MUST support the '--check SUMFILE' option.
opt_algorithm="$(echo "$opt_algorithm" | toLowerCase)";
case $opt_algorithm in
sha256e|*annex) opt_algorithm=sha256e;
if ! command -v sha256sum &>/dev/null; then
die "Missing checksum software: sha256sum or shasum.";
fi
sumCommand=(checksumSHA256E);
;;
sha*) sumCommand=(sha256sum);
if ! command -v sha256sum &>/dev/null; then
die "Missing checksum software: sha256sum or shasum.";
fi
;;
b2*|blake*) sumCommand=(b2sum);;
*) die "Unknown sum algorithm '${opt_algorithm}'.";;
esac
if command -v "${sumCommand[0]}" &>/dev/null; then
feedback 1 "Using $(command -v "${sumCommand[0]}")";
else
die "Missing checksum software: ${sumCommand[0]}.";
fi
}
getSumFile() {
local match;
match="$(ls -1t .*SUM* 2>/dev/null)" \
|| match="$(ls -t *SUM* 2>/dev/null)" \
|| return $?;
echo -en "$match" | head -1
}
countFilesInSumFile() {
grep -Ecv '^\s*#' "$1";
}
describeIgnoreAfter() {
local sumFile="$1"
local pattern="^\\s*#\\s*[^:]*(separat|metadata|ignore)[^:]*: ?'?(.+)'?\$"
local header
header="$(head -10 "$sumFile" | grep -i -P "$pattern")" || return
echo "$header" | perl -pe "s/$pattern/\$2/i";
}
describeSumFile() {
local sumFile="$1";
local fileCount="$2";
[[ -z $fileCount ]] && fileCount="$(countFilesInSumFile "$sumFile")";
local dirCount="$(grep -Ec '^[a-f0-9]+ .*/' "$sumFile")";
local description="${fileCount} files";
if [[ $dirCount -gt 0 ]]; then
description+="; descends into directories";
fi;
local metadataSeparator="$3"
if [[ -n $metadataSeparator ]]; then
description+="; metadata after ${metadataSeparator}";
fi;
echo -n "$description";
}
checksumSHA256E() {
local sumfile path basename size sum ext baseKey
local status # <enum:'OK','FAILED','REF'>
local MAX_KEY_LENGTH=300
while [[ $# -gt 0 ]]; do
case $1 in
--|--check) shift; continue;;
*) sumfile="$1";
esac
shift
done
local readOpts=(-r)
while read "${readOpts[@]}" key path; do
[[ $key =~ ^\s*# ]] && continue; # comment
if [[ ${key:0:1} == '*' ]]; then
status=REF
key="${key#\*}"
fi
if [[ -n $opt_ignoreAfter ]]; then
path="${path%%${opt_ignoreAfter}*}"
fi
if ! [[ -e "$path" ]]; then
status='FAILED open or read';
else
basename="$(basename "$path")"
size=($(du -L -b "$path")) || continue;
sum=($(sha256sum "$path")) || continue;
baseKey="SHA256E-s${size[0]}--${sum[0]}"
if [[ ${key:0:${#baseKey}} == $baseKey ]]; then
status=OK;
elif [[ $status == REF && $size -lt $MAX_KEY_LENGTH ]] \
&& grep -qF "$key" "$path"
then
status='REFerence to file not present'
else
status=FAILED;
fi
fi
echo "${path}: ${status}";
done <"$sumfile"
}
export -f checksumSHA256E
## Giving it a descriptive name, because (with option -e) it shows up in the "full" output to an
## interactive shell.
checkdir_doPathsInSumFileExist() {
local sumFile="$1"; shift;
local numTotal=0 numPresent=0 numMissing=0;
local exitStatus=0;
feedback 2 "About to read sumfile(${sumFile}) line by line in Bash.";
local hash path;
while read hash path; do
if [[ -z $hash || -z "$path" || $hash =~ ^[[:space:]]*# ]]; then
feedback 2 "Skipping commented line: ${hash} ${path}";
continue;
fi;
let numTotal++;
if [[ -e "$path" ]]; then
let numPresent++;
echo "EXISTS: ${path}";
else
let numMissing++;
exitStatus=1;
echo "MISSING: ${path}";
fi
done <"$sumFile"
if [[ $numMissing -gt 0 ]]; then
yell "Missing ${numMissing} files.";
fi
return $exitStatus;
}
main() {
local sumFile="$1";
if [[ -z "$sumFile" ]]; then
sumFile="$(getSumFile)" || die "No hash file to check against. Run sumdir to make one.";
fi
if [[ $sumFile =~ [bB]2 ]]; then
opt_algorithm=b2;
elif [[ $sumFile =~ SHA256E ]]; then
opt_algorithm=sha256e;
else
opt_algorithm=sha256;
fi
checkOutputCacheFile="${TMPDIR}/.checking.${sumFile##*/}.${RANDOM}.log";
local fileCount="$(countFilesInSumFile "$sumFile")";
local metadataSeparatorDescription
if [[ -z $opt_ignoreAfter ]]; then
metadataSeparatorDescription="$(describeIgnoreAfter "$sumFile")"
metadataSeparatorStatus=$?
if [[ $metadataSeparatorStatus -eq 0 ]]; then
case $metadataSeparatorDescription in
*\\t*|*Tab*|*tab*) opt_ignoreAfter=$'\t';;
*) opt_ignoreAfter="$metadataSeparatorDescription";;
esac
else
metadataSeparatorDescription=
fi
fi
#loglevel 1 && >&2 declare -p opt_ignoreAfter metadataSeparatorDescription metadataSeparatorStatus;
yell "Checking files against $sumFile ($(describeSumFile "$sumFile" "$fileCount" "$metadataSeparatorDescription"))";
## PREPARING EXECUTION:
checkCommand=();
if [[ $opt_check_exists_only != 'true' ]]; then
initializeSumCommand;
checkCommand+=("${sumCommand[@]}" --check);
else
checkCommand+=(checkdir_doPathsInSumFileExist);
fi
checkCommand+=("$sumFile")
## EXECUTION:
# WISH (B) 2024-07-07 parallelize file hash check
# DONE x 2024-07-07 (A) 2024-07-07 Show progress
local checkOutput="$("${checkCommand[@]}" | {
outputLineCount=0;
while read outputLine; do
let 'outputLineCount++';
echo "$outputLine";
>&2 echo -n $'\r'" $((100 * outputLineCount / fileCount))% (${outputLineCount} files checked) "; # space at the end to blank out long numbers
done;
>&2 echo;
};)";
checkStatus=$?; # FIXME Status of checkCommand obscured by reading outputLine and subsequent echos. uuid:14601836-19ee-4744-8e0c-cd40cea53393
## HANDLE OUTPUT:
local failedLines
failedLines="$(echo "$checkOutput" \
| tee "${checkOutputCacheFile}" \
| grep ': \(FAILED\|MISSING\)')"
failedMatchStatus=$?
local failedLineCount=$(echo "$failedLines" | wc -l)
if [[ $failedMatchStatus -eq 0 ]]; then
yell "$failedLines";
yell "";
yell "${failedLineCount} of ${fileCount} files failed checks ($((100 * (fileCount - failedLineCount) / fileCount))% OK)";
elif [[ $checkStatus != 0 ]]; then
yell "✗ Finished with problems.";
else
yell "✓ All OK ($fileCount files)";
fi
[[ -t 0 ]] \
&& confirm "See full output?" 'n' \
&& cat <(echo "### output from '${checkCommand[*]}':") "$checkOutputCacheFile" <(echo -e "\n### sum file data being checked:") "$sumFile" | less
[[ -e "$checkOutputCacheFile" ]] && rm "$checkOutputCacheFile"
return $checkStatus;
}
## EXECUTION:
opt_algorithm=sha256;
opt_check_exists_only=false;
opt_verbose=0;
opt_ignoreAfter=
sumCommand=();
sumFileArgs=();
while [[ $# -gt 0 ]]; do
case $1 in
--help|help|-h) showHelp; exit;;
-a) opt_algorithm="$2"; shift;;
-e) opt_check_exists_only=true;;
-I) opt_ignoreAfter+="$2"; shift;;
-l) getSumFile; exit;;
-v) let opt_verbose++;;
--version)
if loglevel 1; then
echo "$(basename "$0") version ${SCRIPT_VERSION} (${SCRIPT_MODIFIED})";
else
echo "${SCRIPT_VERSION}";
fi
exit;;
*) sumFileArgs+=("$1");;
esac
shift;
done
feedback 1 "Log level: $opt_verbose"
main "${sumFileArgs[@]}";
exit;