Rev 2569: (Elliot Murphy) Use os.lstat rather than os.stat for osutils.make_readonly/make_writeable in http://bzr.arbash-meinel.com/branches/bzr/jam-integration

John Arbash Meinel john at arbash-meinel.com
Mon Jul 2 15:24:47 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/jam-integration

------------------------------------------------------------
revno: 2569
revision-id: john at arbash-meinel.com-20070702142427-egq5thf8lj6fh9ls
parent: pqm at pqm.ubuntu.com-20070702070639-um9oyfoc2i6g8umv
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: jam-integration
timestamp: Mon 2007-07-02 09:24:27 -0500
message:
  (Elliot Murphy) Use os.lstat rather than os.stat for osutils.make_readonly/make_writeable
modified:
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
-------------- next part --------------
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2007-06-18 01:56:32 +0000
+++ b/bzrlib/osutils.py	2007-07-02 14:24:27 +0000
@@ -69,18 +69,23 @@
 # OR with 0 on those platforms
 O_BINARY = getattr(os, 'O_BINARY', 0)
 
+# On posix, use lstat instead of stat so that we can
+# operate on broken symlinks. On Windows revert to stat.
+lstat = getattr(os, 'lstat', os.stat)
 
 def make_readonly(filename):
     """Make a filename read-only."""
-    mod = os.stat(filename).st_mode
-    mod = mod & 0777555
-    os.chmod(filename, mod)
+    mod = lstat(filename).st_mode
+    if not stat.S_ISLNK(mod):
+        mod = mod & 0777555
+        os.chmod(filename, mod)
 
 
 def make_writable(filename):
-    mod = os.stat(filename).st_mode
-    mod = mod | 0200
-    os.chmod(filename, mod)
+    mod = lstat(filename).st_mode
+    if not stat.S_ISLNK(mod):
+        mod = mod | 0200
+        os.chmod(filename, mod)
 
 
 _QUOTE_RE = None

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2007-04-16 21:12:29 +0000
+++ b/bzrlib/tests/test_osutils.py	2007-07-02 14:24:27 +0000
@@ -247,6 +247,28 @@
         self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
 
 
+    def test_changing_access(self):
+        f = file('file', 'w')
+        f.write('monkey')
+        f.close()
+
+        # Make a file readonly
+        osutils.make_readonly('file')
+        mode = osutils.lstat('file').st_mode
+        self.assertEqual(mode, mode & 0777555)
+
+        # Make a file writable
+        osutils.make_writable('file')
+        mode = osutils.lstat('file').st_mode
+        self.assertEqual(mode, mode | 0200)
+
+        if osutils.has_symlinks():
+            # should not error when handed a symlink
+            os.symlink('nonexistent', 'dangling')
+            osutils.make_readonly('dangling')
+            osutils.make_writable('dangling')
+
+
     def test_kind_marker(self):
         self.assertEqual("", osutils.kind_marker("file"))
         self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))



More information about the bazaar-commits mailing list