~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Martin Pool
  • Date: 2005-06-10 06:34:26 UTC
  • Revision ID: mbp@sourcefrog.net-20050610063426-cfcf5c0f96c271ec
- split out updated progress indicator

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
"""
36
36
 
37
37
 
38
 
import sys
39
 
import os
 
38
import sys, os
40
39
 
41
 
from bzrlib.trace import mutter, note, warning
42
 
from bzrlib.errors import LockError
 
40
from trace import mutter, note, warning
 
41
from errors import LockError
43
42
 
44
43
class _base_Lock(object):
45
44
    def _open(self, filename, filemode):
46
 
        import errno
47
 
        try:
48
 
            self.f = open(filename, filemode)
49
 
            return self.f
50
 
        except IOError, e:
51
 
            if e.errno != errno.ENOENT:
52
 
                raise
53
 
 
54
 
            # maybe this is an old branch (before may 2005)
55
 
            mutter("trying to create missing branch lock %r", filename)
56
 
            
57
 
            self.f = open(filename, 'wb')
58
 
            return self.f
59
 
 
 
45
        self.f = open(filename, filemode)
 
46
        return self.f
 
47
    
60
48
 
61
49
    def __del__(self):
62
50
        if self.f:
63
51
            from warnings import warn
64
52
            warn("lock on %r not released" % self.f)
65
53
            self.unlock()
66
 
            
67
54
 
68
55
    def unlock(self):
69
56
        raise NotImplementedError()
84
71
        f = None
85
72
 
86
73
        def unlock(self):
87
 
            fcntl.lockf(self.f, fcntl.LOCK_UN)
 
74
            fcntl.flock(self.f, fcntl.LOCK_UN)
88
75
            self.f.close()
89
76
            del self.f 
90
77
 
92
79
    class _fcntl_WriteLock(_fcntl_FileLock):
93
80
        def __init__(self, filename):
94
81
            try:
95
 
                fcntl.lockf(self._open(filename, 'wb'), fcntl.LOCK_EX)
 
82
                fcntl.flock(self._open(filename, 'wb'), fcntl.LOCK_EX)
96
83
            except Exception, e:
97
84
                raise LockError(e)
98
85
 
100
87
    class _fcntl_ReadLock(_fcntl_FileLock):
101
88
        def __init__(self, filename):
102
89
            try:
103
 
                fcntl.lockf(self._open(filename, 'rb'), fcntl.LOCK_SH)
 
90
                fcntl.flock(self._open(filename, 'rb'), fcntl.LOCK_SH)
104
91
            except Exception, e:
105
92
                raise LockError(e)
106
93
 
112
99
        import win32con, win32file, pywintypes
113
100
 
114
101
 
115
 
        LOCK_SH = 0 # the default
116
 
        LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
117
 
        LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
 
102
        #LOCK_SH = 0 # the default
 
103
        #LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
 
104
        #LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
118
105
 
119
106
        class _w32c_FileLock(_base_Lock):
120
107
            def _lock(self, filename, openmode, lockmode):
139
126
 
140
127
        class _w32c_ReadLock(_w32c_FileLock):
141
128
            def __init__(self, filename):
142
 
                _w32c_FileLock._lock(self, filename, 'rb',
143
 
                                     LOCK_NB)
 
129
                _w32c_FileLock._lock(self, filename, 'rb', 0)
144
130
 
145
131
        class _w32c_WriteLock(_w32c_FileLock):
146
132
            def __init__(self, filename):
147
133
                _w32c_FileLock._lock(self, filename, 'wb',
148
 
                                     LOCK_EX + LOCK_NB)
 
134
                                     win32con.LOCKFILE_EXCLUSIVE_LOCK)
149
135
 
150
136
 
151
137
 
169
155
                LOCK_NB = 4
170
156
                def unlock(self):
171
157
                    _msvc_unlock(self.f)
172
 
                    self.f.close()
173
 
                    self.f = None
174
158
 
175
159
 
176
160
            class _msvc_ReadLock(_msvc_FileLock):
201
185
                        fpos = os.lseek(fn, 0,0)
202
186
                        os.lseek(fn, 0,0)
203
187
 
204
 
                    if flags & _msvc_FileLock.LOCK_SH:
205
 
                        if flags & _msvc_FileLock.LOCK_NB:
 
188
                    if flags & self.LOCK_SH:
 
189
                        if flags & self.LOCK_NB:
206
190
                            lock_mode = msvcrt.LK_NBLCK
207
191
                        else:
208
192
                            lock_mode = msvcrt.LK_LOCK
209
 
                    elif flags & _msvc_FileLock.LOCK_EX:
210
 
                        if flags & _msvc_FileLock.LOCK_NB:
 
193
                    elif flags & self.LOCK_EX:
 
194
                        if flags & self.LOCK_NB:
211
195
                            lock_mode = msvcrt.LK_NBRLCK
212
196
                        else:
213
197
                            lock_mode = msvcrt.LK_RLCK