slskd-permission-monitor.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. # Check if script is running as root/sudo
  3. if [ "$(id -u)" -ne 0 ]; then
  4. echo "❌ ERROR: This script must be run with sudo privileges!" >&2
  5. echo "Please re-run with: sudo $0"
  6. exit 1
  7. fi
  8. # Configuration
  9. WATCH_DIR="[CHANGE ME]" # Set this to your SLSKD download directory
  10. CHECK_INTERVAL=30 # Time in seconds between checks
  11. USER="[USER]"
  12. GROUP="[GROUP]" # If you don't know this, type `getent group` or `cat /etc/group`.
  13. PERMISSIONS="775" # Full permissions for all users
  14. TMP_DIR="/tmp/permission_monitor_$$" # Use PID in temp directory name for uniqueness
  15. # Create temp directory and set up cleanup
  16. mkdir -p "$TMP_DIR"
  17. # Function to clean up temp files on exit
  18. cleanup() {
  19. echo "🧹 Cleaning up temporary files..."
  20. rm -rf "$TMP_DIR"
  21. echo "✅ Cleanup complete. Exiting."
  22. exit 0
  23. }
  24. # Set up trap to call cleanup function on script exit, ctrl+c, etc.
  25. trap cleanup EXIT TERM INT HUP
  26. echo "🔍 Starting permission monitor for $WATCH_DIR"
  27. echo "👤 Will set ownership to $USER:$GROUP"
  28. echo "🔐 Will set permissions to $PERMISSIONS (rwxrwxrwx)"
  29. echo "🗑️ Will delete .DS_Store files and ._ resource fork files"
  30. echo "⏱️ Checking every $CHECK_INTERVAL seconds"
  31. echo "💾 Using temp directory: $TMP_DIR"
  32. # Initialize the file list
  33. find "$WATCH_DIR" -type f -o -type d | sort > "$TMP_DIR/files_before.txt"
  34. while true; do
  35. # Wait for the specified interval
  36. sleep $CHECK_INTERVAL
  37. # Clean up macOS metadata files
  38. echo "🔍 Searching for macOS metadata files to remove..."
  39. DS_STORE_COUNT=$(find "$WATCH_DIR" -name ".DS_Store" -type f | wc -l)
  40. RESOURCE_FORK_COUNT=$(find "$WATCH_DIR" -name "._*" -type f | wc -l)
  41. if [ "$DS_STORE_COUNT" -gt 0 ] || [ "$RESOURCE_FORK_COUNT" -gt 0 ]; then
  42. echo "🗑️ Removing macOS metadata files:"
  43. # Remove .DS_Store files
  44. if [ "$DS_STORE_COUNT" -gt 0 ]; then
  45. find "$WATCH_DIR" -name ".DS_Store" -type f -print -delete | while read file; do
  46. echo " 🗑️ Deleted: $file"
  47. done
  48. fi
  49. # Remove Apple resource fork files (._*)
  50. if [ "$RESOURCE_FORK_COUNT" -gt 0 ]; then
  51. find "$WATCH_DIR" -name "._*" -type f -print -delete | while read file; do
  52. echo " 🗑️ Deleted: $file"
  53. done
  54. fi
  55. fi
  56. # Get current file list (after cleaning up metadata files)
  57. find "$WATCH_DIR" -type f -o -type d | sort > "$TMP_DIR/files_current.txt"
  58. # Find new files (items in current but not in before)
  59. comm -13 "$TMP_DIR/files_before.txt" "$TMP_DIR/files_current.txt" > "$TMP_DIR/files_new.txt"
  60. # If we found new files, change their ownership and permissions
  61. if [ -s "$TMP_DIR/files_new.txt" ]; then
  62. echo "$(date '+%Y-%m-%d %H:%M:%S') - 🆕 Found new files/folders!"
  63. while IFS= read -r file; do
  64. # Skip macOS metadata files (should already be deleted, but just in case)
  65. if [[ "$file" == *".DS_Store" ]] || [[ "$file" == */._* ]]; then
  66. echo " ⏭️ Skipping metadata file: $file"
  67. continue
  68. fi
  69. echo " 🔧 Fixing permissions for: $file"
  70. chown -R $USER:$GROUP "$file"
  71. chmod -R $PERMISSIONS "$file"
  72. done < "$TMP_DIR/files_new.txt"
  73. fi
  74. # Update the before list for next iteration
  75. cp "$TMP_DIR/files_current.txt" "$TMP_DIR/files_before.txt"
  76. done