Check if files exist before doing stuff

This commit is contained in:
Lukáš Kucharczyk 2022-11-16 09:39:47 +01:00
parent d7f4a9c299
commit f43c1d433a
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
1 changed files with 20 additions and 4 deletions

24
tag.sh
View File

@ -59,8 +59,10 @@ listtags() {
[ -z "$1" ] && fail "No filename supplied."
FILENAME="$1"
shift
LIMIT="${1:-0}"
! file_exists_in_db "$FILENAME" && fail "File '$FILENAME' does not exist in database."
LIMIT="${1:-0}"
ADDITIONAL_QUERY=""
[ $LIMIT -gt 0 ] && ADDITIONAL_QUERY="LIMIT $LIMIT"
@ -145,6 +147,9 @@ add_path() {
# $PATH1..$PATHN
TABLE="files"
COLUMN="filename"
for FILE in "$@"; do
file_exists_in_db "$FILE" && fail "File '$FILENAME' already exists in database."
done
sqlite_insert_single "$TABLE" "$COLUMN" "$@"
}
@ -154,8 +159,7 @@ add_hash() {
COLUMN="fid,md5"
local FILENAME="${1:-}"
[ -z "$FILENAME" ] && fail "No file specified."
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
[ -z "$RESULT" ] && fail "File \"$FILENAME\" does not exist in database."
! file_exists_in_db "$FILENAME" && fail "File \"$FILENAME\" does not exist in database."
sqlite_insert_multi "$TABLE" "$COLUMN" $RESULT "'$2'"
}
@ -175,6 +179,17 @@ tag_exists_in_db() {
fi
}
file_exists_in_db() {
# $FILENAME
FILENAME=${1:-}
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
if [[ -z "$RESULT" ]]; then
return 1
else
return 0
fi
}
main() {
[ -z "${1:-}" ] && fail "Usage: tag init/add/import/autoimport/list/listtags"
@ -219,8 +234,9 @@ main() {
if [[ "$1" = "tag" ]]; then
shift
[ -z "$1" ] && fail "No filename supplied."
[ -z "${1:-}" ] && fail "No filename supplied."
FILENAME="$1"
! file_exists_in_db "$FILENAME" && fail "File '$FILENAME' does not exist in database."
shift
[ -z "${1:-}" ] && fail "No tag supplied."
COUNTER=0