/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: John Arbash Meinel
  • Date: 2006-08-08 23:48:44 UTC
  • mfrom: (1910 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1912.
  • Revision ID: john@arbash-meinel.com-20060808234844-602f87fbb7bbd2fe
[merge] bzr.dev 1910

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__ = ['closed', '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
 
 
58
        self.realfilename = filename
 
59
        
 
60
        # Use a low level fd operation to avoid chmodding later.
 
61
        fd = os.open(self.tmpfilename, os.O_EXCL | os.O_CREAT | os.O_WRONLY,
 
62
            new_mode)
 
63
        # open a normal python file to get the text vs binary support needed
 
64
        # for windows.
 
65
        self.closed = False
 
66
        try:
 
67
            self.f = os.fdopen(fd, mode)
 
68
        except:
 
69
            os.close(fd)
 
70
            self.closed = True
 
71
            raise
 
72
        self.write = self.f.write
 
73
 
 
74
    def __repr__(self):
 
75
        return '%s(%r)' % (self.__class__.__name__,
 
76
                           self.realfilename)
 
77
 
 
78
    def commit(self):
 
79
        """Close the file and move to final name."""
 
80
        if self.closed:
 
81
            raise Exception('%r is already closed' % self)
 
82
 
 
83
        f = self.f
 
84
        self.f = None
 
85
        f.close()
 
86
        rename(self.tmpfilename, self.realfilename)
 
87
 
 
88
    def abort(self):
 
89
        """Discard temporary file without committing changes."""
 
90
 
 
91
        if self.f is None:
 
92
            raise Exception('%r is already closed' % self)
 
93
 
 
94
        f = self.f
 
95
        self.f = None
 
96
        f.close()
 
97
        os.remove(self.tmpfilename)
 
98
 
 
99
    def close(self):
 
100
        """Discard the file unless already committed."""
 
101
        if self.f is not None:
 
102
            self.abort()
 
103
 
 
104
    def __del__(self):
 
105
        if self.f is not None:
 
106
            warn("%r leaked" % self)