diff --git a/validate_workflow.sh b/validate_workflow.sh index 9192645176b5e0a4e06a0c673a076d5a0d3f2296..b22d643a9b45509aa8c6b79506493e161437a312 100755 --- a/validate_workflow.sh +++ b/validate_workflow.sh @@ -100,16 +100,25 @@ validators[_____kitodo_icc_upcoming]="/usr/local/bin/iccDumpProfile -v FILE" set -o nounset # Treat unset variables as an error +set -e check_argument_count() { local count=$1 local expected=$2 - if [ $count -ne $expected ]; then + if [ "$count" -ne "$expected" ]; then error "called function ${FUNCNAME[1]} expected $expected params, but got $count by caller function ${FUNCNAME[2]} (line ${BASH_LINENO[2]})" exit 1 fi } +check_argument_notempty() { + local param=$1 + if [ -z "$param" ]; then + error "called function ${FUNCNAME[1]} expected non-empty params, but some are empty set in line ${BASH_LINENO[0]}, eventually taken from caller function ${FUNCNAME[1]} (line ${BASH_LINENO[1]})" + exit 1 + fi +} + # Don't just call this function "help()", as that's a reserved command in Bash. comment_help() { sed -rn 's/^#hh ?//;T;p' "$0" @@ -144,6 +153,11 @@ update_statistics() { local ftype=$3 local workflow=$4 local stage=$5 + check_argument_notempty "$is_valid" + check_argument_notempty "$duration" + check_argument_notempty "$ftype" + check_argument_notempty "$workflow" + check_argument_notempty "$stage" local date date=$(date +"%F%T") debug "date=$date" @@ -162,13 +176,19 @@ debug() { fi } +warn() { + >&2 echo "WARN: $1" +} + error() { >&2 echo "ERROR: $1" + exit 1 } get_mimetype() { check_argument_count $# 1 local filename=$1 + check_argument_notempty "$filename" local res res=$(file --mime-type "$filename" | sed -e "s/^.*: //") echo "$res" @@ -315,22 +335,29 @@ prepare_cmd() { local mode=$1 local ftype=$2 local stage=$3 + check_argument_notempty "$mode" + check_argument_notempty "$ftype" + check_argument_notempty "$stage" local key local cmd key=$(printf "%11s%4s%9s" "$mode" "$ftype" "$stage"|sed -e "y/ /_/") + check_argument_notempty "$key" debug "prepare_cmd, key=$key" if [[ ${validators[$key]:+1} ]]; then cmd=${validators[$key]}; + check_argument_notempty "$cmd" debug "prepare_cmd, cmd=$cmd" echo "$cmd" else debug "no valid command found using key $key" + echo "echo 'no validation tool detected!'" fi } prepare_ftype() { check_argument_count $# 1 local mimetype=$1 + check_argument_notempty "$mimetype" local ftype debug "prepare_ftype, using mimetype: $mimetype" case ${mimetype} in @@ -344,10 +371,11 @@ prepare_ftype() { ftype="icc" ;; *) - error "unknown file format '$mimetype'" - exit 1 + warn "unknown file format with mime-type '$mimetype'" + ftype="???" ;; esac + check_argument_notempty "$ftype" debug "prepare_ftype, detect ftype: $ftype" echo "$ftype" } @@ -355,6 +383,7 @@ prepare_ftype() { estimate_mode() { check_argument_count $# 1 local mimetype=$1 + check_argument_notempty "$mimetype" debug "estimate_mode, using mimetype: $mimetype" case ${mimetype} in "image/tiff") @@ -367,10 +396,11 @@ estimate_mode() { MODE="kitodo" ;; *) - error "workflow not detectable" - exit 1 + warn "workflow not detectable for mimetype $mimetype" + MODE="???" ;; esac + check_argument_notempty "$MODE" debug "estimate_mode, detected mode: $MODE" echo "$MODE" } @@ -384,10 +414,16 @@ exec_cmd() { local log=$5 local start_t local stop_t + check_argument_notempty "$cmd" + check_argument_notempty "$ftype" + check_argument_notempty "$workflow" + check_argument_notempty "$stage" + check_argument_notempty "$log" start_t=$(date +"%s") debug "scan_file, calling cmd='$cmd'" $cmd >>"$log" 2>&1 local is_valid=$? + check_argument_notempty $is_valid stop_t=$(date +"%s") local duration=$((stop_t - start_t)) debug "exec_cmd, duration=$duration is_valid=$is_valid log=$log" @@ -399,6 +435,8 @@ handle_input_if_requested() { check_argument_count $# 2 local filename=$1 local is_valid=$2 + check_argument_notempty "$filename" + check_argument_notempty "$is_valid" debug "handle_input_if_requested, filename=$filename is_valid=$is_valid" if [ "$FILES_MODE" = "sort" ]; then if [ "$is_valid" -eq 0 ]; then @@ -418,12 +456,15 @@ get_logfile() { check_argument_count $# 1 local filename=$1 local logname + check_argument_notempty "$filename" if [ -n "${WATCH_FOLDER}" ] && [ "${WITH_PIPE}" -eq 0 ]; then logname=$(echo "$filename"| sed -e "s#^${WATCH_FOLDER}#${RESULT_FOLDER}#" -e "s#\$#.log#") else # pipe uses a temp filename logname="$filename.log" fi + check_argument_notempty "$logname" logdir=$(dirname "$logname") + check_argument_notempty "$logdir" if [ ! -d "$logdir" ]; then debug "get_logfile, mkdir $logdir" mkdir -p "$logdir" @@ -441,6 +482,7 @@ scan_file() { local logname local cmd local is_valid + check_argument_notempty "$filename" debug "scan_file, using filename: $filename" mimetype=$(get_mimetype "$filename") ftype=$(prepare_ftype "$mimetype") @@ -449,6 +491,9 @@ scan_file() { # try best guess MODE=$(estimate_mode "$mimetype") fi + check_argument_notempty "$mimetype" + check_argument_notempty "$ftype" + check_argument_notempty "$logname" trap "" SIGINT debug "scan_file, === entering protected area ===" if [ "$STAGE" = "any" ]; then @@ -476,6 +521,7 @@ scan_file() { scan_dir() { check_argument_count $# 1 + check_argument_notempty "$1" find "$1" -type f -print0| while IFS= read -r -d '' filename; do scan_file "$filename" done