Skip to content
Snippets Groups Projects
Commit d762f7e8 authored by Jens Steidl's avatar Jens Steidl :baby_chick:
Browse files

- new feature: validation result as exit code in pipe mode

- added more debug infos
- refactor: validation command creation & execution
parent 7c18fb5d
No related branches found
No related tags found
No related merge requests found
Pipeline #5293 failed
...@@ -74,9 +74,9 @@ FILES_MODE_LIST="sort,delete,nothing" ...@@ -74,9 +74,9 @@ FILES_MODE_LIST="sort,delete,nothing"
# DEFAULTS # DEFAULTS
WITH_DAEMON=0 WITH_DAEMON=0
WITH_DEBUG=0 WITH_DEBUG=0
WITH_PIPE=0 export WITH_PIPE=0
STAGE=any export STAGE=any
MODE=auto export MODE=auto
FILES_MODE=nothing FILES_MODE=nothing
STATFILE=${HOME}/.cache/validate_workflows/statistics.cnt STATFILE=${HOME}/.cache/validate_workflows/statistics.cnt
LOCKFILE=/var/lock/validate_workflows.lock LOCKFILE=/var/lock/validate_workflows.lock
...@@ -221,6 +221,12 @@ get_mimetype() { ...@@ -221,6 +221,12 @@ get_mimetype() {
echo "${res}" echo "${res}"
} }
echo_validation_result_if_pipe() {
local is_valid="$1"
if [[ "${WITH_PIPE}" -eq 1 ]]; then
echo "${is_valid}"
fi
}
get_cli_args() { get_cli_args() {
local lines local lines
...@@ -372,7 +378,7 @@ prepare_cmd() { ...@@ -372,7 +378,7 @@ prepare_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!'" error "No validation tool detected!"
fi fi
} }
...@@ -509,6 +515,30 @@ get_logfile() { ...@@ -509,6 +515,30 @@ get_logfile() {
echo "${logname}" echo "${logname}"
} }
run_validation_cmd() {
local mode="$1"
local mimetype="$2"
local ftype="$3"
local stage="$4"
local filename="$5"
local logname="$6"
local cmd
local cmd_exitcode
local is_valid
cmd=$(prepare_cmd "${mode}" "${ftype}" "${stage}") # has FILE placeholder
cmd_exitcode=$?
debug "cmd: ${cmd}"
debug "cmd_exitcode: ${cmd_exitcode}"
if [[ "${cmd_exitcode}" -eq 1 ]]; then
is_valid="1" # invalid, prepare cmd failed
echo "Failed to build validation command from stage '${stage}', mode '${mode}', mimetype '${mimetype}', mapped file type '${ftype}'" >> "${logname}"
else
cmd=$(echo "${cmd}" | sed -e "s#FILE#${filename}#") # replace FILE placeholder
is_valid=$(exec_cmd "${cmd}" "${ftype}" "${mode}" "${stage}" "${logname}")
fi
debug "run_validation_cmd, is_valid: ${is_valid}"
echo "${is_valid}"
}
scan_file() { scan_file() {
check_argument_count $# 1 check_argument_count $# 1
...@@ -535,8 +565,7 @@ scan_file() { ...@@ -535,8 +565,7 @@ scan_file() {
if [[ "${STAGE}" = "any" ]]; then if [[ "${STAGE}" = "any" ]]; then
is_valid=1 is_valid=1
for stage in upcoming current; do for stage in upcoming current; do
cmd=$(prepare_cmd "${MODE}" "${ftype}" "${stage}" | sed -e "s#FILE#${filename}#") is_valid=$(run_validation_cmd "${MODE}" "${mimetype}" "${ftype}" "${stage}" "${filename}" "${logname}") # stage set by loop
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" debug "scan_file, early break"
break break
...@@ -546,13 +575,13 @@ scan_file() { ...@@ -546,13 +575,13 @@ scan_file() {
done done
handle_input_if_requested "${filename}" "${is_valid}" handle_input_if_requested "${filename}" "${is_valid}"
else else
cmd=$(prepare_cmd "${MODE}" "${ftype}" "${STAGE}" | sed -e "s#FILE#${filename}#") is_valid=$(run_validation_cmd "${MODE}" "${mimetype}" "${ftype}" "${STAGE}" "${filename}" "${logname}") # stage set by user
is_valid=$(exec_cmd "${cmd}" "${ftype}" "${MODE}" "${STAGE}" "${logname}")
handle_input_if_requested "${filename}" "${is_valid}" handle_input_if_requested "${filename}" "${is_valid}"
fi fi
debug "scan_file, === leaving protected area ===" debug "scan_file, === leaving protected area ==="
trap - SIGINT trap - SIGINT
debug "---" debug "---"
echo_validation_result_if_pipe "${is_valid}"
} }
scan_dir() { scan_dir() {
...@@ -572,10 +601,12 @@ if [[ "${WITH_PIPE}" -eq 1 ]]; then ...@@ -572,10 +601,12 @@ if [[ "${WITH_PIPE}" -eq 1 ]]; then
debug "checking stream" debug "checking stream"
filename=$(mktemp --tmpdir validate_wrg.XXXX) filename=$(mktemp --tmpdir validate_wrg.XXXX)
cat - > "${filename}" cat - > "${filename}"
scan_file "${filename}" result=$(scan_file "${filename}")
debug "pipe mode result (is_valid): ${result}"
cat "${filename}.log" cat "${filename}.log"
rm -f "${filename}.log" || error "could not remove temporary file '${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}'"
exit "${result}"
else else
if [[ "${WITH_DAEMON}" -eq 1 ]]; then if [[ "${WITH_DAEMON}" -eq 1 ]]; then
# TODO: protect DAEMON from STRG-C for clean shutdown # TODO: protect DAEMON from STRG-C for clean shutdown
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment