Checksum : Use _iterate_targets.
This commit is contained in:
parent
0be0396232
commit
47a30351d1
|
@ -208,7 +208,7 @@ function unarchive() {
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
else
|
else
|
||||||
_error "Validation failed!"
|
_error "Failed."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,45 +4,20 @@
|
||||||
# Usage: checksum_create [FILES]
|
# Usage: checksum_create [FILES]
|
||||||
function checksum_create() {
|
function checksum_create() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=("${@}")
|
local targets=(${@})
|
||||||
local count=0
|
[[ "${targets}" = "" ]] && targets=($(_ls_file))
|
||||||
local total=${#}
|
|
||||||
local failed=0
|
|
||||||
|
|
||||||
# All files by default.
|
process() {
|
||||||
if [[ "${targets}" = "" ]]; then
|
|
||||||
targets=($(ls --classify | grep -v /\$))
|
|
||||||
total=${#targets[@]}
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Iterate each file.
|
|
||||||
for target in "${targets[@]}"; do
|
|
||||||
local hashfile=".${target#./}.sha1"
|
local hashfile=".${target#./}.sha1"
|
||||||
|
|
||||||
# Increment count.
|
|
||||||
((count++))
|
|
||||||
|
|
||||||
# Status info.
|
|
||||||
local status="[${count}/${total}] ${target}"
|
|
||||||
echo -e "${color_bwhite}${status}${color_default}"
|
|
||||||
|
|
||||||
# Skip if hash exists.
|
# Skip if hash exists.
|
||||||
[[ -f "${hashfile}" ]] && continue
|
[[ -f "${hashfile}" ]] && return 0
|
||||||
|
|
||||||
# Calculate hash.
|
# Calculate hash.
|
||||||
pv "${target}" | sha1sum > ${hashfile}
|
pv ${target} | sha1sum > ${hashfile}
|
||||||
|
}
|
||||||
|
|
||||||
# Report success.
|
_iterate_targets process ${targets[@]}
|
||||||
if [[ ${?} != 0 ]]; then
|
|
||||||
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
||||||
((failed++))
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [[ ${failed} != 0 ]]; then
|
|
||||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check stored values against actual files.
|
# Check stored values against actual files.
|
||||||
|
@ -50,51 +25,26 @@ function checksum_create() {
|
||||||
# Usage: checksum_check [FILES]
|
# Usage: checksum_check [FILES]
|
||||||
function checksum_check() {
|
function checksum_check() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=("${@}")
|
local targets=(${@})
|
||||||
local count=0
|
[[ "${targets}" = "" ]] && targets=($(_ls_file))
|
||||||
local total=${#}
|
|
||||||
local failed=0
|
|
||||||
local skipped=0
|
|
||||||
|
|
||||||
# All files by default.
|
process() {
|
||||||
if [[ "${targets}" = "" ]]; then
|
|
||||||
targets=($(ls --classify | grep -v /\$))
|
|
||||||
total=${#targets[@]}
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Iterate each file.
|
|
||||||
for target in "${targets[@]}"; do
|
|
||||||
local hashfile=".${target#./}.sha1"
|
local hashfile=".${target#./}.sha1"
|
||||||
|
|
||||||
# Increment count.
|
|
||||||
((count++))
|
|
||||||
|
|
||||||
# Status info.
|
|
||||||
local status="[${count}/${total}] ${target}"
|
|
||||||
echo -e "${color_bwhite}${status}${color_default}"
|
|
||||||
|
|
||||||
# Skip if hash doesn't exist.
|
# Skip if hash doesn't exist.
|
||||||
[[ -f "${hashfile}" ]] || { ((skipped++)); continue; }
|
[[ -f "${hashfile}" ]] || { ((skipped++)); _warn "No hash found."; return 0; }
|
||||||
|
|
||||||
# Calculate hash.
|
# Calculate hash.
|
||||||
local stored=$(cat "${hashfile}" | cut -d\ -f1)
|
local stored=$(cat "${hashfile}" | cut -d\ -f1)
|
||||||
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
|
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
|
||||||
|
|
||||||
if [[ "${stored}" != "${actual}" ]]; then
|
if [[ "${stored}" != "${actual}" ]]; then
|
||||||
echo -e "${color_bred}${status}: Failed.${color_default}"
|
_error "Failed."
|
||||||
((failed++))
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [[ ${skipped} != 0 ]]; then
|
|
||||||
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
|
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [[ ${failed} != 0 ]]; then
|
_iterate_targets process ${targets[@]}
|
||||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calculate hashes for all files recursively and store in a file called `checksum.sha1`.
|
# Calculate hashes for all files recursively and store in a file called `checksum.sha1`.
|
||||||
|
|
|
@ -85,7 +85,7 @@ function name_hash_check() {
|
||||||
|
|
||||||
# compare hashes.
|
# compare hashes.
|
||||||
if [[ "${stored}" != "${actual}" ]]; then
|
if [[ "${stored}" != "${actual}" ]]; then
|
||||||
_error "Validation failed!"
|
_error "Failed."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ function _parse_ints() {
|
||||||
|
|
||||||
# Function-wrapper to iterate with specified function with provided files.
|
# Function-wrapper to iterate with specified function with provided files.
|
||||||
# By default Iterates on all non-hidden files and directories.
|
# By default Iterates on all non-hidden files and directories.
|
||||||
# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, status - status line (not recommended to use).
|
# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, skipped - count of skipped files, status - status line (not recommended to use).
|
||||||
# Usage: _iterate_targets <FUNCTION> [FILES]
|
# Usage: _iterate_targets <FUNCTION> [FILES]
|
||||||
function _iterate_targets() {
|
function _iterate_targets() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
|
@ -19,6 +19,7 @@ function _iterate_targets() {
|
||||||
local total=${#targets[@]}
|
local total=${#targets[@]}
|
||||||
local count=0
|
local count=0
|
||||||
local failed=0
|
local failed=0
|
||||||
|
local skipped=0
|
||||||
local code=0
|
local code=0
|
||||||
|
|
||||||
# set dafult value to target all supported archives.
|
# set dafult value to target all supported archives.
|
||||||
|
@ -49,18 +50,30 @@ function _iterate_targets() {
|
||||||
[[ "${count}" = "${total}" ]] || echo
|
[[ "${count}" = "${total}" ]] || echo
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Show skipped.
|
||||||
|
if [[ ${skipped} != 0 ]]; then
|
||||||
|
echo
|
||||||
|
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Show error.
|
# Show error.
|
||||||
if [[ ${failed} != 0 ]]; then
|
if [[ ${failed} != 0 ]]; then
|
||||||
echo
|
[[ "${skipped}" = 0 ]] && echo
|
||||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||||
false
|
false
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Report error to stdout.
|
# Report an error to stdout.
|
||||||
# Always returns code 1.
|
# Always returns code 1.
|
||||||
# If no message specified, prints "An error has occured".
|
# Usage: _error <MESSAGE>
|
||||||
# Usage: _error [MESSAGE]
|
|
||||||
function _error() {
|
function _error() {
|
||||||
echo -e "${color_bred}${*}${color_default}"
|
echo -e "${color_bred}${*}${color_default}"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Report a warning to stdout.
|
||||||
|
# Usage: _warn <MESSAGE>
|
||||||
|
function _warn() {
|
||||||
|
echo -e "${color_byellow}${*}${color_default}"
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue