Skip to content

Commit

Permalink
Rewrite AtomicFile to use NamedTemporaryFile
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Jul 26, 2024
1 parent e395137 commit afba9f8
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions python/eups/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,17 +921,17 @@ def AtomicFile(fn: str, mode: str):
"""
dir = os.path.dirname(fn)

fh, tmpfn = tempfile.mkstemp(suffix='.tmp', dir=dir)
fp = os.fdopen(fh, mode)
with tempfile.NamedTemporaryFile(
prefix=dir, suffix=".tmp", delete=False, mode=mode,
) as fh:
yield fh

yield fp
# Needed because fclose() doesn't guarantee fsync()
# in POSIX, which may lead to interesting issues (e.g., see
# http://thunk.org/tytso/blog/2009/03/12/delayed-allocation-and-the-zero-length-file-problem/ )
os.fsync(fh)

# Needed because fclose() doesn't guarantee fsync()
# in POSIX, which may lead to interesting issues (e.g., see
# http://thunk.org/tytso/blog/2009/03/12/delayed-allocation-and-the-zero-length-file-problem/ )
os.fsync(fh)
fp.close()
os.rename(tmpfn, fn)
os.rename(fh.name, fn)


def isSubpath(path, root):
Expand Down

0 comments on commit afba9f8

Please sign in to comment.