changeset 8364:fa901423ac23

windows: avoid deleting non-empty reparse points If a hg repository including working directory is a reparse point (directory symlinked or a junction point), then using os.removedirs will remove the reparse point erroneously. This is fixed by only removing directories if they are empty.
author Henrik Stuart <henrik.stuart@edlund.dk>
date Tue, 12 May 2009 15:50:44 +0200
parents c8e81f557da7
children 94e91205d9b6
files mercurial/windows.py
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/windows.py	Mon May 11 21:12:40 2009 +0200
+++ b/mercurial/windows.py	Tue May 12 15:50:44 2009 +0200
@@ -240,6 +240,33 @@
     If gid is None, return the name of the current group."""
     return None
 
+def _removedirs(name):
+    """special version of os.removedirs that does not remove symlinked
+    directories or junction points if they actually contain files"""
+    if osutil.listdir(name):
+        return
+    os.rmdir(name)
+    head, tail = os.path.split(name)
+    if not tail:
+        head, tail = os.path.split(head)
+    while head and tail:
+        try:
+            if osutil.listdir(name):
+                return
+            os.rmdir(head)
+        except:
+            break
+        head, tail = os.path.split(head)
+
+def unlink(f):
+    """unlink and remove the directory if it is empty"""
+    os.unlink(f)
+    # try removing directories that might now be empty
+    try:
+        _removedirs(os.path.dirname(f))
+    except OSError:
+        pass
+
 try:
     # override functions with win32 versions if possible
     from win32 import *