INNER CODE UNIT · Python
_write_secure_file
ChrispyBacon-dev/DockFlare · dockflare-agent/app/main.py:247
def _write_secure_file(path, writer):
directory = os.path.dirname(path)
os.makedirs(directory, exist_ok=True)
temp_path = None
try:
with tempfile.NamedTemporaryFile('w', dir=directory, delete=False) as tmp_file:
writer(tmp_file)
tmp_file.flush()
try:
os.fsync(tmp_file.fileno())
except (AttributeError, OSError):
pass
temp_path = tmp_file.name
try:
os.chmod(temp_path, 0o600)
except OSError:
pass
os.replace(temp_path, path)