~bzr-pqm/bzr/bzr.dev

1553.5.39 by Martin Pool
More lock docs
1
# Copyright (C) 2005, 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
577 by Martin Pool
- merge portable lock module from John
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
577 by Martin Pool
- merge portable lock module from John
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
577 by Martin Pool
- merge portable lock module from John
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
1553.5.39 by Martin Pool
More lock docs
18
"""Locking using OS file locks or file existence.
577 by Martin Pool
- merge portable lock module from John
19
1553.5.46 by Martin Pool
doc
20
Note: This method of locking is generally deprecated in favour of LockDir, but
21
is used to lock local WorkingTrees, and by some old formats.  It's accessed
22
through Transport.lock_read(), etc.
577 by Martin Pool
- merge portable lock module from John
23
24
This module causes two methods, lock() and unlock() to be defined in
25
any way that works on the current platform.
26
27
It is not specified whether these locks are reentrant (i.e. can be
28
taken repeatedly by a single process) or whether they exclude
1553.5.39 by Martin Pool
More lock docs
29
different threads in a single process.  That reentrancy is provided by 
30
LockableFiles.
615 by Martin Pool
Major rework of locking code:
31
32
This defines two classes: ReadLock and WriteLock, which can be
33
implemented in different ways on different platforms.  Both have an
34
unlock() method.
614 by Martin Pool
- unify two defintions of LockError
35
"""
577 by Martin Pool
- merge portable lock module from John
36
1185.65.29 by Robert Collins
Implement final review suggestions.
37
import errno
38
import os
656 by Martin Pool
- create branch lock files if they don't exist
39
import sys
577 by Martin Pool
- merge portable lock module from John
40
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
41
from bzrlib.errors import LockError
42
from bzrlib.osutils import realpath
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
43
from bzrlib.trace import mutter
1711.8.1 by John Arbash Meinel
Branch.lock_read/lock_write/unlock should handle failures
44
577 by Martin Pool
- merge portable lock module from John
45
615 by Martin Pool
Major rework of locking code:
46
class _base_Lock(object):
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
47
615 by Martin Pool
Major rework of locking code:
48
    def _open(self, filename, filemode):
656 by Martin Pool
- create branch lock files if they don't exist
49
        try:
50
            self.f = open(filename, filemode)
51
            return self.f
52
        except IOError, e:
53
            if e.errno != errno.ENOENT:
54
                raise
55
56
            # maybe this is an old branch (before may 2005)
1185.31.4 by John Arbash Meinel
Fixing mutter() calls to not have to do string processing.
57
            mutter("trying to create missing branch lock %r", filename)
656 by Martin Pool
- create branch lock files if they don't exist
58
            
1185.65.29 by Robert Collins
Implement final review suggestions.
59
            self.f = open(filename, 'wb+')
656 by Martin Pool
- create branch lock files if they don't exist
60
            return self.f
61
615 by Martin Pool
Major rework of locking code:
62
    def __del__(self):
63
        if self.f:
64
            from warnings import warn
65
            warn("lock on %r not released" % self.f)
66
            self.unlock()
656 by Martin Pool
- create branch lock files if they don't exist
67
            
615 by Martin Pool
Major rework of locking code:
68
    def unlock(self):
69
        raise NotImplementedError()
70
71
72
############################################################
73
# msvcrt locks
74
75
577 by Martin Pool
- merge portable lock module from John
76
try:
77
    import fcntl
615 by Martin Pool
Major rework of locking code:
78
79
    class _fcntl_FileLock(_base_Lock):
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
80
615 by Martin Pool
Major rework of locking code:
81
        f = None
82
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
83
        def _unlock(self):
1185.9.2 by Harald Meland
Use fcntl.lockf() rather than fcntl.flock() to support NFS file systems.
84
            fcntl.lockf(self.f, fcntl.LOCK_UN)
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
85
            self._clear_f()
86
87
        def _clear_f(self):
88
            """Clear the self.f attribute cleanly."""
615 by Martin Pool
Major rework of locking code:
89
            self.f.close()
90
            del self.f 
91
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
92
615 by Martin Pool
Major rework of locking code:
93
    class _fcntl_WriteLock(_fcntl_FileLock):
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
94
95
        open_locks = {}
96
615 by Martin Pool
Major rework of locking code:
97
        def __init__(self, filename):
