69 lines
1.4 KiB
Bash
69 lines
1.4 KiB
Bash
# Save file checksums.
|
|
checksum_create()
|
|
{
|
|
local file=".checksum"
|
|
|
|
# Error if checksum already exists.
|
|
if [[ -f "${file}" ]]; then
|
|
echo -e "${color_bred}Checksum already exists.${color_default}"
|
|
return 1
|
|
fi
|
|
|
|
find -type f | parallel -j $(_core_count) -- sha1sum {} >> "${file}"
|
|
sed -i "/${file}/d" ${file}
|
|
}
|
|
|
|
# Check stored values against actual files.
|
|
checksum_check()
|
|
{
|
|
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
|
|
}
|
|
|
|
# Check old values and create new one if all was fine.
|
|
checksum_update()
|
|
{
|
|
local file=".checksum"
|
|
|
|
if [[ ! -f "${file}" ]]; then
|
|
echo -e "${color_bred}Checksum doesn't exists.${color_default}"
|
|
return 1
|
|
fi
|
|
|
|
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
|
|
}
|
|
|
|
export -f _checksum_check
|