dir-check: change naming and add log file

This commit is contained in:
Jérémy Lecour 2022-06-02 18:23:40 +02:00 committed by Jérémy Lecour
parent df0c850ceb
commit db28f0c47d

View file

@ -3,7 +3,7 @@
PROGNAME="dir-check" PROGNAME="dir-check"
REPOSITORY="https://gitea.evolix.org/evolix/ansible-roles" REPOSITORY="https://gitea.evolix.org/evolix/ansible-roles"
VERSION="22.06" VERSION="22.06.1"
readonly VERSION readonly VERSION
show_version() { show_version() {
@ -48,9 +48,6 @@ is_verbose() {
is_quiet() { is_quiet() {
test "${quiet}" = "1" test "${quiet}" = "1"
} }
is_check() {
test "${check}" = "1"
}
log_line() { log_line() {
level=$1 level=$1
msg=$2 msg=$2
@ -117,18 +114,19 @@ log_fatal() {
fi fi
} }
metadata_algorithm() { data_command() {
echo "du --bytes" echo "du --bytes"
} }
list_files_with_size() { list_files_with_size() {
path=$1 path=$1
find "${path}" -type f -exec $(metadata_algorithm) {} \; | sort -k2 # shellcheck disable=SC2014,SC2046
find "${path}" -type f -exec $(data_command) {} \; | sort -k2
} }
prepare_metadata() { prepare_data() {
list_files_with_size "${final_dir}" > "${metadata_file}" list_files_with_size "${final_dir}" > "${data_file}"
"${checksum_bin}" "${metadata_file}" > "${checksum_file}" "${checksum_bin}" "${data_file}" > "${checksum_file}"
} }
check_metadata() { check_data() {
if [ -f "${checksum_file}" ]; then if [ -f "${checksum_file}" ]; then
# subshell to scope the commands to "parent_dir" # subshell to scope the commands to "parent_dir"
"${checksum_bin}" --status --check "${checksum_file}" "${checksum_bin}" --status --check "${checksum_file}"
@ -140,28 +138,28 @@ check_metadata() {
else else
log_warning "Couldn't find checksum file \`${checksum_file}' (inside \`${parent_dir}'). Skip verification." log_warning "Couldn't find checksum file \`${checksum_file}' (inside \`${parent_dir}'). Skip verification."
fi fi
if [ -f "${metadata_file}" ]; then if [ -f "${data_file}" ]; then
while read metadata_line; do while read -r data_line; do
expected_size=$(echo "${metadata_line}" | cut -f1) expected_size=$(echo "${data_line}" | cut -f1)
file=$(echo "${metadata_line}" | cut -f2) file=$(echo "${data_line}" | cut -f2)
if [ -f "${file}" ]; then if [ -f "${file}" ]; then
actual_size=$($(metadata_algorithm) "${file}" | cut -f1) actual_size=$($(data_command) "${file}" | cut -f1)
if [ "${actual_size}" != "${expected_size}" ]; then if [ "${actual_size}" != "${expected_size}" ]; then
log_error "File ${file}' has actual size of ${actual_size} instead of ${expected_size}." log_error "File \`${file}' has actual size of ${actual_size} instead of ${expected_size}."
rc=1 rc=1
fi fi
else else
log_error "Couldn't find file \`${file}'." log_error "Couldn't find file \`${file}'."
rc=1 rc=1
fi fi
done < "${metadata_file}" done < "${data_file}"
if [ ${rc} -eq 0 ]; then if [ ${rc} -eq 0 ]; then
log_info "Directory \`${final_dir}' is consistent with metadata stored in \`${metadata_file}' (inside \`${parent_dir}')." log_info "Directory \`${final_dir}' is consistent with data stored in \`${data_file}' (inside \`${parent_dir}')."
fi fi
else else
log_fatal "Couldn't find metadata file \`${metadata_file}' (inside \`${parent_dir}')." log_fatal "Couldn't find data file \`${data_file}' (inside \`${parent_dir}')."
exit 1 exit 1
fi fi
} }
@ -185,8 +183,8 @@ main() {
parent_dir=$(dirname "${dir}") parent_dir=$(dirname "${dir}")
final_dir=$(basename "${dir}") final_dir=$(basename "${dir}")
metadata_file="${final_dir}.metadata" data_file="${PROGNAME}.db"
checksum_file="${metadata_file}.${checksum_cmd}" checksum_file="${data_file}.${checksum_cmd}"
cwd=${PWD} cwd=${PWD}
cd "${parent_dir}" || log_error "Impossible to change to \`${parent_dir}'" cd "${parent_dir}" || log_error "Impossible to change to \`${parent_dir}'"
@ -200,10 +198,10 @@ main() {
case ${action} in case ${action} in
check) check)
check_metadata check_data
;; ;;
prepare) prepare)
prepare_metadata prepare_data
;; ;;
*) *)
log_fatal "Unknown action \`${action}'." log_fatal "Unknown action \`${action}'."
@ -211,7 +209,11 @@ main() {
;; ;;
esac esac
cd "${cwd}" || log_error "Impossible to change back to \`${cwd}'" if [ -d "${cwd}" ]; then
cd "${cwd}" || log_error "Impossible to change back to \`${cwd}'"
else
log_error "Previous working directory \`${cwd}' is not a directory."
fi
} }
# Declare variables # Declare variables
@ -235,7 +237,7 @@ while :; do
exit 0 exit 0
;; ;;
--dir) -d|--dir)
# with value separated by space # with value separated by space
if [ -n "$2" ]; then if [ -n "$2" ]; then
dir="$2" dir="$2"
@ -253,6 +255,24 @@ while :; do
log_fatal '"--dir" requires a non-empty option argument.' log_fatal '"--dir" requires a non-empty option argument.'
;; ;;
-l|--log)
# with value separated by space
if [ -n "$2" ]; then
log_file="$2"
shift
else
log_fatal 'ERROR: "--log" requires a non-empty option argument.'
fi
;;
--log=?*)
# with value speparated by =
log_file=${1#*=}
;;
--log=)
# without value
log_fatal '"--log" requires a non-empty option argument.'
;;
--prepare) --prepare)
action="prepare" action="prepare"
;; ;;