~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright (C) 2004, 2005 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


import os
import sys

from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import stat
import socket
import warnings

from bzrlib import (
    errors,
    osutils,
    symbol_versioning,
    )
""")

# not forksafe - but we dont fork.
_pid = os.getpid()
_hostname = None


class AtomicFile(object):
    """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.
    """

    __slots__ = ['tmpfilename', 'realfilename', '_fd']

    def __init__(self, filename, mode='wb', new_mode=None):
        global _hostname

        self._fd = None

        if _hostname is None:
            _hostname = osutils.get_host_name()

        self.tmpfilename = '%s.%d.%s.%s.tmp' % (filename, _pid, _hostname,
                                                osutils.rand_chars(10))

        self.realfilename = filename

        flags = os.O_EXCL | os.O_CREAT | os.O_WRONLY
        if mode == 'wb':
            flags |= osutils.O_BINARY
        elif mode != 'wt':
            raise ValueError("invalid AtomicFile mode %r" % mode)

        if new_mode is not None:
            local_mode = new_mode
        else:
            local_mode = 0666

        # Use a low level fd operation to avoid chmodding later.
        # This may not succeed, but it should help most of the time
        self._fd = os.open(self.tmpfilename, flags, local_mode)

        if new_mode is not None:
            # Because of umask issues, we may need to chmod anyway
            # the common case is that we won't, though.
            st = os.fstat(self._fd)
            if stat.S_IMODE(st.st_mode) != new_mode:
                os.chmod(self.tmpfilename, new_mode)

    def _get_closed(self):
        symbol_versioning.warn('AtomicFile.closed deprecated in bzr 0.10',
                               DeprecationWarning, stacklevel=2)
        return self._fd is None

    closed = property(_get_closed)

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__,
                           self.realfilename)

    def write(self, data):
        """Write some data to the file. Like file.write()"""
        os.write(self._fd, data)

    def _close_tmpfile(self, func_name):
        """Close the local temp file in preparation for commit or abort"""
        if self._fd is None:
            raise errors.AtomicFileAlreadyClosed(path=self.realfilename,
                                                 function=func_name)
        fd = self._fd
        self._fd = None
        os.close(fd)

    def commit(self):
        """Close the file and move to final name."""
        self._close_tmpfile('commit')
        osutils.rename(self.tmpfilename, self.realfilename)

    def abort(self):
        """Discard temporary file without committing changes."""
        self._close_tmpfile('abort')
        os.remove(self.tmpfilename)

    def close(self):
        """Discard the file unless already committed."""
        if self._fd is not None:
            self.abort()

    def __del__(self):
        if self._fd is not None:
            warnings.warn("%r leaked" % self)