When atomically replacing files, copy attributes

This commit is contained in:
Daniel Hokka Zakrisson 2012-10-25 20:50:41 +02:00 committed by Michael DeHaan
parent d55e7f35cd
commit 447ea1624a
2 changed files with 2 additions and 8 deletions

View file

@ -125,13 +125,7 @@ def writekeys(module, filename, keys):
except IOError, e:
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
f.close()
try:
shutil.copyfile(tmp_path, filename)
os.unlink(tmp_path)
except IOError, e:
module.fail_json(msg="Failed to copy temp file to %s: %s" % (filename, str(e)))
except OSError, e:
module.fail_json(msg="Failed to remove temp file: %s" % str(e))
module.atomic_replace(tmp_path, filename)
def enforce_state(module, params):
"""

2
copy
View file

@ -114,7 +114,7 @@ def main():
# might be an issue with exceeding path length
dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.time())
shutil.copyfile(src, dest_tmp)
shutil.move(dest_tmp, dest)
module.atomic_replace(dest_tmp, dest)
except shutil.Error:
module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest))
except IOError: