From e67a40b082f6e50440f78545691473f924328349 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Thu, 15 Feb 2024 18:41:53 +0100 Subject: [PATCH] Sets the file permissions by inheriting them from the directory that contains it. --- maintenance_mode/io.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/maintenance_mode/io.py b/maintenance_mode/io.py index 88920e2..d4dcc5c 100644 --- a/maintenance_mode/io.py +++ b/maintenance_mode/io.py @@ -1,20 +1,31 @@ import fsutil -def read_file(file_path, default_content=""): +def read_file(filepath, default_content=""): """ Read file at the specified path. If file doesn't exist, it will be created with default-content. Returns the file content. """ - if not fsutil.exists(file_path): - fsutil.write_file(file_path, default_content, atomic=True) - return fsutil.read_file(file_path) or default_content + if not fsutil.exists(filepath): + write_file(filepath, default_content) + return fsutil.read_file(filepath) or default_content -def write_file(file_path, content): +def set_file_permissions(filepath): + """ + Sets the file permissions by inheriting + them from the directory that contains it. + """ + dirpath, filename = fsutil.split_filepath(filepath) + dirpath_permissions = fsutil.get_permissions(dirpath) + fsutil.set_permissions(filepath, dirpath_permissions) + + +def write_file(filepath, content): """ Write file at the specified path with content. If file exists, it will be overwritten. """ - fsutil.write_file(file_path, content, atomic=True) + fsutil.write_file(filepath, content, atomic=True) + set_file_permissions(filepath)