From 49287ac86e070414baf6c51f5df20d0cfbc1c2f6 Mon Sep 17 00:00:00 2001 From: CaliBrain Date: Sun, 27 Apr 2025 12:43:07 -0400 Subject: [PATCH] Revert "Better file permission initialization" (#164) Reverts calibrain/calibre-web-automated-book-downloader#162 --- Dockerfile | 4 ++++ backend.py | 18 +++++++++++------- entrypoint.sh | 51 +++++++-------------------------------------------- 3 files changed, 22 insertions(+), 51 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6cce8f9..2e61762 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,6 +66,7 @@ WORKDIR /app # Install Python dependencies using pip # Upgrade pip first, then copy requirements and install # Copying requirements.txt separately leverages build cache +# No --chown needed as it's copied as root COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt && \ # Clean root's pip cache @@ -83,6 +84,7 @@ RUN echo "#!/bin/sh" > /usr/local/bin/pyrequests && \ chmod +x /usr/local/bin/pyrequests # Copy application code *after* dependencies are installed +# No --chown needed as it's copied as root, entrypoint will handle permissions COPY . . # Final setup: permissions and directories in one layer @@ -90,6 +92,8 @@ COPY . . # Ownership will be handled by the entrypoint script. RUN mkdir -p /var/log/cwa-book-downloader /cwa-book-ingest && \ chmod +x /app/entrypoint.sh /app/tor.sh /app/genDebug.sh + # chown is removed + # Expose the application port EXPOSE ${FLASK_PORT} diff --git a/backend.py b/backend.py index 2057866..941fa1f 100644 --- a/backend.py +++ b/backend.py @@ -8,7 +8,7 @@ import subprocess import os from logger import setup_logger -from config import CUSTOM_SCRIPT +from config import CUSTOM_SCRIPT, CROSS_FILE_SYSTEM from env import INGEST_DIR, TMP_DIR, MAIN_LOOP_SLEEP_TIME, USE_BOOK_TITLE from models import book_queue, BookInfo, QueueStatus, SearchFilters import book_manager @@ -141,13 +141,17 @@ def _download_book(book_id: str) -> Optional[str]: final_path = INGEST_DIR / book_name if os.path.exists(book_path): - logger.info(f"Moving book to ingest directory then renaming: {book_path} -> {intermediate_path} -> {final_path}") - try: + if CROSS_FILE_SYSTEM: + logger.info(f"Copying book to ingest directory then renaming: {book_path} -> {intermediate_path} -> {final_path}") + try: + shutil.move(book_path, intermediate_path) + except Exception as e: + logger.debug(f"Error moving book: {e}, will try copying instead") + shutil.copy(book_path, intermediate_path) + os.remove(book_path) + else: + logger.info(f"Moving book to ingest directory: {book_path} -> {intermediate_path}") shutil.move(book_path, intermediate_path) - except Exception as e: - logger.debug(f"Error moving book: {e}, will try copying instead") - shutil.copy(book_path, intermediate_path) - os.remove(book_path) logger.info(f"Renaming book: {intermediate_path} -> {final_path}") os.rename(intermediate_path, final_path) return str(final_path) diff --git a/entrypoint.sh b/entrypoint.sh index 5c918a4..545d1b5 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,10 +6,11 @@ LOG_FILE=${LOG_DIR}/cwa-bd_entrypoint.log # Cleanup any existing files or folders in the log directory rm -rf $LOG_DIR/* + ( - if [ "$USING_TOR" = "true" ]; then - ./tor.sh - fi +if [ "$USING_TOR" = "true" ]; then + ./tor.sh +fi ) exec 3>&1 4>&2 @@ -51,43 +52,11 @@ fi # Get username for the UID (whether we just created it or it existed) USERNAME=$(getent passwd "$UID" | cut -d: -f1) echo "Username for UID $UID is $USERNAME" - -test_write() { - folder=$1 - mkdir -p $folder - set +e - ( - sudo -E -u "$USERNAME" HOME=/app echo 0123456789_TEST > $folder/calibre-web-automated-book-downloader_TEST_WRITE - ) - set -e - FILE_CONTENT=$(cat $folder/calibre-web-automated-book-downloader_TEST_WRITE) - rm -f $folder/calibre-web-automated-book-downloader_TEST_WRITE - [ "$FILE_CONTENT" = "0123456789_TEST" ] - result=$? - if [ $result -eq 0 ]; then - result_text="true" - else - result_text="false" - fi - echo "Test write to $folder by $USERNAME: $result_text" - return $result -} - # Ensure proper ownership of application directories change_ownership() { - folder=$1 - set +e - mkdir -p $folder - if test_write $folder; then - echo "Successfully wrote to $folder as $USERNAME, no need to change ownership" - else - echo "Failed to write to $folder as $USERNAME" - echo "Changing ownership of $folder to $USERNAME:$GID" - chown -R "${UID}:${GID}" "${folder}" || echo "Failed to change ownership for ${folder}, continuing..." - echo "Changing mode of $folder to group r/w" - chmod g+r,g+w "${folder}" || echo "Failed to change mode for ${folder}, continuing..." - fi - set -e + folder=$1 + echo "Changing ownership of $folder to $USERNAME:$GID" + chown -R "${UID}:${GID}" "${folder}" || echo "Failed to change ownership for ${folder}, continuing..." } change_ownership /app @@ -95,12 +64,6 @@ change_ownership /var/log/cwa-book-downloader change_ownership /cwa-book-ingest change_ownership /tmp/cwa-book-downloader -# Test write to all folders -test_write /app -test_write /var/log/cwa-book-downloader -test_write /cwa-book-ingest -test_write /tmp/cwa-book-downloader - # Set the command to run based on the environment is_prod=$(echo "$APP_ENV" | tr '[:upper:]' '[:lower:]') if [ "$is_prod" = "prod" ]; then