changeset 3999:0b740dcf0cf1

symlinks: add basic symlink functions to util.py
author Matt Mackall <mpm@selenic.com>
date Fri, 29 Dec 2006 20:04:31 -0600
parents 315d47991fd4
children 3297aa945cf2
files mercurial/util.py
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Fri Dec 29 20:04:31 2006 -0600
+++ b/mercurial/util.py	Fri Dec 29 20:04:31 2006 -0600
@@ -787,6 +787,9 @@
     def set_exec(f, mode):
         pass
 
+    def set_link(f, mode):
+        pass
+
     def set_binary(fd):
         msvcrt.setmode(fd.fileno(), os.O_BINARY)
 
@@ -873,6 +876,30 @@
         else:
             os.chmod(f, s & 0666)
 
+    def is_link(f):
+        """check whether a file is a symlink"""
+        return (os.lstat(f).st_mode & 0120000 == 0120000)
+
+    def set_link(f, mode):
+        """make a file a symbolic link/regular file
+
+        if a file is changed to a link, its contents become the link data
+        if a link is changed to a file, its link data become its contents
+        """
+
+        m = is_link(f)
+        if m == bool(mode):
+            return
+
+        if mode: # switch file to link
+            data = file(f).read()
+            os.unlink(f)
+            os.symlink(data, f)
+        else:
+            data = os.readlink(f)
+            os.unlink(f)
+            file(f, "w").write(data)
+
     def set_binary(fd):
         pass