[Pkg-bazaar-commits] ./bzr/unstable r409: - New AtomicFile class

Martin Pool mbp at sourcefrog.net
Fri Apr 10 07:43:39 UTC 2009


------------------------------------------------------------
revno: 409
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Mon 2005-05-09 14:50:11 +1000
message:
  - New AtomicFile class
  - bzr ignore: Write out ignore list using AtomicFile to break hardlinks
added:
  bzrlib/atomicfile.py
modified:
  NEWS
  bzrlib/commands.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-05-09 03:11:21 +0000
+++ b/NEWS	2005-05-09 04:50:11 +0000
@@ -60,6 +60,7 @@
 
     * bzrlib.open_tracefile() takes a tracefilename parameter.
 
+    * New AtomicFile class.
 bzr-0.0.4  2005-04-22
 
   ENHANCEMENTS:

=== added file 'bzrlib/atomicfile.py'
--- a/bzrlib/atomicfile.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/atomicfile.py	2005-05-09 04:50:11 +0000
@@ -0,0 +1,52 @@
+# Copyright (C) 2004, 2005 by Canonical Ltd
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+
+
+class AtomicFile:
+    """A file that does an atomic-rename to move into place.
+
+    This also causes hardlinks to break when it's written out.
+
+    Open this as for a regular file, then use commit() to move into
+    place or abort() to cancel.
+
+    You may wish to wrap this in a codecs.EncodedFile to do unicode
+    encoding.
+    """
+
+    def __init__(self, filename, mode='wb'):
+        if mode != 'wb' and mode != 'wt':
+            raise ValueError("invalid AtomicFile mode %r" % mode)
+
+        import os, socket
+        self.tmpfilename = '%s.tmp.%d.%s' % (filename, os.getpid(),
+                                             socket.gethostname())
+        self.realfilename = filename
+        
+        self.f = open(self.tmpfilename, mode)
+        self.write = self.f.write
+
+    def commit(self):
+        self.f.close()
+        if sys.platform == 'win32':
+            os.remove(self.realfilename)
+        os.rename(self.tmpfilename, self.realfilename)
+
+    def abort(self):
+        self.f.close()
+        os.remove(self.tmpfilename)
+        

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-05-09 03:51:55 +0000
+++ b/bzrlib/commands.py	2005-05-09 04:50:11 +0000
@@ -486,12 +486,27 @@
     takes_args = ['name_pattern']
     
     def run(self, name_pattern):
+        from bzrlib.atomicfile import AtomicFile
+        import codecs
+
         b = Branch('.')
 
-        # XXX: This will fail if it's a hardlink; should use an AtomicFile class.
-        f = open(b.abspath('.bzrignore'), 'at')
-        f.write(name_pattern + '\n')
-        f.close()
+        # FIXME: probably doesn't handle non-ascii patterns
+
+        if os.path.exists(b.controlfilename('.bzrignore')):
+            f = b.controlfile('.bzrignore', 'rt')
+            igns = f.read()
+            f.close()
+        else:
+            igns = ''
+
+        if igns and igns[-1] != '\n':
+            igns += '\n'
+        igns += name_pattern + '\n'
+
+        f = AtomicFile(b.controlfilename('.bzrignore'), 'wt')
+        f.write(igns)
+        f.commit()
 
         inv = b.working_tree().inventory
         if inv.path2id('.bzrignore'):



More information about the Pkg-bazaar-commits mailing list