85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
import os
|
|
import stat
|
|
import subprocess
|
|
|
|
# Standardwerte für Berechtigungen
|
|
DEFAULT_DIR_PERMS = 0o755
|
|
DEFAULT_FILE_PERMS = 0o644
|
|
DEFAULT_OWNER = "65534:65534" # nobody:nogroup
|
|
|
|
def check_permissions(path):
|
|
try:
|
|
stat_info = os.stat(path)
|
|
uid = stat_info.st_uid
|
|
gid = stat_info.st_gid
|
|
mode = stat_info.st_mode
|
|
if uid == 65534 and gid == 65534 and (mode & 0o777) == 0o755:
|
|
print(f"Permissions are correct for: {path}")
|
|
else:
|
|
print(f"Warning: Permissions may not be correct for: {path}")
|
|
print(f"Current: UID={uid}, GID={gid}, Mode={mode:o}")
|
|
print(f"Expected: UID=65534, GID=65534, Mode=755")
|
|
except OSError as e:
|
|
print(f"Error checking permissions: {e}")
|
|
|
|
def set_permissions(path, is_directory=True):
|
|
if is_directory:
|
|
perms = input(f"Enter directory permissions [{DEFAULT_DIR_PERMS:o}]: ") or DEFAULT_DIR_PERMS
|
|
else:
|
|
perms = input(f"Enter file permissions [{DEFAULT_FILE_PERMS:o}]: ") or DEFAULT_FILE_PERMS
|
|
|
|
owner = input(f"Enter owner [{DEFAULT_OWNER}]: ") or DEFAULT_OWNER
|
|
|
|
if isinstance(perms, str):
|
|
perms = int(perms, 8)
|
|
|
|
try:
|
|
subprocess.run(['sudo', 'chmod', f'{perms:o}', path], check=True)
|
|
subprocess.run(['sudo', 'chown', owner, path], check=True)
|
|
print(f"Set permissions for {path}: {perms:o}, owner: {owner}")
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Error setting permissions: {e}")
|
|
|
|
def set_permissions_recursive(path):
|
|
for root, dirs, files in os.walk(path):
|
|
set_permissions(root, is_directory=True)
|
|
for file in files:
|
|
set_permissions(os.path.join(root, file), is_directory=False)
|
|
|
|
def ensure_correct_permissions(path, is_directory=True):
|
|
stat_info = os.stat(path)
|
|
uid = stat_info.st_uid
|
|
gid = stat_info.st_gid
|
|
mode = stat_info.st_mode
|
|
|
|
expected_mode = DEFAULT_DIR_PERMS if is_directory else DEFAULT_FILE_PERMS
|
|
|
|
if uid != 65534 or gid != 65534 or (mode & 0o777) != expected_mode:
|
|
print(f"Correcting permissions for: {path}")
|
|
set_permissions(path, is_directory)
|
|
else:
|
|
print(f"Permissions are already correct for: {path}")
|
|
|
|
def ensure_correct_permissions_recursive(path):
|
|
for root, dirs, files in os.walk(path):
|
|
ensure_correct_permissions(root, is_directory=True)
|
|
for file in files:
|
|
ensure_correct_permissions(os.path.join(root, file), is_directory=False)
|
|
|
|
def create_files_in_directory(directory):
|
|
while True:
|
|
create_file = input(f"Do you want to create a file in {directory}? (y/n): ").lower()
|
|
if create_file != 'y':
|
|
break
|
|
|
|
filename = input("Enter the file name: ")
|
|
full_path = os.path.join(directory, filename)
|
|
|
|
try:
|
|
with open(full_path, 'w') as f:
|
|
pass # Create an empty file
|
|
print(f"Created file: {full_path}")
|
|
set_permissions(full_path, is_directory=False)
|
|
except IOError as e:
|
|
print(f"Error creating file: {e}")
|