fix a bug where hg could remove file ending with .tmp
authorBenoit Boissinot <benoit.boissinot@ens-lyon.org>
Fri, 28 Oct 2005 17:18:50 -0700
changeset 1479 1a3c6689ef2b
parent 1478 e6dd91a88b57
child 1480 ae0d8d632b83
fix a bug where hg could remove file ending with .tmp util.opener used a fixed filename for writing tempfile instead of using the tempfile module.
mercurial/util.py
--- a/mercurial/util.py	Fri Oct 28 11:03:18 2005 -0700
+++ b/mercurial/util.py	Fri Oct 28 17:18:50 2005 -0700
@@ -377,8 +377,17 @@
                     os.makedirs(d)
             else:
                 if nlink > 1:
-                    file(f + ".tmp", "wb").write(file(f, "rb").read())
-                    rename(f+".tmp", f)
+                    d, fn = os.path.split(f)
+                    fd, temp = tempfile.mkstemp(prefix=fn, dir=d)
+                    fp = os.fdopen(fd, "wb")
+                    try:
+                        fp.write(file(f, "rb").read())
+                    except:
+                        try: os.unlink(temp)
+                        except: pass
+                        raise
+                    fp.close()
+                    rename(temp, f)
 
         return file(f, mode)