From 159161e7c5a5ea5694d0e75c4ce7d8a1a3402f0a Mon Sep 17 00:00:00 2001 From: q Date: Thu, 19 Jun 2025 10:33:04 +0300 Subject: [PATCH] individual checksum files --- files/hash-update | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/files/hash-update b/files/hash-update index 06a26ba..aa23b2d 100755 --- a/files/hash-update +++ b/files/hash-update @@ -9,6 +9,7 @@ Usage: -c check -t hash-type defaults to sha256. choose from md5, sha1, sha256, sha512 etc.. -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 Check files with "sha256sum -c sha256sums.txt", or passing -c to this script. @@ -30,6 +31,7 @@ function valid_type() { recursive=0 check=0 hashtype=sha256 +individual=0 paths=() for ((i=1; i<=${#@}; i++)) { @@ -38,6 +40,7 @@ for ((i=1; i<=${#@}; i++)) { [[ "${!i}" = "--help" ]] && helpexit [[ "${!i}" = "-r" ]] && { recursive=1; continue; } [[ "${!i}" = "-c" ]] && { check=1; continue; } + [[ "${!i}" = "-i" ]] && { individual=1; continue; } [[ "${!i}" = "-t" ]] && { hashtype="${!j}"; i=$(( $i + 1 )); continue; } [[ "${!i}" = "-f" ]] && { list_name="${!j}"; i=$(( $i + 1 )); continue; } paths+=("${!i}") @@ -67,9 +70,23 @@ if [[ ${#paths[@]} -eq 0 ]]; then fi which pv &>/dev/null && PVBIN=$( which pv ) set -e -echo "Updating: $list_name" -test -f "$list_name" || touch "$list_name" +if [[ $individual = 0 ]]; then + echo "Updating: $list_name" + test -f "$list_name" || touch "$list_name" +fi 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() { if [ "$1" = "$list_name" ]; then return @@ -82,19 +99,24 @@ _update() { echo "$1 is a directory" return 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 sed 's,^[^ ]\+ ,,' "$list_name" | grep -qFx "$1"; then echo "Exists: $1" else echo "New: $1" - size=$( stat -c %s "$1" ) - 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 + _add_single "$1" "$list_name" updates_done=1 fi fi