diff --git a/fix_cache_permissions.sh b/fix_cache_permissions.sh new file mode 100644 index 00000000..dbb29e9f --- /dev/null +++ b/fix_cache_permissions.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# LEDMatrix Cache Permissions Fix Script +# This script fixes permissions on the cache directory so it's writable by the daemon user + +echo "Fixing LEDMatrix cache directory permissions..." + +CACHE_DIR="/var/cache/ledmatrix" + +if [ ! -d "$CACHE_DIR" ]; then + echo "Cache directory does not exist. Run setup_cache.sh first." + exit 1 +fi + +# Get the real user (not root when running with sudo) +REAL_USER=${SUDO_USER:-$USER} + +echo "Current cache directory permissions:" +ls -la "$CACHE_DIR" + +echo "" +echo "Fixing permissions..." + +# Make the directory writable by the daemon user (which the system runs as) +sudo chmod 777 "$CACHE_DIR" + +# Also set ownership to daemon:daemon to match the cache files +sudo chown daemon:daemon "$CACHE_DIR" + +echo "" +echo "Updated cache directory permissions:" +ls -la "$CACHE_DIR" + +echo "" +echo "Testing write access..." +if sudo -u daemon test -w "$CACHE_DIR"; then + echo "✓ Cache directory is now writable by daemon user" +else + echo "✗ Cache directory is still not writable by daemon user" + exit 1 +fi + +echo "" +echo "Permissions fix complete! LEDMatrix should now use persistent caching." +echo "The cache will survive system restarts." \ No newline at end of file diff --git a/src/cache_manager.py b/src/cache_manager.py index d60cab7b..5395082e 100644 --- a/src/cache_manager.py +++ b/src/cache_manager.py @@ -62,18 +62,46 @@ class CacheManager: try: # Try /var/cache/ledmatrix first (most standard) system_cache_dir = '/var/cache/ledmatrix' - os.makedirs(system_cache_dir, exist_ok=True) - if os.access(system_cache_dir, os.W_OK): - return system_cache_dir + + # Check if directory exists and we can write to it + if os.path.exists(system_cache_dir): + # Test if we can write to the existing directory + test_file = os.path.join(system_cache_dir, '.writetest') + try: + with open(test_file, 'w') as f: + f.write('test') + os.remove(test_file) + return system_cache_dir + except (IOError, OSError): + self.logger.warning(f"Directory exists but is not writable: {system_cache_dir}") + else: + # Try to create the directory + os.makedirs(system_cache_dir, exist_ok=True) + if os.access(system_cache_dir, os.W_OK): + return system_cache_dir except Exception as e: self.logger.warning(f"Could not use /var/cache/ledmatrix: {e}") # Attempt 3: /opt/ledmatrix/cache (alternative persistent location) try: opt_cache_dir = '/opt/ledmatrix/cache' - os.makedirs(opt_cache_dir, exist_ok=True) - if os.access(opt_cache_dir, os.W_OK): - return opt_cache_dir + + # Check if directory exists and we can write to it + if os.path.exists(opt_cache_dir): + # Test if we can write to the existing directory + test_file = os.path.join(opt_cache_dir, '.writetest') + try: + with open(test_file, 'w') as f: + f.write('test') + os.remove(test_file) + return opt_cache_dir + except (IOError, OSError): + self.logger.warning(f"Directory exists but is not writable: {opt_cache_dir}") + else: + # Try to create the directory + os.makedirs(opt_cache_dir, exist_ok=True) + if os.access(opt_cache_dir, os.W_OK): + return opt_cache_dir except Exception as e: self.logger.warning(f"Could not use /opt/ledmatrix/cache: {e}")