From 85dd586802e61b0db3be4c3d592fdb634f41aabc Mon Sep 17 00:00:00 2001 From: home Date: Wed, 22 Nov 2023 14:51:46 +0300 Subject: [PATCH] checksum_update : recreate hashes. --- .README.md | 5 ++-- .config/bash/module/checksum.sh | 47 +++++++++++++-------------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/.README.md b/.README.md index 30514b6..8707c42 100644 --- a/.README.md +++ b/.README.md @@ -359,8 +359,9 @@ Command|Description Command|Description ---|--- -`checksum new`|Create checksums recursively. -`checksum check`|Check previously created checksums. +`checksum_create`|Create checksums for files in current directory. +`checksum_check`|Check previously created checksums. +`checksum_update`|Check old hashes and create new ones for all the files. ## Chmod. diff --git a/.config/bash/module/checksum.sh b/.config/bash/module/checksum.sh index c039ce3..154f70b 100644 --- a/.config/bash/module/checksum.sh +++ b/.config/bash/module/checksum.sh @@ -1,37 +1,28 @@ -# create or check checksums for all files in current directory. -# usage: checksum -# actions: "new", "check". -checksum() +checksum_create() { - local action="$1" # what to do with checksum. - local file="checksum.sha1" # file name to store checksums. - - if [[ "$action" =~ ^(n|new)$ ]]; then - # create checksums for all files. - find -type f | parallel sha1sum {} >> "$file" - - # remove checksum file from resulting hashes. - sed -i "/$file/d" "$file" - - return 0 + if [[ -f .checksum ]]; then + echo -e "${color_bred}Checksum already exists.${color_default}" + return 1 fi - if [[ "$action" =~ ^(c|check)$ ]]; then - # check checsums for all files. - sha1sum --quiet -c ./checksum.sha1 - - return 0 - fi + sha1sum * > .checksum 2> /dev/null - # error on wrong action. - echo "supported actions: new (n), check (c)." - return 2 + sed -i "/\.checksum/d" .checksum } -# autocomplete. -_checksum() +checksum_check() { - _autocomplete_first "{new,check}" + sha1sum --quiet -c .checksum } -complete -F _checksum checksum +checksum_update() +{ + if [[ ! -f .checksum ]]; then + echo -e "${color_bred}Checksum doesn't exists.${color_default}" + return 1 + fi + + sha1sum --quiet -c .checksum && rm .checksum && sha1sum * > .checksum 2> /dev/null + + true +}