individual checksum files

This commit is contained in:
q
2025-06-19 10:33:04 +03:00
parent 4fe95c765f
commit 159161e7c5

View File

@@ -9,6 +9,7 @@ Usage:
-c check -c check
-t hash-type defaults to sha256. choose from md5, sha1, sha256, sha512 etc.. -t hash-type defaults to sha256. choose from md5, sha1, sha256, sha512 etc..
-f list_name is [type]sums.txt by default -f list_name is [type]sums.txt by default
-i individual [filename].[hash] files, instead of -f list of files.
If files to check omitted, list files in current folder If files to check omitted, list files in current folder
Check files with "sha256sum -c sha256sums.txt", or passing -c to this script. Check files with "sha256sum -c sha256sums.txt", or passing -c to this script.
@@ -30,6 +31,7 @@ function valid_type() {
recursive=0 recursive=0
check=0 check=0
hashtype=sha256 hashtype=sha256
individual=0
paths=() paths=()
for ((i=1; i<=${#@}; i++)) { for ((i=1; i<=${#@}; i++)) {
@@ -38,6 +40,7 @@ for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "--help" ]] && helpexit [[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-r" ]] && { recursive=1; continue; } [[ "${!i}" = "-r" ]] && { recursive=1; continue; }
[[ "${!i}" = "-c" ]] && { check=1; continue; } [[ "${!i}" = "-c" ]] && { check=1; continue; }
[[ "${!i}" = "-i" ]] && { individual=1; continue; }
[[ "${!i}" = "-t" ]] && { hashtype="${!j}"; i=$(( $i + 1 )); continue; } [[ "${!i}" = "-t" ]] && { hashtype="${!j}"; i=$(( $i + 1 )); continue; }
[[ "${!i}" = "-f" ]] && { list_name="${!j}"; i=$(( $i + 1 )); continue; } [[ "${!i}" = "-f" ]] && { list_name="${!j}"; i=$(( $i + 1 )); continue; }
paths+=("${!i}") paths+=("${!i}")
@@ -67,9 +70,23 @@ if [[ ${#paths[@]} -eq 0 ]]; then
fi fi
which pv &>/dev/null && PVBIN=$( which pv ) which pv &>/dev/null && PVBIN=$( which pv )
set -e set -e
if [[ $individual = 0 ]]; then
echo "Updating: $list_name" echo "Updating: $list_name"
test -f "$list_name" || touch "$list_name" test -f "$list_name" || touch "$list_name"
fi
updates_done=0 updates_done=0
_add_single() {
size=$( stat -c %s "$1" )
if [[ $size -gt 100000000 ]] && [[ -n "$PVBIN" ]]; then
# Use pv is size > 100MB
touch "$2"
sum=$( $PVBIN "$1" | $SUMBIN | awk '{ print $1 }' )
printf "%s %s\n" "$sum" "$1" >> "$2"
else
$SUMBIN "$1" >> "$2"
fi
}
_update() { _update() {
if [ "$1" = "$list_name" ]; then if [ "$1" = "$list_name" ]; then
return return
@@ -82,19 +99,24 @@ _update() {
echo "$1 is a directory" echo "$1 is a directory"
return return
fi fi
# individual checksum files
if [[ $individual = 1 ]]; then
if [[ "$1" = *.$hashtype ]]; then
return
fi
if [[ -e "$1.$hashtype" ]]; then
return
fi
_add_single "$1" "$1.$hashtype"
return
fi
# sum list file
if test -f "$1"; then if test -f "$1"; then
if sed 's,^[^ ]\+ ,,' "$list_name" | grep -qFx "$1"; then if sed 's,^[^ ]\+ ,,' "$list_name" | grep -qFx "$1"; then
echo "Exists: $1" echo "Exists: $1"
else else
echo "New: $1" echo "New: $1"
size=$( stat -c %s "$1" ) _add_single "$1" "$list_name"
if [[ $size -gt 100000000 ]] && [[ -n "$PVBIN" ]]; then
# Use pv is size > 100MB
sum=$( $PVBIN "$1" | $SUMBIN | awk '{ print $1 }' )
printf "%s %s\n" "$sum" "$1" >> "$list_name"
else
$SUMBIN "$1" >> "$list_name"
fi
updates_done=1 updates_done=1
fi fi
fi fi