From e8d2ac5c625f4a66eab8c5ddb26fe4b71cf9959c Mon Sep 17 00:00:00 2001 From: Jo-Be-Co Date: Sat, 11 Apr 2026 21:22:54 +0200 Subject: [PATCH 1/2] #1729 added resilience when creating the link to the database file --- Docker/liberate.sh | 58 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/Docker/liberate.sh b/Docker/liberate.sh index 82947829..859ea4f2 100755 --- a/Docker/liberate.sh +++ b/Docker/liberate.sh @@ -72,43 +72,65 @@ create_db() { } setup_db() { - DBPATH=$1 - dbpattern="*.db" + local DBPATH=$1 + local dbpattern="*.db" + local FILE LINK LINK_ORIGIN files debug "using database directory ${DBPATH}" # Figure out the right databse file if [[ -z "${LIBATION_DB_FILE}" ]]; then - dbCount=$(find "${DBPATH}" -maxdepth 1 -type f -name "${dbpattern}" | wc -l) - if [ "${dbCount}" -gt 1 ]; - then - error "too many database files found, set LIBATION_DB_FILE to the filename you wish to use" - exit 1 - elif [ "${dbCount}" -eq 1 ]; - then - files=( ${DBPATH}/${dbpattern} ) - FILE=${files[0]} - else - FILE="${DBPATH}/LibationContext.db" - fi + shopt -s nullglob # files shall be empty if no match is found + files=( "${DBPATH}"/${dbpattern} ) + shopt -u nullglob + + case ${#files[@]} in + 0) + FILE="${DBPATH}/LibationContext.db" + ;; + 1) + FILE="${files[0]}" + ;; + *) + debug "found ${#files[@]} database files matching '${dbpattern}'" + error "too many database files found, set LIBATION_DB_FILE to the filename you wish to use" + exit 1 + ;; + esac else FILE="${DBPATH}/${LIBATION_DB_FILE}" fi - debug "planning to use database ${FILE}" + debug "planning to use database '${FILE}'" if [ -f "${FILE}" ]; then info "database found at ${FILE}" - elif [ ${LIBATION_CREATE_DB} = "true" ]; + elif [ "${LIBATION_CREATE_DB}" = "true" ]; then warn "database not found, creating one at ${FILE}" - create_db ${FILE} + create_db "${FILE}" else error "database not found and creation is disabled" exit 1 fi - ln -s "${FILE}" "${LIBATION_CONFIG_INTERNAL}/LibationContext.db" + + LINK="${LIBATION_CONFIG_INTERNAL}/LibationContext.db" + if [ -L "$LINK" ]; then + # check if the link is correct + LINK_ORIGIN=$(readlink -sfn "$LINK") + if [ "$LINK_ORIGIN" = "$(readlink -sfn "$FILE")" ]; then + warn "symlink '${LINK_ORIGIN}' to '${LINK}' already established" + return 0 + fi + warn "removing existing symlink '${LINK_ORIGIN}' to '${LINK}'" + rm -f "$LINK" + elif [ -e "$LINK" ]; then + error "found blocking file at '${LINK}' - can't create symlink" + exit 1 + fi + + ln -s "${FILE}" "${LINK}" } run() { From 4c212e752fe0f07aed08e2437a236f4a516dbf4d Mon Sep 17 00:00:00 2001 From: Jo-Be-Co Date: Sat, 11 Apr 2026 22:19:31 +0200 Subject: [PATCH 2/2] some refinements --- Docker/liberate.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Docker/liberate.sh b/Docker/liberate.sh index 859ea4f2..9eaa6888 100755 --- a/Docker/liberate.sh +++ b/Docker/liberate.sh @@ -74,12 +74,13 @@ create_db() { setup_db() { local DBPATH=$1 local dbpattern="*.db" - local FILE LINK LINK_ORIGIN files + local FILE LINK LINK_ORIGIN + local -a files debug "using database directory ${DBPATH}" # Figure out the right databse file - if [[ -z "${LIBATION_DB_FILE}" ]]; + if [[ -z $LIBATION_DB_FILE ]] then shopt -s nullglob # files shall be empty if no match is found files=( "${DBPATH}"/${dbpattern} ) @@ -104,9 +105,10 @@ setup_db() { debug "planning to use database '${FILE}'" - if [ -f "${FILE}" ]; then + if [[ -f $FILE ]] + then info "database found at ${FILE}" - elif [ "${LIBATION_CREATE_DB}" = "true" ]; + elif [[ $LIBATION_CREATE_DB == "true" ]] then warn "database not found, creating one at ${FILE}" create_db "${FILE}" @@ -116,16 +118,19 @@ setup_db() { fi LINK="${LIBATION_CONFIG_INTERNAL}/LibationContext.db" - if [ -L "$LINK" ]; then + if [[ -L $LINK ]] + then # check if the link is correct - LINK_ORIGIN=$(readlink -sfn "$LINK") - if [ "$LINK_ORIGIN" = "$(readlink -sfn "$FILE")" ]; then + LINK_ORIGIN=$(readlink -fn "$LINK") + if [[ $LINK_ORIGIN == $(readlink -fn "$FILE") ]] + then warn "symlink '${LINK_ORIGIN}' to '${LINK}' already established" return 0 fi warn "removing existing symlink '${LINK_ORIGIN}' to '${LINK}'" rm -f "$LINK" - elif [ -e "$LINK" ]; then + elif [[ -e $LINK ]] + then error "found blocking file at '${LINK}' - can't create symlink" exit 1 fi