Skip to content
Snippets Groups Projects
Commit 7f63061a authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- abort if script functions exit with "exit" (via set -e)

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