1185.65.29 by Robert Collins
Implement final review suggestions.
98
            # standard IO errors get exposed directly.
99
            self._open(filename, 'wb')
615 by Martin Pool
Major rework of locking code:
100
            try:
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
101
                self.filename = realpath(filename)
102
                if self.filename in self.open_locks:
103
                    self._clear_f() 
104
                    raise LockError("Lock already held.")
105
                # reserve a slot for this lock - even if the lockf call fails, 
106
                # at thisi point unlock() will be called, because self.f is set.
107
                # TODO: make this fully threadsafe, if we decide we care.
108
                self.open_locks[self.filename] = self.filename
1185.65.29 by Robert Collins
Implement final review suggestions.
109
                fcntl.lockf(self.f, fcntl.LOCK_EX)
110
            except IOError, e:
111
                # we should be more precise about whats a locking
112
                # error and whats a random-other error
615 by Martin Pool
Major rework of locking code:
113
                raise LockError(e)
114
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
115
        def unlock(self):
116
            del self.open_locks[self.filename]
117
            self._unlock()
118
119
615 by Martin Pool
Major rework of locking code:
120
    class _fcntl_ReadLock(_fcntl_FileLock):
1185.65.29 by Robert Collins
Implement final review suggestions.
121
615 by Martin Pool
Major rework of locking code:
122
        def __init__(self, filename):
1185.65.29 by Robert Collins
Implement final review suggestions.
123
            # standard IO errors get exposed directly.
124
            self._open(filename, 'rb')
615 by Martin Pool
Major rework of locking code:
125
            try:
1185.65.29 by Robert Collins
Implement final review suggestions.
126
                fcntl.lockf(self.f, fcntl.LOCK_SH)
127
            except IOError, e:
128
                # we should be more precise about whats a locking
129
                # error and whats a random-other error
615 by Martin Pool
Major rework of locking code:
130
                raise LockError(e)
131
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
132
        def unlock(self):
133
            self._unlock()
134
135
615 by Martin Pool
Major rework of locking code:
136
    WriteLock = _fcntl_WriteLock
137
    ReadLock = _fcntl_ReadLock
577 by Martin Pool
- merge portable lock module from John
138
1185.65.29 by Robert Collins
Implement final review suggestions.
139
577 by Martin Pool
- merge portable lock module from John
140
except ImportError:
141
    try:
142
        import win32con, win32file, pywintypes
615 by Martin Pool
Major rework of locking code:
143
144
1185.23.3 by Aaron Bentley
win32 locking fix from Belchenko
145
        LOCK_SH = 0 # the default
146
        LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
147
        LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
615 by Martin Pool
Major rework of locking code:
148
149
        class _w32c_FileLock(_base_Lock):
150
            def _lock(self, filename, openmode, lockmode):
151
                try:
152
                    self._open(filename, openmode)
153
                    self.hfile = win32file._get_osfhandle(self.f.fileno())
154
                    overlapped = pywintypes.OVERLAPPED()
155
                    win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
156
                except Exception, e:
1711.7.31 by John Arbash Meinel
Clean up win32 locks when they fail to lock, so we don't try to unlock them later.
157
                    if self.f:
158
                        self.f.close()
159
                        self.f = None
615 by Martin Pool
Major rework of locking code:
160
                    raise LockError(e)
161
162
            def unlock(self):
163
                try:
164
                    overlapped = pywintypes.OVERLAPPED()
165
                    win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
166
                    self.f.close()
167
                    self.f = None
168
                except Exception, e:
169
                    raise LockError(e)
170
171
172
        class _w32c_ReadLock(_w32c_FileLock):
173
            def __init__(self, filename):
1185.23.3 by Aaron Bentley
win32 locking fix from Belchenko
174
                _w32c_FileLock._lock(self, filename, 'rb',
175
                                     LOCK_NB)
615 by Martin Pool
Major rework of locking code:
176
177
        class _w32c_WriteLock(_w32c_FileLock):
178
            def __init__(self, filename):
179
                _w32c_FileLock._lock(self, filename, 'wb',
1185.23.3 by Aaron Bentley
win32 locking fix from Belchenko
180
                                     LOCK_EX + LOCK_NB)
615 by Martin Pool
Major rework of locking code:
181
182
183
        WriteLock = _w32c_WriteLock
