From: Brian Warner Date: Thu, 19 Feb 2009 06:13:10 +0000 (-0700) Subject: fileutil: add move_into_place(), to perform the standard unix trick of atomically... X-Git-Tag: allmydata-tahoe-1.4.0~188 X-Git-Url: https://git.rkrishnan.org/architecture.txt?a=commitdiff_plain;h=89f041ac83de5ec258d953f42cf52f401d4f9890;p=tahoe-lafs%2Ftahoe-lafs.git fileutil: add move_into_place(), to perform the standard unix trick of atomically replacing a file, with a fallback for windows --- diff --git a/src/allmydata/util/fileutil.py b/src/allmydata/util/fileutil.py index d8b084cf..3e33222a 100644 --- a/src/allmydata/util/fileutil.py +++ b/src/allmydata/util/fileutil.py @@ -10,7 +10,7 @@ Futz with files like a pro. """ -import exceptions, os, stat, tempfile, time +import sys, exceptions, os, stat, tempfile, time from twisted.python import log @@ -195,3 +195,10 @@ def du(basedir): size += os.path.getsize(fn) return size + +def move_into_place(source, dest): + """Atomically replace a file, or as near to it as the platform allows. + The dest file may or may not exist.""" + if "win32" in sys.platform.lower(): + remove_if_possible(dest) + os.rename(source, dest)