mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-02-25 08:43:36 +00:00
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
31 lines
655 B
Python
31 lines
655 B
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import contextlib
|
|
import os
|
|
import uuid
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def atomically_rewrite(filepath):
|
|
successful = False
|
|
tmp = '%s.%s.plinth-tmp' % (filepath, uuid.uuid4().hex)
|
|
fd = open(tmp, 'x')
|
|
|
|
try:
|
|
# Let client write to a temporary file
|
|
yield fd
|
|
successful = True
|
|
finally:
|
|
fd.close()
|
|
|
|
try:
|
|
if successful:
|
|
# Invoke rename(2) to atomically replace the original
|
|
os.rename(tmp, filepath)
|
|
finally:
|
|
# Delete temp file
|
|
try:
|
|
os.unlink(tmp)
|
|
except FileNotFoundError:
|
|
pass
|