184
        ReadLock = _w32c_ReadLock
185
577 by Martin Pool
- merge portable lock module from John
186
    except ImportError:
187
        try:
188
            import msvcrt
615 by Martin Pool
Major rework of locking code:
189
190
577 by Martin Pool
- merge portable lock module from John
191
            # Unfortunately, msvcrt.locking() doesn't distinguish between
192
            # read locks and write locks. Also, the way the combinations
193
            # work to get non-blocking is not the same, so we
194
            # have to write extra special functions here.
195
615 by Martin Pool
Major rework of locking code:
196
197
            class _msvc_FileLock(_base_Lock):
198
                LOCK_SH = 1
199
                LOCK_EX = 2
200
                LOCK_NB = 4
201
                def unlock(self):
202
                    _msvc_unlock(self.f)
1185.7.1 by Martin Pool
- Quieten warnings about locking; patch from Matt Lavin.
203
                    self.f.close()
204
                    self.f = None
615 by Martin Pool
Major rework of locking code:
205
206
207
            class _msvc_ReadLock(_msvc_FileLock):
208
                def __init__(self, filename):
209
                    _msvc_lock(self._open(filename, 'rb'), self.LOCK_SH)
210
211
212
            class _msvc_WriteLock(_msvc_FileLock):
213
                def __init__(self, filename):
214
                    _msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
215
216
217
            def _msvc_lock(f, flags):
577 by Martin Pool
- merge portable lock module from John
218
                try:
219
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
220
                    # according to the comments, LK_RLCK is open the lock for writing.
221
222
                    # Unfortunately, msvcrt.locking() also has the side effect that it
223
                    # will only block for 10 seconds at most, and then it will throw an
224
                    # exception, this isn't terrible, though.
225
                    if type(f) == file:
226
                        fpos = f.tell()
227
                        fn = f.fileno()
228
                        f.seek(0)
229
                    else:
230
                        fn = f
231
                        fpos = os.lseek(fn, 0,0)
232
                        os.lseek(fn, 0,0)
615 by Martin Pool
Major rework of locking code:
233
951 by Martin Pool
- fix up self reference in msvc lock object
234
                    if flags & _msvc_FileLock.LOCK_SH:
235
                        if flags & _msvc_FileLock.LOCK_NB:
577 by Martin Pool
- merge portable lock module from John
236
                            lock_mode = msvcrt.LK_NBLCK
237
                        else:
238
                            lock_mode = msvcrt.LK_LOCK
951 by Martin Pool
- fix up self reference in msvc lock object
239
                    elif flags & _msvc_FileLock.LOCK_EX:
240
                        if flags & _msvc_FileLock.LOCK_NB:
577 by Martin Pool
- merge portable lock module from John
241
                            lock_mode = msvcrt.LK_NBRLCK
242
                        else:
243
                            lock_mode = msvcrt.LK_RLCK
244
                    else:
245
                        raise ValueError('Invalid lock mode: %r' % flags)
246
                    try:
247
                        msvcrt.locking(fn, lock_mode, -1)
248
                    finally:
249
                        os.lseek(fn, fpos, 0)
250
                except Exception, e:
251
                    raise LockError(e)
252
615 by Martin Pool
Major rework of locking code:
253
            def _msvc_unlock(f):
577 by Martin Pool
- merge portable lock module from John
254
                try:
255
                    if type(f) == file:
256
                        fpos = f.tell()
257
                        fn = f.fileno()
258
                        f.seek(0)
259
                    else:
260
                        fn = f
261
                        fpos = os.lseek(fn, 0,0)
262
                        os.lseek(fn, 0,0)
263
264
                    try:
265
                        msvcrt.locking(fn, msvcrt.LK_UNLCK, -1)
266
                    finally:
267
                        os.lseek(fn, fpos, 0)
268
                except Exception, e:
269
                    raise LockError(e)
615 by Martin Pool
Major rework of locking code:
270
271
272
            WriteLock = _msvc_WriteLock
273
            ReadLock = _msvc_ReadLock
577 by Martin Pool
- merge portable lock module from John
274
        except ImportError:
615 by Martin Pool
Major rework of locking code:
275
            raise NotImplementedError("please write a locking method "
276
                                      "for platform %r" % sys.platform)