-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmd5dirs
executable file
·284 lines (222 loc) · 7.21 KB
/
md5dirs
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
#!/bin/bash
# Do md5 files for all subdirectories
ver=0.3.6
me=$(basename "$0")
set -o pipefail # keep errors from pipes
#### DEFAULTS
# Without the .md5 extension, which will be added automatically
md5_filename=checksums
#### FUNCTIONS
usage() {
cat <<EOF
$0 Version $ver
Usage: $me [options] DIRECTORY
This script goes into the directory given as argument,
and into any of it's subdirectories and writes separate
.md5 checksums files for each directory.
Options:
-f Filename : Filename for checksums (without extension. eg. "-f cs-md5". Default: ${md5_filename%%.md5}.md5)
-n : No action (dry_run) for quick testing. Don't actually calculate md5
-d : Debug (can be repeated)
-v : Verbose (can be repeated)
-V : show version and exit
-y : Yes, do it without asking first
-h, -? : This help
EOF
}
die() {
echo "$@" 1>&2
exit 1;
}
warn() {
er="$@"
echo "$er" 1>&2
errors+=("$er")
}
confirm() {
while true; do
read -p "$* [y/n]: " yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) echo "Aborted" ; return 1 ;;
esac
done
}
#### PARSE ARGUMENTS
while getopts "f:ndvVyh?" opt; do
case $opt in
f) md5_filename=$OPTARG;;
n) dry_run=1;;
d) (( debug+=1 ));;
v) (( verbose+=1 ));;
V) echo "$0 version $ver"; exit;;
y) yes=1;;
h|?) usage; exit;;
*) usage; warn "WARNING: ignoring unknow option '$opt'";;
esac
done
shift $((OPTIND -1))
dir="$1"
if [ -z "$dir" ]; then
usage
die "Error: missing folder argument. Specify folder to process."
fi
if [ ! -d "$dir" ]; then
usage
die "Directory '$dir' not found"
fi
#### OTHER INIT
(( debug+verbose )) && echo "$0 version $ver"
md5_filename="${md5_filename%%.md5}.md5"
(( debug+verbose )) && echo "md5_filename = $md5_filename"
# Check OS to select md5 command
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
is_linux=1
md5cmd="md5sum"
size_opts="-c %s"
elif [[ "$OSTYPE" == "darwin"* ]]; then
is_mac=1
md5cmd="md5 -r"
size_opts="-f %z"
else
die "Error: unknown OS '$OSTYPE'. Don't know correct md5 command."
fi
(( debug )) && echo "DEBUG: md5 command set to '$md5cmd'"
#### START
total_size=0
total_files=0
start_time=$(date "+%F_%T")
(( debug )) && echo "DEBUG: dir='$dir'" >&2
# go to base directory after saving where we started
start_dir="$PWD"
cd "$dir"
(( debug )) && echo "DEBUG: cd to '$dir'" >&2
# if we're in a symlink cd to the real path
if [ ! "$dir" = "$(pwd -P)" ]; then
dir="$(pwd -P)"
(( debug )) && echo "DEBUG: \$dir changed to real path '$dir'" >&2
cd "$dir"
(( debug )) && echo "DEBUG: Now in '$PWD'" >&2
fi
if [ "$PWD" = "/" ]; then
die "Refusing to do it on system root '$PWD'"
fi
if ((yes)); then
echo "Warning: -y yes option used. Will not ask for confirmation."
fi
# Find all folders to process
declare -a subdirs=()
declare -a skipped=()
declare -a dcpdirs=()
declare -a wanted=()
# 1. find all non-hidden subdirectories (not if the name begins with "." like ".Trashes", ".Spotlight-V100", etc.)
while IFS= read -r; do
#(( debug>1 )) && echo "subdir: '$REPLY'"
REPLY="${REPLY/\/\///}" # replace double "/" : some//path = some/path
#(( debug>1 )) && echo "subdir> '$REPLY'"
REPLY="${REPLY##./}" # remove leading "./"
#(( debug>1 )) && echo "subdir> '$REPLY'"
subdirs+=("$PWD/$REPLY")
done < <(find . -type d -not -name ".*" | LC_ALL=C sort)
(( debug > 1 )) && printf "DEBUG: subdirs: %s\n" "${subdirs[@]}"
# 2. find DCP dirs to be excluded
if (( is_mac )); then
while IFS= read -r; do
REPLY="${REPLY/\/\///}" # replace double "/" : some//path = some/path
dcpdirs+=("$REPLY")
done < <(find -E . -type f -regex ".*/ASSETMAP(\.xml)?" -execdir sh -c 'echo $PWD' \; | LC_ALL=C sort)
elif (( is_linux )); then
while IFS= read -r; do
REPLY="${REPLY/\/\///}" # replace double "/" : some//path = some/path
REPLY="${REPLY##./}" # remove leading "./"
dcpdirs+=("$PWD/$REPLY")
done < <(find . -type f -regextype posix-extended -regex ".*/ASSETMAP(\.xml)?" -printf "%h\n" | LC_ALL=C sort)
fi
(( debug )) && printf "DEBUG: dcpdirs: %s\n" "${dcpdirs[@]}"
# 3. count files and if there are any, add dir to wanted, and calculate size
echo "Counting files and sizes to process ..."
for d in "$dir" "${subdirs[@]}"; do # include "$dir" itself, not only it's subdirs
(( debug > 1)) && echo "DEBUG: checking dir '$d'"
# skip if it's a DCP
isdcp=0
for dcp in "${dcpdirs[@]}"; do
(( debug )) && printf "%s" "DEBUG: Is '$d' in DCP dir '$dcp'? "
if [[ "$d" =~ ^"$dcp"* ]]; then
(( debug )) && echo "yes"
echo "Skipping DCP dir '$d'."
skipped+=("$d")
isdcp=1
break
else
(( debug )) && echo "no"
fi
done;
(( isdcp )) && continue
found_md5="$(find "$d" -maxdepth 1 -name "$md5_filename" -print -quit)"
if [ -n "$found_md5" ]; then
#fixme: warning not shown??
echo "Skipping '$d'. It already has '$found_md5'"
skipped+=("$d")
continue
fi
files_here=0
while IFS= read -r ; do
(( total_files += 1 ))
(( files_here += 1 ))
size=$(stat $size_opts "$REPLY")
(( total_size += size ))
done < <(find "$d" -maxdepth 1 -type f -not -name ".DS_Store" -not -name "._*" -not -name "*.md5")
if (( files_here )); then
wanted+=("$d")
else
skipped+=("$d")
fi
done
total_size_mb=$(( total_size / 1000000 ))
echo "Found ${#wanted[@]} folders to process:"
printf " * %s\n" "${wanted[@]}"
if [ "${#skipped[@]}" -gt 0 ]; then
echo "Skipping ${#skipped[@]} folder(s) :"
printf " * %s\n" "${skipped[@]}"
fi
if [ "${#wanted[*]}" = 0 ]; then
echo "Nothing to do. Exiting."
exit 0
fi
echo "total files : $total_files"
echo "total size : $total_size_mb MB. ($total_size Bytes)"
# Do you want to start?
# if not (dry_run or yes), ask confirmation:
if ! (( dry_run + yes )); then
confirm "Start processing $total_size_mb MB." \
|| { cd "$start_dir"; exit 0; }
fi
for d in "${wanted[@]}"; do
echo "== Going to write $d/$md5_filename"
cd "$d"
(( debug )) && echo "DEBUG: processing $PWD"
find . -maxdepth 1 -type f -not -name "$md5_filename" -not -name '.DS_Store' -not -name "._*" -print0 \
| LC_ALL=C sort -z \
| while IFS= read -rd '' f; do
if (( dry_run )); then
(( debug )) && echo "DEBUG: (-n) would do: $md5cmd \"$f\" | tee -a \"$md5_filename\""
(( verbose-debug > 0 )) && echo "$f"
continue
fi
$md5cmd "$f" | tee -a "$md5_filename"
rv=$?
[ "$rv" -eq 0 ] || die "ERROR $rv with '$md5cmd \"$f\" | tee -a \"$md5_filename\"'"
done
(( debug )) && echo "DEBUG: Done $d/$md5_filename"
echo
cd "$dir"
done
end_time=$(date "+%F_%T")
echo "Done."
echo "Started at $start_time"
echo "Finished at $end_time"
cd "$start_dir"
if (( ${#errors[@]} )); then
echo "WARNING: There were errors:"
printf "* %s\n" "${errors[@]}"
fi