~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Martin Pool
  • Date: 2005-05-30 02:45:48 UTC
  • Revision ID: mbp@sourcefrog.net-20050530024548-120dad7e43de5fec
- rsync upload should be quieter

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
different threads in a single process.  
28
28
 
29
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
 
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
 
"""
 
30
will work on a dumb filesystem without actual locking primitives."""
36
31
 
37
32
 
38
33
import sys, os
39
34
 
 
35
import bzrlib
40
36
from trace import mutter, note, warning
41
 
from errors import LockError
42
 
 
43
 
class _base_Lock(object):
44
 
    def _open(self, filename, filemode):
45
 
        self.f = open(filename, filemode)
46
 
        return self.f
47
 
    
48
 
 
49
 
    def __del__(self):
50
 
        if self.f:
51
 
            from warnings import warn
52
 
            warn("lock on %r not released" % self.f)
53
 
            self.unlock()
54
 
 
55
 
    def unlock(self):
56
 
        raise NotImplementedError()
57
 
 
58
 
        
59
 
 
60
 
 
61
 
 
62
 
 
63
 
############################################################
64
 
# msvcrt locks
65
 
 
 
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)
66
48
 
67
49
try:
68
50
    import fcntl
69
 
 
70
 
    class _fcntl_FileLock(_base_Lock):
71
 
        f = None
72
 
 
73
 
        def unlock(self):
74
 
            fcntl.flock(self.f, fcntl.LOCK_UN)
75
 
            self.f.close()
76
 
            del self.f 
77
 
 
78
 
 
79
 
    class _fcntl_WriteLock(_fcntl_FileLock):
80
 
        def __init__(self, filename):
81
 
            try:
82
 
                fcntl.flock(self._open(filename, 'wb'), fcntl.LOCK_EX)
83
 
            except Exception, e:
84
 
                raise LockError(e)
85
 
 
86
 
 
87
 
    class _fcntl_ReadLock(_fcntl_FileLock):
88
 
        def __init__(self, filename):
89
 
            try:
90
 
                fcntl.flock(self._open(filename, 'rb'), fcntl.LOCK_SH)
91
 
            except Exception, e:
92
 
                raise LockError(e)
93
 
 
94
 
    WriteLock = _fcntl_WriteLock
95
 
    ReadLock = _fcntl_ReadLock
 
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)
96
65
 
97
66
except ImportError:
98
67
    try:
99
68
        import win32con, win32file, pywintypes
100
 
 
101
 
 
102
 
        #LOCK_SH = 0 # the default
103
 
        #LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
104
 
        #LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
105
 
 
106
 
        class _w32c_FileLock(_base_Lock):
107
 
            def _lock(self, filename, openmode, lockmode):
108
 
                try:
109
 
                    self._open(filename, openmode)
110
 
                    self.hfile = win32file._get_osfhandle(self.f.fileno())
111
 
                    overlapped = pywintypes.OVERLAPPED()
112
 
                    win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
113
 
                except Exception, e:
114
 
                    raise LockError(e)
115
 
 
116
 
            def unlock(self):
117
 
                try:
118
 
                    overlapped = pywintypes.OVERLAPPED()
119
 
                    win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
120
 
                    self.f.close()
121
 
                    self.f = None
122
 
                except Exception, e:
123
 
                    raise LockError(e)
124
 
 
125
 
 
126
 
 
127
 
        class _w32c_ReadLock(_w32c_FileLock):
128
 
            def __init__(self, filename):
129
 
                _w32c_FileLock._lock(self, filename, 'rb', 0)
130
 
 
131
 
        class _w32c_WriteLock(_w32c_FileLock):
132
 
            def __init__(self, filename):
133
 
                _w32c_FileLock._lock(self, filename, 'wb',
134
 
                                     win32con.LOCKFILE_EXCLUSIVE_LOCK)
135
 
 
136
 
 
137
 
 
138
 
        WriteLock = _w32c_WriteLock
139
 
        ReadLock = _w32c_ReadLock
140
 
 
 
69
        LOCK_SH = 0 # the default
 
70
        LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
 
71
        LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
 
72
 
 
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)
141
94
    except ImportError:
142
95
        try:
143
96
            import msvcrt
144
 
 
145
 
 
146
97
            # Unfortunately, msvcrt.locking() doesn't distinguish between
147
98
            # read locks and write locks. Also, the way the combinations
148
99
            # work to get non-blocking is not the same, so we
149
100
            # have to write extra special functions here.
150
101
 
151
 
 
152
 
            class _msvc_FileLock(_base_Lock):
153
 
                LOCK_SH = 1
154
 
                LOCK_EX = 2
155
 
                LOCK_NB = 4
156
 
                def unlock(self):
157
 
                    _msvc_unlock(self.f)
158
 
 
159
 
 
160
 
            class _msvc_ReadLock(_msvc_FileLock):
161
 
                def __init__(self, filename):
162
 
                    _msvc_lock(self._open(filename, 'rb'), self.LOCK_SH)
163
 
 
164
 
 
165
 
            class _msvc_WriteLock(_msvc_FileLock):
166
 
                def __init__(self, filename):
167
 
                    _msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
168
 
 
169
 
 
170
 
 
171
 
            def _msvc_lock(f, flags):
 
102
            LOCK_SH = 1
 
103
            LOCK_EX = 2
 
104
            LOCK_NB = 4
 
105
 
 
106
            def lock(f, flags):
172
107
                try:
173
108
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
174
109
                    # according to the comments, LK_RLCK is open the lock for writing.
184
119
                        fn = f
185
120
                        fpos = os.lseek(fn, 0,0)
186
121
                        os.lseek(fn, 0,0)
187
 
 
188
 
                    if flags & self.LOCK_SH:
189
 
                        if flags & self.LOCK_NB:
 
122
                    
 
123
                    if flags & LOCK_SH:
 
124
                        if flags & LOCK_NB:
190
125
                            lock_mode = msvcrt.LK_NBLCK
191
126
                        else:
192
127
                            lock_mode = msvcrt.LK_LOCK
193
 
                    elif flags & self.LOCK_EX:
194
 
                        if flags & self.LOCK_NB:
 
128
                    elif flags & LOCK_EX:
 
129
                        if flags & LOCK_NB:
195
130
                            lock_mode = msvcrt.LK_NBRLCK
196
131
                        else:
197
132
                            lock_mode = msvcrt.LK_RLCK
204
139
                except Exception, e:
205
140
                    raise LockError(e)
206
141
 
207
 
            def _msvc_unlock(f):
 
142
            def unlock(f):
208
143
                try:
209
144
                    if type(f) == file:
210
145
                        fpos = f.tell()
221
156
                        os.lseek(fn, fpos, 0)
222
157
                except Exception, e:
223
158
                    raise LockError(e)
224
 
 
225
 
 
226
 
 
227
 
            WriteLock = _msvc_WriteLock
228
 
            ReadLock = _msvc_ReadLock
229
159
        except ImportError:
230
 
            raise NotImplementedError("please write a locking method "
231
 
                                      "for platform %r" % sys.platform)
232
 
 
233
 
 
234
 
 
235
 
 
236
 
 
237
 
 
 
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
238
169