Last active
September 7, 2022 13:55
-
-
Save keks24/30af7806a3704401dd7adfccf52ed527 to your computer and use it in GitHub Desktop.
Generate and check file checksums via "BLAKE2" in parallel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################# | |
# Copyright 2020-2022 Ramon Fischer # | |
# # | |
# Licensed under the Apache License, Version 2.0 (the "License"); # | |
# you may not use this file except in compliance with the License. # | |
# You may obtain a copy of the License at # | |
# # | |
# http://www.apache.org/licenses/LICENSE-2.0 # | |
# # | |
# Unless required by applicable law or agreed to in writing, software # | |
# distributed under the License is distributed on an "AS IS" BASIS, # | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # | |
# See the License for the specific language governing permissions and # | |
# limitations under the License. # | |
############################################################################# | |
## generate and check file checksums via "blake2" in parallel | |
gcheck() | |
{ | |
local checksum_file="./checksums.b2" | |
local available_processors=$(nproc --all) | |
local max_arguments="1" | |
local overwrite_checksum_file | |
local file_list | |
if [[ -f "${checksum_file}" ]] | |
then | |
read $'overwrite_checksum_file?\e[01;31mThe file: '"'${checksum_file}'"$' was found. Do you really want to overwrite it? (y/N): \e[0m' >&2 | |
if [[ "${overwrite_checksum_file:-n}" =~ [nN] ]] | |
then | |
return 1 | |
fi | |
fi | |
file_list=$(find "." \ | |
-type f \ | |
-not \ | |
-name "${checksum_file/\.\//}" \ | |
-print0) | |
# the command "printf" is important here to keep a null-character-separated string. | |
# using "here-string" ("<<<") would add a trailing newline. | |
xargs \ | |
--null \ | |
--max-procs="${available_processors}" \ | |
--max-args="${max_arguments}" \ | |
b2sum --zero > "${checksum_file}" < <(printf "%s" "${file_list}") | |
} | |
ccheck() | |
{ | |
local checksum_file="./checksums.b2" | |
local available_processors=$(nproc --all --ignore="1") | |
if [[ ! -f "${checksum_file}" ]] | |
then | |
echo -e "\e[01;31mCould not find file: '${checksum_file}' or is not a file.\e[0m" >&2 | |
return 1 | |
else | |
# the parameter "--replace" and a subshell ("sh") must be used here! | |
xargs \ | |
--null \ | |
--arg-file="${checksum_file}" \ | |
--max-procs="${available_processors}" \ | |
--replace="{}" \ | |
sh -c \ | |
"b2sum --check --quiet <<< '{}'" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment