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

69 lines
1.4 KiB
Bash
Raw Normal View History

2023-11-23 00:13:51 +03:00
# Save file checksums.
2023-11-22 14:51:46 +03:00
checksum_create()
2023-08-08 16:24:15 +03:00
{
2023-11-23 00:13:51 +03:00
local file=".checksum"
# Error if checksum already exists.
if [[ -f "${file}" ]]; then
2023-11-22 14:51:46 +03:00
echo -e "${color_bred}Checksum already exists.${color_default}"
return 1
2023-08-08 16:24:15 +03:00
fi
2023-11-23 00:13:51 +03:00
find -type f | parallel -j $(_core_count) -- sha1sum {} >> "${file}"
sed -i "/${file}/d" ${file}
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-23 00:13:51 +03:00
local file=".checksum"
local failed=0
cat "${file}" | parallel -j $(_core_count) -- _checksum_check {} || ((failed++))
if [[ ${failed} = 0 ]]; then
echo -e "${color_green}All successful.${color_default}"
return 0
else
# echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
return 1
fi
2023-10-23 00:36:27 +03:00
}
2023-11-23 00:13:51 +03:00
# Check old values and create new one if all was fine.
2023-11-22 14:51:46 +03:00
checksum_update()
{
2023-11-23 00:13:51 +03:00
local file=".checksum"
if [[ ! -f "${file}" ]]; then
2023-11-22 14:51:46 +03:00
echo -e "${color_bred}Checksum doesn't exists.${color_default}"
return 1
fi
2023-11-23 00:13:51 +03:00
checksum_check && rm "${file}" && checksum_create
}
_checksum_check()
{
local file="${1##*\ \ }"
local stored="${1%%\ \ *}"
local failed=0
# 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
export -f _checksum_check