mitch donaberger 1 сар өмнө
parent
commit
2c859e0177
1 өөрчлөгдсөн 93 нэмэгдсэн , 0 устгасан
  1. 93 0
      permissions-fixer.sh

+ 93 - 0
permissions-fixer.sh

@@ -0,0 +1,93 @@
+#!/bin/bash
+
+# Check if script is running as root/sudo
+if [ "$(id -u)" -ne 0 ]; then
+    echo "❌ ERROR: This script must be run with sudo privileges!" >&2
+    echo "Please re-run with: sudo $0"
+    exit 1
+fi
+
+# Configuration
+WATCH_DIR="/media/mitch/media-annex1"  # Set this to your SLSKD download directory
+CHECK_INTERVAL=30  # Time in seconds between checks
+USER="mitch"
+GROUP="mitch"
+PERMISSIONS="777"  # Full permissions for all users
+TMP_DIR="/tmp/permission_monitor_$$"  # Use PID in temp directory name for uniqueness
+
+# Create temp directory and set up cleanup 
+mkdir -p "$TMP_DIR"
+
+# Function to clean up temp files on exit
+cleanup() {
+    echo "🧹 Cleaning up temporary files..."
+    rm -rf "$TMP_DIR"
+    echo "✅ Cleanup complete. Exiting."
+    exit 0
+}
+
+# Set up trap to call cleanup function on script exit, ctrl+c, etc.
+trap cleanup EXIT TERM INT HUP
+
+echo "🔍 Starting permission monitor for $WATCH_DIR"
+echo "👤 Will set ownership to $USER:$GROUP"
+echo "🔐 Will set permissions to $PERMISSIONS (rwxrwxrwx)"
+echo "🗑️ Will delete .DS_Store files and ._ resource fork files"
+echo "⏱️ Checking every $CHECK_INTERVAL seconds"
+echo "💾 Using temp directory: $TMP_DIR"
+
+# Initialize the file list
+find "$WATCH_DIR" -type f -o -type d | sort > "$TMP_DIR/files_before.txt"
+
+while true; do
+    # Wait for the specified interval
+    sleep $CHECK_INTERVAL
+    
+    # Clean up macOS metadata files
+    echo "🔍 Searching for macOS metadata files to remove..."
+    DS_STORE_COUNT=$(find "$WATCH_DIR" -name ".DS_Store" -type f | wc -l)
+    RESOURCE_FORK_COUNT=$(find "$WATCH_DIR" -name "._*" -type f | wc -l)
+    
+    if [ "$DS_STORE_COUNT" -gt 0 ] || [ "$RESOURCE_FORK_COUNT" -gt 0 ]; then
+        echo "🗑️ Removing macOS metadata files:"
+        
+        # Remove .DS_Store files
+        if [ "$DS_STORE_COUNT" -gt 0 ]; then
+            find "$WATCH_DIR" -name ".DS_Store" -type f -print -delete | while read file; do
+                echo "  🗑️ Deleted: $file"
+            done
+        fi
+        
+        # Remove Apple resource fork files (._*)
+        if [ "$RESOURCE_FORK_COUNT" -gt 0 ]; then
+            find "$WATCH_DIR" -name "._*" -type f -print -delete | while read file; do
+                echo "  🗑️ Deleted: $file"
+            done
+        fi
+    fi
+    
+    # Get current file list (after cleaning up metadata files)
+    find "$WATCH_DIR" -type f -o -type d | sort > "$TMP_DIR/files_current.txt"
+    
+    # Find new files (items in current but not in before)
+    comm -13 "$TMP_DIR/files_before.txt" "$TMP_DIR/files_current.txt" > "$TMP_DIR/files_new.txt"
+    
+    # If we found new files, change their ownership and permissions
+    if [ -s "$TMP_DIR/files_new.txt" ]; then
+        echo "$(date '+%Y-%m-%d %H:%M:%S') - 🆕 Found new files/folders!"
+        while IFS= read -r file; do
+            # Skip macOS metadata files (should already be deleted, but just in case)
+            if [[ "$file" == *".DS_Store" ]] || [[ "$file" == */._* ]]; then
+                echo "  ⏭️ Skipping metadata file: $file"
+                continue
+            fi
+            
+            echo "  🔧 Fixing permissions for: $file"
+            chown -R $USER:$GROUP "$file"
+            chmod -R $PERMISSIONS "$file"
+        done < "$TMP_DIR/files_new.txt"
+    fi
+    
+    # Update the before list for next iteration
+    cp "$TMP_DIR/files_current.txt" "$TMP_DIR/files_before.txt"
+done