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

- added check_argument_count()

- bugfix, also --pipe mode needs a logfile, to avoid conflicting return in functions
- bugfix, create directories in result-folder if needed to reflect structure of watch-folder
parent 13861b7e
No related branches found
No related tags found
No related merge requests found
...@@ -95,12 +95,21 @@ validators[______lfulg_tif__current]="/usr/bin/checkit_tiff_current /etc/checkit ...@@ -95,12 +95,21 @@ validators[______lfulg_tif__current]="/usr/bin/checkit_tiff_current /etc/checkit
validators[______lfulg_tif_upcoming]="/usr/bin/checkit_tiff_upcoming /etc/checkit_tiff/retrogeomono_upcoming FILE" validators[______lfulg_tif_upcoming]="/usr/bin/checkit_tiff_upcoming /etc/checkit_tiff/retrogeomono_upcoming FILE"
validators[___fotothek_tif__current]="/usr/bin/checkit_tiff_current /etc/checkit_tiff/retrofoto_current FILE" validators[___fotothek_tif__current]="/usr/bin/checkit_tiff_current /etc/checkit_tiff/retrofoto_current FILE"
validators[___fotothek_tif_upcoming]="/usr/bin/checkit_tiff_upcoming /etc/checkit_tiff/retrofoto_upcoming FILE" validators[___fotothek_tif_upcoming]="/usr/bin/checkit_tiff_upcoming /etc/checkit_tiff/retrofoto_upcoming FILE"
validators[_____kitodo_icc__current]="/usr/bin/iccDumpProfile -v FILE" validators[_____kitodo_icc__current]="/usr/local/bin/iccDumpProfile -v FILE"
validators[_____kitodo_icc_upcoming]="/usr/bin/iccDumpProfile -v FILE" 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
check_argument_count() {
local count=$1
local expected=$2
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
}
# 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"
...@@ -129,6 +138,7 @@ print_statistics() { ...@@ -129,6 +138,7 @@ print_statistics() {
} }
update_statistics() { update_statistics() {
check_argument_count $# 5
local is_valid=$1 local is_valid=$1
local duration=$2 local duration=$2
local ftype=$3 local ftype=$3
...@@ -157,6 +167,7 @@ error() { ...@@ -157,6 +167,7 @@ error() {
} }
get_mimetype() { get_mimetype() {
check_argument_count $# 1
local filename=$1 local filename=$1
local res local res
res=$(file --mime-type "$filename" | sed -e "s/^.*: //") res=$(file --mime-type "$filename" | sed -e "s/^.*: //")
...@@ -300,6 +311,7 @@ get_cli_args() { ...@@ -300,6 +311,7 @@ get_cli_args() {
} }
prepare_cmd() { prepare_cmd() {
check_argument_count $# 3
local mode=$1 local mode=$1
local ftype=$2 local ftype=$2
local stage=$3 local stage=$3
...@@ -317,6 +329,7 @@ prepare_cmd() { ...@@ -317,6 +329,7 @@ prepare_cmd() {
} }
prepare_ftype() { prepare_ftype() {
check_argument_count $# 1
local mimetype=$1 local mimetype=$1
local ftype local ftype
debug "prepare_ftype, using mimetype: $mimetype" debug "prepare_ftype, using mimetype: $mimetype"
...@@ -340,6 +353,7 @@ prepare_ftype() { ...@@ -340,6 +353,7 @@ prepare_ftype() {
} }
estimate_mode() { estimate_mode() {
check_argument_count $# 1
local mimetype=$1 local mimetype=$1
debug "estimate_mode, using mimetype: $mimetype" debug "estimate_mode, using mimetype: $mimetype"
case ${mimetype} in case ${mimetype} in
...@@ -362,6 +376,7 @@ estimate_mode() { ...@@ -362,6 +376,7 @@ estimate_mode() {
} }
exec_cmd() { exec_cmd() {
check_argument_count $# 5
local cmd=$1 local cmd=$1
local ftype=$2 local ftype=$2
local workflow=$3 local workflow=$3
...@@ -371,20 +386,17 @@ exec_cmd() { ...@@ -371,20 +386,17 @@ exec_cmd() {
local stop_t local stop_t
start_t=$(date +"%s") start_t=$(date +"%s")
debug "scan_file, calling cmd='$cmd'" debug "scan_file, calling cmd='$cmd'"
if [ -n "$log" ]; then
$cmd >>"$log" 2>&1 $cmd >>"$log" 2>&1
else
$cmd 2>&1
fi
local is_valid=$? local is_valid=$?
stop_t=$(date +"%s") stop_t=$(date +"%s")
local duration=$((stop_t - start_t)) local duration=$((stop_t - start_t))
debug "scan_file, duration=$duration is_valid=$is_valid log=$log" debug "exec_cmd, duration=$duration is_valid=$is_valid log=$log"
update_statistics "$is_valid" "$duration" "$ftype" "$workflow" "$stage" update_statistics "$is_valid" "$duration" "$ftype" "$workflow" "$stage"
echo "$is_valid" echo "$is_valid"
} }
handle_input_if_requested() { handle_input_if_requested() {
check_argument_count $# 2
local filename=$1 local filename=$1
local is_valid=$2 local is_valid=$2
debug "handle_input_if_requested, filename=$filename is_valid=$is_valid" debug "handle_input_if_requested, filename=$filename is_valid=$is_valid"
...@@ -403,12 +415,18 @@ handle_input_if_requested() { ...@@ -403,12 +415,18 @@ handle_input_if_requested() {
} }
get_logfile() { get_logfile() {
check_argument_count $# 1
local filename=$1 local filename=$1
local logname local logname
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="" logname="$filename.log"
fi
logdir=$(dirname "$logname")
if [ ! -d "$logdir" ]; then
debug "get_logfile, mkdir $logdir"
mkdir -p "$logdir"
fi fi
debug "get_logfile, logname=$logname (filename=$filename)" debug "get_logfile, logname=$logname (filename=$filename)"
echo "$logname" echo "$logname"
...@@ -416,6 +434,7 @@ get_logfile() { ...@@ -416,6 +434,7 @@ get_logfile() {
scan_file() { scan_file() {
check_argument_count $# 1
local filename="$1" local filename="$1"
local mimetype local mimetype
local ftype local ftype
...@@ -438,7 +457,10 @@ scan_file() { ...@@ -438,7 +457,10 @@ scan_file() {
cmd=$(prepare_cmd "$MODE" "$ftype" "$stage" | sed -e "s#FILE#$filename#") cmd=$(prepare_cmd "$MODE" "$ftype" "$stage" | sed -e "s#FILE#$filename#")
is_valid=$(exec_cmd "$cmd" "$ftype" "$MODE" "$stage" "$logname") is_valid=$(exec_cmd "$cmd" "$ftype" "$MODE" "$stage" "$logname")
if [ "$is_valid" -eq 0 ]; then if [ "$is_valid" -eq 0 ]; then
debug "scan_file, early break"
break break
else
debug "scan_file, no early break, because is_valid='$is_valid'"
fi fi
done done
handle_input_if_requested "$filename" "$is_valid" handle_input_if_requested "$filename" "$is_valid"
...@@ -453,6 +475,7 @@ scan_file() { ...@@ -453,6 +475,7 @@ scan_file() {
} }
scan_dir() { scan_dir() {
check_argument_count $# 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
...@@ -468,6 +491,8 @@ if [ "$WITH_PIPE" -eq 1 ]; then ...@@ -468,6 +491,8 @@ if [ "$WITH_PIPE" -eq 1 ]; then
filename=$(mktemp --tmpdir validate_wrg.XXXX) filename=$(mktemp --tmpdir validate_wrg.XXXX)
cat - > "$filename" cat - > "$filename"
scan_file "$filename" scan_file "$filename"
cat "$filename.log"
rm -f "$filename.log" || error "could not remove temporary file'$filename.log'"
rm -f "$filename" || error "could not remove temporary file '$filename'" rm -f "$filename" || error "could not remove temporary file '$filename'"
else else
if [ "$WITH_DAEMON" -eq 1 ]; then if [ "$WITH_DAEMON" -eq 1 ]; then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment