From f5481621f85a494f9597a076f38a132c525a670f Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 11 Sep 2012 17:53:43 -0400 Subject: [PATCH 1/3] made copy atomic by creating tmp file in dest location (ensures same partition) uses pid and epoch to prevent collisions, good enough for most cases Signed-off-by: Brian Coca --- library/copy | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/copy b/library/copy index b4da56c9797..beda6652ba3 100755 --- a/library/copy +++ b/library/copy @@ -20,6 +20,7 @@ import os import shutil +import time def main(): @@ -64,7 +65,11 @@ def main(): if backup: if os.path.exists(dest): backup_file = module.backup_local(dest) - shutil.copyfile(src, dest) + #TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs? + # might be an issue with exceeding path length + dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.gmtime) + shutil.copyfile(src, dest_tmp) + shutil.copyfile(dest_tmp, dest) except shutil.Error: module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest)) except IOError: From 06b914c5b35bebd07ac2be216bf231fc774beba4 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 12 Sep 2012 14:47:06 -0400 Subject: [PATCH 2/3] small fix to prevent temp file from living past its succesful usage Signed-off-by: Brian Coca --- library/copy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/copy b/library/copy index beda6652ba3..b9506aac12f 100755 --- a/library/copy +++ b/library/copy @@ -67,9 +67,9 @@ def main(): backup_file = module.backup_local(dest) #TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs? # might be an issue with exceeding path length - dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.gmtime) + dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.gmtime()) shutil.copyfile(src, dest_tmp) - shutil.copyfile(dest_tmp, dest) + shutil.move(dest_tmp, dest) except shutil.Error: module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest)) except IOError: From 88d1285f33e532de34c1265fab40f5175937e6dc Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 12 Sep 2012 14:52:54 -0400 Subject: [PATCH 3/3] time is what i wanted, not gmtime Signed-off-by: Brian Coca --- library/copy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/copy b/library/copy index b9506aac12f..6491f6ea5e7 100755 --- a/library/copy +++ b/library/copy @@ -67,7 +67,7 @@ def main(): backup_file = module.backup_local(dest) #TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs? # might be an issue with exceeding path length - dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.gmtime()) + dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.time()) shutil.copyfile(src, dest_tmp) shutil.move(dest_tmp, dest) except shutil.Error: