/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: Robert Collins
  • Date: 2006-08-08 23:19:29 UTC
  • mfrom: (1884 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1912.
  • Revision ID: robertc@robertcollins.net-20060808231929-4e3e298190214b3a
current status

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
import os
39
39
import sys
40
40
 
41
 
from bzrlib.trace import mutter, note, warning
42
41
from bzrlib.errors import LockError
 
42
from bzrlib.osutils import realpath
 
43
from bzrlib.trace import mutter
 
44
 
43
45
 
44
46
class _base_Lock(object):
 
47
 
45
48
    def _open(self, filename, filemode):
46
49
        try:
47
50
            self.f = open(filename, filemode)
65
68
    def unlock(self):
66
69
        raise NotImplementedError()
67
70
 
68
 
        
69
 
 
70
 
 
71
 
 
72
71
 
73
72
############################################################
74
73
# msvcrt locks
78
77
    import fcntl
79
78
 
80
79
    class _fcntl_FileLock(_base_Lock):
 
80
 
81
81
        f = None
82
82
 
83
 
        def unlock(self):
 
83
        def _unlock(self):
84
84
            fcntl.lockf(self.f, fcntl.LOCK_UN)
 
85
            self._clear_f()
 
86
 
 
87
        def _clear_f(self):
 
88
            """Clear the self.f attribute cleanly."""
85
89
            self.f.close()
86
90
            del self.f 
87
91
 
 
92
 
88
93
    class _fcntl_WriteLock(_fcntl_FileLock):
 
94
 
 
95
        open_locks = {}
 
96
 
89
97
        def __init__(self, filename):
90
98
            # standard IO errors get exposed directly.
91
99
            self._open(filename, 'wb')
92
100
            try:
 
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
93
109
                fcntl.lockf(self.f, fcntl.LOCK_EX)
94
110
            except IOError, e:
95
111
                # we should be more precise about whats a locking
96
112
                # error and whats a random-other error
97
113
                raise LockError(e)
98
114
 
 
115
        def unlock(self):
 
116
            del self.open_locks[self.filename]
 
117
            self._unlock()
 
118
 
 
119
 
99
120
    class _fcntl_ReadLock(_fcntl_FileLock):
100
121
 
101
122
        def __init__(self, filename):
108
129
                # error and whats a random-other error
109
130
                raise LockError(e)
110
131
 
 
132
        def unlock(self):
 
133
            self._unlock()
 
134
 
 
135
 
111
136
    WriteLock = _fcntl_WriteLock
112
137
    ReadLock = _fcntl_ReadLock
113
138
 
129
154
                    overlapped = pywintypes.OVERLAPPED()
130
155
                    win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
131
156
                except Exception, e:
 
157
                    if self.f:
 
158
                        self.f.close()
 
159
                        self.f = None
132
160
                    raise LockError(e)
133
161
 
134
162
            def unlock(self):
141
169
                    raise LockError(e)
142
170
 
143
171
 
144
 
 
145
172
        class _w32c_ReadLock(_w32c_FileLock):
146
173
            def __init__(self, filename):
147
174
                _w32c_FileLock._lock(self, filename, 'rb',
153
180
                                     LOCK_EX + LOCK_NB)
154
181
 
155
182
 
156
 
 
157
183
        WriteLock = _w32c_WriteLock
158
184
        ReadLock = _w32c_ReadLock
159
185
 
188
214
                    _msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
189
215
 
190
216
 
191
 
 
192
217
            def _msvc_lock(f, flags):
193
218
                try:
194
219
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
244
269
                    raise LockError(e)
245
270
 
246
271
 
247
 
 
248
272
            WriteLock = _msvc_WriteLock
249
273
            ReadLock = _msvc_ReadLock
250
274
        except ImportError: