This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/checksum.sh

128 lines
2.6 KiB
Bash
Raw Normal View History

2023-11-23 00:13:51 +03:00
# Save file checksums.
2023-11-24 01:19:49 +03:00
# Usage: checksum_create [FILES]
2023-11-22 14:51:46 +03:00
checksum_create()
2023-08-08 16:24:15 +03:00
{
2023-11-24 01:19:49 +03:00
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
2023-11-23 00:13:51 +03:00
2023-11-24 01:19:49 +03:00
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
2023-08-08 16:24:15 +03:00
fi
2023-11-24 01:19:49 +03:00
# Iterate each file.
for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1"
# Increment count.
((count++))
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Skip if hash exists.
[[ -f "${hashfile}" ]] && continue
# Calculate hash.
pv "${target}" | sha1sum > ${hashfile}
# Report success.
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}"
false
fi
2023-08-08 16:24:15 +03:00
}
2023-10-23 00:36:27 +03:00
2023-11-23 00:13:51 +03:00
# Check stored values against actual files.
2023-11-22 14:51:46 +03:00
checksum_check()
2023-10-23 00:36:27 +03:00
{
2023-11-24 01:19:49 +03:00
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
2023-11-23 00:13:51 +03:00
local failed=0
2023-11-24 01:19:49 +03:00
local skipped=0
2023-11-23 00:13:51 +03:00
2023-11-24 01:19:49 +03:00
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
2023-11-23 00:13:51 +03:00
fi
2023-10-23 00:36:27 +03:00
2023-11-24 01:19:49 +03:00
# Iterate each file.
for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1"
2023-11-23 00:13:51 +03:00
2023-11-24 01:19:49 +03:00
# Increment count.
((count++))
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Skip if hash doesn't exist.
[[ -f "${hashfile}" ]] || { ((skipped++)); continue; }
# Calculate hash.
local stored=$(cat "${hashfile}" | cut -d\ -f1)
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
false
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
2023-11-22 14:51:46 +03:00
fi
2023-11-24 01:19:49 +03:00
}
2023-11-22 14:51:46 +03:00
2023-11-24 01:19:49 +03:00
_checksum_create()
{
local path="${1%/*}"
local name="${1##*/}"
sha1sum "${path}/${name}" > "${path}/.${name}.sha1"
2023-11-23 00:13:51 +03:00
}
_checksum_check()
{
local file="${1##*\ \ }"
local stored="${1%%\ \ *}"
# Skip if no file.
[[ -f "${file}" ]] || return 0
# Check file hash.
local actual=$(sha1sum "${file}")
actual="${actual%%\ \ *}"
# Compare values.
if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${file}: Failed.${color_default}"
return 1
fi
return 0
2023-11-22 14:51:46 +03:00
}
2023-11-23 00:13:51 +03:00
2023-11-24 01:19:49 +03:00
export -f checksum_create checksum_check
export -f _checksum_check _checksum_create