/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Robert Collins
  • Date: 2006-06-09 11:05:14 UTC
  • mto: This revision was merged to the branch mainline in revision 1912.
  • Revision ID: robertc@robertcollins.net-20060609110514-f384a18e6be3b4fa
Tune the time to build our kernel_like tree : make LocalTransport.put faster, AtomicFile faster, LocalTransport.append faster.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005 by Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
import codecs
 
19
import errno
 
20
import os
 
21
import socket
 
22
import sys
 
23
from warnings import warn
 
24
 
 
25
from bzrlib.osutils import rename
 
26
 
 
27
# not forksafe - but we dont fork.
 
28
_pid = os.getpid()
 
29
 
 
30
class AtomicFile(object):
 
31
    """A file that does an atomic-rename to move into place.
 
32
 
 
33
    This also causes hardlinks to break when it's written out.
 
34
 
 
35
    Open this as for a regular file, then use commit() to move into
 
36
    place or abort() to cancel.
 
37
 
 
38
    An encoding can be specified; otherwise the default is ascii.
 
39
    """
 
40
 
 
41
    __slots__ = ['f', 'tmpfilename', 'realfilename', 'write']
 
42
 
 
43
    def __init__(self, filename, mode='wb', new_mode=0666):
 
44
        self.f = None
 
45
        assert mode in ('wb', 'wt'), \
 
46
            "invalid AtomicFile mode %r" % mode
 
47
 
 
48
        # old version:
 
49
        #self.tmpfilename = '%s.%d.%s.tmp' % (filename, os.getpid(),
 
50
        #                                     socket.gethostname())
 
51
        # new version:
 
52
        # This is 'broken' on NFS: it wmay collide with another NFS client.
 
53
        # however, we use this to write files within a directory that we have
 
54
        # locked, so it being racy on NFS is not a concern. The only other
 
55
        # files we use this for are .bzr.ignore, which can race anyhow.
 
56
        self.tmpfilename = '%s.%d.tmp' % (filename, _pid)
 
57
        self.realfilename = filename
 
58
        
 
59
        # Use a low level fd operation to avoid chmodding later.
 
60
        fd = os.open(self.tmpfilename, os.O_EXCL | os.O_CREAT | os.O_WRONLY,
 
61
            new_mode)
 
62
        # open a normal python file to get the text vs binary support needed
 
63
        # for windows.
 
64
        try:
 
65
            self.f = os.fdopen(fd, mode)
 
66
        except:
 
67
            os.close(fd)
 
68
            raise
 
69
        self.write = self.f.write
 
70
 
 
71
    def __repr__(self):
 
72
        return '%s(%r)' % (self.__class__.__name__,
 
73
                           self.realfilename)
 
74
 
 
75
    def commit(self):
 
76
        """Close the file and move to final name."""
 
77
        
 
78
        if self.f is None:
 
79
            raise Exception('%r is already closed' % self)
 
80
 
 
81
        f = self.f
 
82
        self.f = None
 
83
        f.close()
 
84
        rename(self.tmpfilename, self.realfilename)
 
85
 
 
86
    def abort(self):
 
87
        """Discard temporary file without committing changes."""
 
88
 
 
89
        if self.f is None:
 
90
            raise Exception('%r is already closed' % self)
 
91
 
 
92
        f = self.f
 
93
        self.f = None
 
94
        f.close()
 
95
        os.remove(self.tmpfilename)
 
96
 
 
97
    def close(self):
 
98
        """Discard the file unless already committed."""
 
99
        if self.f is not None:
 
100
            self.abort()
 
101
 
 
102
    def __del__(self):
 
103
        if self.f is not None:
 
104
            warn("%r leaked" % self)