exec.file.write_file(): fix situation where file exists but stat fails due to permissions

This commit is contained in:
InsanePrawn 2022-08-28 01:19:08 +02:00
parent bef0efc637
commit ac7d16e4a7

View file

@ -82,8 +82,12 @@ def write_file(
fstat: os.stat_result fstat: os.stat_result
exists = root_check_exists(path) exists = root_check_exists(path)
dirname = os.path.dirname(path) dirname = os.path.dirname(path)
failed = False
if exists: if exists:
fstat = os.stat(path) try:
fstat = os.stat(path)
except PermissionError:
failed = True
else: else:
chown_user = chown_user or get_user_name(os.getuid()) chown_user = chown_user or get_user_name(os.getuid())
chown_group = chown_group or get_group_name(os.getgid()) chown_group = chown_group or get_group_name(os.getgid())
@ -94,9 +98,10 @@ def write_file(
if mode: if mode:
if not mode.isnumeric(): if not mode.isnumeric():
raise Exception(f"Unknown file mode '{mode}' (must be numeric): {path}") raise Exception(f"Unknown file mode '{mode}' (must be numeric): {path}")
if not exists or stat.filemode(int(mode, 8)) != stat.filemode(fstat.st_mode): if not exists or failed or stat.filemode(int(mode, 8)) != stat.filemode(fstat.st_mode):
chmod_mode = mode chmod_mode = mode
failed = try_native_filewrite(path, content, chmod_mode) if not failed:
failed = try_native_filewrite(path, content, chmod_mode) is not None
if exists or failed: if exists or failed:
if failed: if failed:
try: try: