Skip to content

Commit dd785bc

Browse files
committed
performance optimization for 'update' and 'create'
1 parent 8a62818 commit dd785bc

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

bcfv

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ BASEDIR=
1919
OVERWRITE=
2020
TOUCH=
2121
DOTDIRS=
22-
ALGOS_RE='.*\.\(b3\|md5\|sha256\|sha512\)$'
2322

2423
if [ -z "${CHECKSUM}" ]; then
2524
CHECKSUM=".checksum"
@@ -76,6 +75,33 @@ list_files() {
7675
((${#DIR_FILES[@]})) && printf '%s\n' "${DIR_FILES[@]}"
7776
}
7877

78+
collect_dir_files() {
79+
local file
80+
DIR_FILES=()
81+
shopt -s dotglob nullglob
82+
for file in *; do
83+
[[ -f "${file}" && ! -L "${file}" ]] || continue
84+
case "${file,,}" in
85+
*.b3 | *.md5 | *.sha256 | *.sha512) continue ;;
86+
esac
87+
[[ "${file}" == "${CHECKSUM}.tmp"* ]] && continue
88+
DIR_FILES+=("${file}")
89+
done
90+
shopt -u dotglob nullglob
91+
}
92+
93+
# Hash directly for typical directories; retain xargs batching for large ones.
94+
hash_dir_files() {
95+
local alg="${1}"
96+
if ((${#DIR_FILES[@]} == 0)); then
97+
return
98+
elif ((${#DIR_FILES[@]} <= 512)); then
99+
"${alg}sum" -- "${DIR_FILES[@]}"
100+
else
101+
list_files | xargs -r -d "\n" "${alg}sum" --
102+
fi
103+
}
104+
79105
# usage: list_missing_files CHECKSUM_FILE
80106
# list files not in CHECKSUM_FILE in sorted order
81107
list_missing_files() {
@@ -198,7 +224,7 @@ for dir in "${DIRS[@]}"; do
198224
continue
199225
fi
200226
pushd "${d}" >/dev/null || continue
201-
mapfile -t DIR_FILES < <(find . -maxdepth 1 -type f ! -iregex "${ALGOS_RE}" ! -name "${CHECKSUM}.tmp*" -printf '%P\n' | sort)
227+
collect_dir_files
202228
for alg in "${ALGO[@]}"; do
203229
CHECKSUM_FILE="${CHECKSUM}.${alg}"
204230
CHECKSUM_TMP="${CHECKSUM}.tmp"
@@ -280,11 +306,10 @@ for dir in "${DIRS[@]}"; do
280306

281307
elif [ "${MODE}" = "create" ]; then
282308
# echo "# Directory: ${d}"
283-
touch "${CHECKSUM_TMP}"
284309
if [ -n "${QUIET}" ]; then
285-
list_files | xargs -r -d "\n" "${alg}sum" -- >"${CHECKSUM_TMP}" # 2>&1
310+
hash_dir_files "${alg}" >"${CHECKSUM_TMP}" # 2>&1
286311
else
287-
list_files | xargs -r -d "\n" "${alg}sum" -- | tee "${CHECKSUM_TMP}" | print_path_chksum "${d}"
312+
hash_dir_files "${alg}" | tee "${CHECKSUM_TMP}" | print_path_chksum "${d}"
288313
fi
289314
else
290315
error "Unknown mode ${MODE}"

0 commit comments

Comments
 (0)