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

84 lines
1.9 KiB
Bash
Raw Normal View History

2023-11-23 00:13:51 +03:00
# Save file checksums.
2023-12-07 05:11:26 +03:00
# For file with a name `file` it will create a new file called `.file.sha1` with hash in it.
2023-12-07 04:02:47 +03:00
# All files by default.
2023-11-24 01:19:49 +03:00
# Usage: checksum_create [FILES]
2023-12-07 01:44:42 +03:00
function checksum_create() {
2023-12-05 21:50:45 +03:00
local IFS=$'\n'
2023-12-17 20:12:33 +03:00
local targets=(${@})
[[ "${targets}" = "" ]] && targets=($(_ls_file))
2023-12-05 21:50:45 +03:00
2023-12-17 20:12:33 +03:00
process() {
2023-12-05 21:50:45 +03:00
local hashfile=".${target#./}.sha1"
# Skip if hash exists.
2023-12-17 20:12:33 +03:00
[[ -f "${hashfile}" ]] && return 0
2023-12-05 21:50:45 +03:00
# Calculate hash.
2023-12-17 20:12:33 +03:00
pv ${target} | sha1sum > ${hashfile}
}
2023-12-05 21:50:45 +03:00
2023-12-17 20:12:33 +03:00
_iterate_targets process ${targets[@]}
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-12-07 04:02:47 +03:00
# All files by default.
# Usage: checksum_check [FILES]
2023-12-07 01:44:42 +03:00
function checksum_check() {
2023-12-05 21:50:45 +03:00
local IFS=$'\n'
2023-12-17 20:12:33 +03:00
local targets=(${@})
[[ "${targets}" = "" ]] && targets=($(_ls_file))
2023-12-05 21:50:45 +03:00
2023-12-17 20:12:33 +03:00
process() {
2023-12-05 21:50:45 +03:00
local hashfile=".${target#./}.sha1"
# Skip if hash doesn't exist.
2024-01-03 18:50:59 +03:00
[[ -f "${hashfile}" ]] || { _iterate_skip "No hash found."; return 0; }
2023-12-05 21:50:45 +03:00
# Calculate hash.
2023-12-07 22:37:08 +03:00
local stored=$(cat "${hashfile}" | cut -d\ -f1)
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
2023-12-05 21:50:45 +03:00
if [[ "${stored}" != "${actual}" ]]; then
2023-12-17 20:12:33 +03:00
_error "Failed."
return 1
2023-12-05 21:50:45 +03:00
fi
2023-12-17 20:12:33 +03:00
}
2023-12-05 21:50:45 +03:00
2023-12-17 20:12:33 +03:00
_iterate_targets process ${targets[@]}
2023-11-24 01:19:49 +03:00
}
2023-11-22 14:51:46 +03:00
2023-12-07 05:11:26 +03:00
# Calculate hashes for all files recursively and store in a file called `checksum.sha1`.
2023-12-07 01:44:42 +03:00
function checksum() {
2023-12-05 21:50:45 +03:00
find -type f | parallel -j $(_core_count) -- sha1sum {} >> checksum.sha1
}
2023-12-07 04:02:47 +03:00
# Create checksum for a file.
# Usage: _checksum_create <FILE>
2023-12-07 01:44:42 +03:00
function _checksum_create() {
2023-12-05 21:50:45 +03:00
local path="${1%/*}"
local name="${1##*/}"
sha1sum "${path}/${name}" > "${path}/.${name}.sha1"
2023-11-23 00:13:51 +03:00
}
2023-12-07 04:02:47 +03:00
# Check checksum for a file.
# Usage: _checksum_check <FILE>
2023-12-07 01:44:42 +03:00
function _checksum_check() {
2023-12-05 21:50:45 +03:00
local file="${1##*\ \ }"
local stored="${1%%\ \ *}"
2023-11-23 00:13:51 +03:00
2023-12-05 21:50:45 +03:00
# Skip if no file.
[[ -f "${file}" ]] || return 0
2023-11-23 00:13:51 +03:00
2023-12-05 21:50:45 +03:00
# Check file hash.
local actual=$(sha1sum "${file}")
actual="${actual%%\ \ *}"
2023-11-23 00:13:51 +03:00
2023-12-05 21:50:45 +03:00
# Compare values.
if [[ "${stored}" != "${actual}" ]]; then
_error "${file}: Failed."
2023-12-05 21:50:45 +03:00
return 1
fi
2023-11-23 00:13:51 +03:00
2023-12-05 21:50:45 +03:00
return 0
2023-11-22 14:51:46 +03:00
}