clone: fix race with same target directory (issue716)
authorMatt Mackall <mpm@selenic.com>
Sun, 02 Dec 2007 13:37:30 -0600
changeset 5569 9e209193f18d
parent 5568 de620356064f
child 5570 78a6b985882f
clone: fix race with same target directory (issue716) Whichever side creates .hg first wins the race, and the other aborts politely without deleting the tree.
mercurial/hg.py
--- a/mercurial/hg.py	Sat Dec 01 13:09:27 2007 -0600
+++ b/mercurial/hg.py	Sun Dec 02 13:37:30 2007 -0600
@@ -173,8 +173,15 @@
             src_store = os.path.realpath(src_repo.spath)
             if not os.path.exists(dest):
                 os.mkdir(dest)
-            dest_path = os.path.realpath(os.path.join(dest, ".hg"))
-            os.mkdir(dest_path)
+            try:
+                dest_path = os.path.realpath(os.path.join(dest, ".hg"))
+                os.mkdir(dest_path)
+            except OSError, inst:
+                if inst.errno == errno.EEXIST:
+                    dir_cleanup.close()
+                    raise util.Abort(_("destination '%s' already exists")
+                                     % dest)
+                raise
             if src_repo.spath != src_repo.path:
                 # XXX racy
                 dummy_changelog = os.path.join(dest_path, "00changelog.i")
@@ -203,7 +210,14 @@
             dest_repo = repository(ui, dest)
 
         else:
-            dest_repo = repository(ui, dest, create=True)
+            try:
+                dest_repo = repository(ui, dest, create=True)
+            except OSError, inst:
+                if inst.errno == errno.EEXIST:
+                    dir_cleanup.close()
+                    raise util.Abort(_("destination '%s' already exists")
+                                     % dest)
+                raise
 
             revs = None
             if rev: