/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/lock.py

  • Committer: Martin Pool
  • Date: 2005-05-31 03:03:54 UTC
  • Revision ID: mbp@sourcefrog.net-20050531030354-561dbe9ec2862d46
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
"""Locking using OS file locks or file existence.
 
18
"""Locking wrappers.
19
19
 
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.
 
20
This only does local locking using OS locks for now.
23
21
 
24
22
This module causes two methods, lock() and unlock() to be defined in
25
23
any way that works on the current platform.
26
24
 
27
25
It is not specified whether these locks are reentrant (i.e. can be
28
26
taken repeatedly by a single process) or whether they exclude
29
 
different threads in a single process.  That reentrancy is provided by 
30
 
LockableFiles.
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.
35
 
"""
36
 
 
37
 
import errno
38
 
import os
39
 
import sys
40
 
 
41
 
from bzrlib.trace import mutter, note, warning
42
 
from bzrlib.errors import LockError
43
 
 
44
 
class _base_Lock(object):
45
 
    def _open(self, filename, filemode):
46
 
        try:
47
 
            self.f = open(filename, filemode)
48
 
            return self.f
49
 
        except IOError, e:
50
 
            if e.errno != errno.ENOENT:
51
 
                raise
52
 
 
53
 
            # maybe this is an old branch (before may 2005)
54
 
            mutter("trying to create missing branch lock %r", filename)
55
 
            
56
 
            self.f = open(filename, 'wb+')
57
 
            return self.f
58
 
 
59
 
    def __del__(self):
60
 
        if self.f:
61
 
            from warnings import warn
62
 
            warn("lock on %r not released" % self.f)
63
 
            self.unlock()
64
 
            
65
 
    def unlock(self):
66
 
        raise NotImplementedError()
67
 
 
68
 
        
69
 
 
70
 
 
71
 
 
72
 
 
73
 
############################################################
74
 
# msvcrt locks
75
 
 
 
27
different threads in a single process.  
 
28
 
 
29
Eventually we may need to use some kind of lock representation that
 
30
will work on a dumb filesystem without actual locking primitives."""
 
31
 
 
32
 
 
33
import sys, os
 
34
 
 
35
import bzrlib
 
36
from trace import mutter, note, warning
 
37
 
 
38
class LockError(Exception):
 
39
    """All exceptions from the lock/unlock functions should be from this exception class.
 
40
    They will be translated as necessary. The original exception is available as e.original_error
 
41
    """
 
42
    def __init__(self, e=None):
 
43
        self.original_error = e
 
44
        if e:
 
45
            Exception.__init__(self, e)
 
46
        else:
 
47
            Exception.__init__(self)
76
48
 
77
49
try:
78
50
    import fcntl
79
 
 
80
 
    class _fcntl_FileLock(_base_Lock):
81
 
        f = None
82
 
 
83
 
        def unlock(self):
84
 
            fcntl.lockf(self.f, fcntl.LOCK_UN)
85
 
            self.f.close()
86
 
            del self.f 
87
 
 
88
 
    class _fcntl_WriteLock(_fcntl_FileLock):
89
 
        def __init__(self, filename):
90
 
            # standard IO errors get exposed directly.
91
 
            self._open(filename, 'wb')
92
 
            try:
93
 
                fcntl.lockf(self.f, fcntl.LOCK_EX)
94
 
            except IOError, e:
95
 
                # we should be more precise about whats a locking
96
 
                # error and whats a random-other error
97
 
                raise LockError(e)
98
 
 
99
 
    class _fcntl_ReadLock(_fcntl_FileLock):
100
 
 
101
 
        def __init__(self, filename):
102
 
            # standard IO errors get exposed directly.
103
 
            self._open(filename, 'rb')
104
 
            try:
105
 
                fcntl.lockf(self.f, fcntl.LOCK_SH)
106
 
            except IOError, e:
107
 
                # we should be more precise about whats a locking
108
 
                # error and whats a random-other error
109
 
                raise LockError(e)
110
 
 
111
 
    WriteLock = _fcntl_WriteLock
112
 
    ReadLock = _fcntl_ReadLock
113
 
 
 
51
    LOCK_SH = fcntl.LOCK_SH
 
52
    LOCK_EX = fcntl.LOCK_EX
 
53
    LOCK_NB = fcntl.LOCK_NB
 
54
    def lock(f, flags):
 
55
        try:
 
56
            fcntl.flock(f, flags)
 
57
        except Exception, e:
 
58
            raise LockError(e)
 
59
 
 
60
    def unlock(f):
 
61
        try:
 
62
            fcntl.flock(f, fcntl.LOCK_UN)
 
63
        except Exception, e:
 
64
            raise LockError(e)
114
65
 
115
66
except ImportError:
116
67
    try:
117
68
        import win32con, win32file, pywintypes
118
 
 
119
 
 
120
69
        LOCK_SH = 0 # the default
121
70
        LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
122
71
        LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
123
72
 
124
 
        class _w32c_FileLock(_base_Lock):
125
 
            def _lock(self, filename, openmode, lockmode):
126
 
                try:
127
 
                    self._open(filename, openmode)
128
 
                    self.hfile = win32file._get_osfhandle(self.f.fileno())
129
 
                    overlapped = pywintypes.OVERLAPPED()
130
 
                    win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
131
 
                except Exception, e:
132
 
                    raise LockError(e)
133
 
 
134
 
            def unlock(self):
135
 
                try:
136
 
                    overlapped = pywintypes.OVERLAPPED()
137
 
                    win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
138
 
                    self.f.close()
139
 
                    self.f = None
140
 
                except Exception, e:
141
 
                    raise LockError(e)
142
 
 
143
 
 
144
 
 
145
 
        class _w32c_ReadLock(_w32c_FileLock):
146
 
            def __init__(self, filename):
147
 
                _w32c_FileLock._lock(self, filename, 'rb',
148
 
                                     LOCK_NB)
149
 
 
150
 
        class _w32c_WriteLock(_w32c_FileLock):
151
 
            def __init__(self, filename):
152
 
                _w32c_FileLock._lock(self, filename, 'wb',
153
 
                                     LOCK_EX + LOCK_NB)
154
 
 
155
 
 
156
 
 
157
 
        WriteLock = _w32c_WriteLock
158
 
        ReadLock = _w32c_ReadLock
159
 
 
 
73
        def lock(f, flags):
 
74
            try:
 
75
                if type(f) == file:
 
76
                    hfile = win32file._get_osfhandle(f.fileno())
 
77
                else:
 
78
                    hfile = win32file._get_osfhandle(f)
 
79
                overlapped = pywintypes.OVERLAPPED()
 
80
                win32file.LockFileEx(hfile, flags, 0, 0x7fff0000, overlapped)
 
81
            except Exception, e:
 
82
                raise LockError(e)
 
83
 
 
84
        def unlock(f):
 
85
            try:
 
86
                if type(f) == file:
 
87
                    hfile = win32file._get_osfhandle(f.fileno())
 
88
                else:
 
89
                    hfile = win32file._get_osfhandle(f)
 
90
                overlapped = pywintypes.OVERLAPPED()
 
91
                win32file.UnlockFileEx(hfile, 0, 0x7fff0000, overlapped)
 
92
            except Exception, e:
 
93
                raise LockError(e)
160
94
    except ImportError:
161
95
        try:
162
96
            import msvcrt
163
 
 
164
 
 
165
97
            # Unfortunately, msvcrt.locking() doesn't distinguish between
166
98
            # read locks and write locks. Also, the way the combinations
167
99
            # work to get non-blocking is not the same, so we
168
100
            # have to write extra special functions here.
169
101
 
170
 
 
171
 
            class _msvc_FileLock(_base_Lock):
172
 
                LOCK_SH = 1
173
 
                LOCK_EX = 2
174
 
                LOCK_NB = 4
175
 
                def unlock(self):
176
 
                    _msvc_unlock(self.f)
177
 
                    self.f.close()
178
 
                    self.f = None
179
 
 
180
 
 
181
 
            class _msvc_ReadLock(_msvc_FileLock):
182
 
                def __init__(self, filename):
183
 
                    _msvc_lock(self._open(filename, 'rb'), self.LOCK_SH)
184
 
 
185
 
 
186
 
            class _msvc_WriteLock(_msvc_FileLock):
187
 
                def __init__(self, filename):
188
 
                    _msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
189
 
 
190
 
 
191
 
 
192
 
            def _msvc_lock(f, flags):
 
102
            LOCK_SH = 1
 
103
            LOCK_EX = 2
 
104
            LOCK_NB = 4
 
105
 
 
106
            def lock(f, flags):
193
107
                try:
194
108
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
195
109
                    # according to the comments, LK_RLCK is open the lock for writing.
205
119
                        fn = f
206
120
                        fpos = os.lseek(fn, 0,0)
207
121
                        os.lseek(fn, 0,0)
208
 
 
209
 
                    if flags & _msvc_FileLock.LOCK_SH:
210
 
                        if flags & _msvc_FileLock.LOCK_NB:
 
122
                    
 
123
                    if flags & LOCK_SH:
 
124
                        if flags & LOCK_NB:
211
125
                            lock_mode = msvcrt.LK_NBLCK
212
126
                        else:
213
127
                            lock_mode = msvcrt.LK_LOCK
214
 
                    elif flags & _msvc_FileLock.LOCK_EX:
215
 
                        if flags & _msvc_FileLock.LOCK_NB:
 
128
                    elif flags & LOCK_EX:
 
129
                        if flags & LOCK_NB:
216
130
                            lock_mode = msvcrt.LK_NBRLCK
217
131
                        else:
218
132
                            lock_mode = msvcrt.LK_RLCK
225
139
                except Exception, e:
226
140
                    raise LockError(e)
227
141
 
228
 
            def _msvc_unlock(f):
 
142
            def unlock(f):
229
143
                try:
230
144
                    if type(f) == file:
231
145
                        fpos = f.tell()
242
156
                        os.lseek(fn, fpos, 0)
243
157
                except Exception, e:
244
158
                    raise LockError(e)
245
 
 
246
 
 
247
 
 
248
 
            WriteLock = _msvc_WriteLock
249
 
            ReadLock = _msvc_ReadLock
250
159
        except ImportError:
251
 
            raise NotImplementedError("please write a locking method "
252
 
                                      "for platform %r" % sys.platform)
 
160
            from warnings import Warning
 
161
            
 
162
            warning("please write a locking method for platform %r" % sys.platform)
 
163
 
 
164
            # Creating no-op lock/unlock for now
 
165
            def lock(f, flags):
 
166
                pass
 
167
            def unlock(f):
 
168
                pass
 
169