~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Martin Pool
  • Date: 2005-05-17 07:01:47 UTC
  • Revision ID: mbp@sourcefrog.net-20050517070147-c38da17418ea6711
- Add patch to give symlink support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
 
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.
7
 
 
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.
12
 
 
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
 
 
18
 
"""Locking wrappers.
19
 
 
20
 
This only does local locking using OS locks for now.
21
 
 
22
 
This module causes two methods, lock() and unlock() to be defined in
23
 
any way that works on the current platform.
24
 
 
25
 
It is not specified whether these locks are reentrant (i.e. can be
26
 
taken repeatedly by a single process) or whether they exclude
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
 
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
 
 
38
 
import sys, os
39
 
 
40
 
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
 
 
66
 
 
67
 
try:
68
 
    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
96
 
 
97
 
except ImportError:
98
 
    try:
99
 
        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
 
 
141
 
    except ImportError:
142
 
        try:
143
 
            import msvcrt
144
 
 
145
 
 
146
 
            # Unfortunately, msvcrt.locking() doesn't distinguish between
147
 
            # read locks and write locks. Also, the way the combinations
148
 
            # work to get non-blocking is not the same, so we
149
 
            # have to write extra special functions here.
150
 
 
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):
172
 
                try:
173
 
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
174
 
                    # according to the comments, LK_RLCK is open the lock for writing.
175
 
 
176
 
                    # Unfortunately, msvcrt.locking() also has the side effect that it
177
 
                    # will only block for 10 seconds at most, and then it will throw an
178
 
                    # exception, this isn't terrible, though.
179
 
                    if type(f) == file:
180
 
                        fpos = f.tell()
181
 
                        fn = f.fileno()
182
 
                        f.seek(0)
183
 
                    else:
184
 
                        fn = f
185
 
                        fpos = os.lseek(fn, 0,0)
186
 
                        os.lseek(fn, 0,0)
187
 
 
188
 
                    if flags & self.LOCK_SH:
189
 
                        if flags & self.LOCK_NB:
190
 
                            lock_mode = msvcrt.LK_NBLCK
191
 
                        else:
192
 
                            lock_mode = msvcrt.LK_LOCK
193
 
                    elif flags & self.LOCK_EX:
194
 
                        if flags & self.LOCK_NB:
195
 
                            lock_mode = msvcrt.LK_NBRLCK
196
 
                        else:
197
 
                            lock_mode = msvcrt.LK_RLCK
198
 
                    else:
199
 
                        raise ValueError('Invalid lock mode: %r' % flags)
200
 
                    try:
201
 
                        msvcrt.locking(fn, lock_mode, -1)
202
 
                    finally:
203
 
                        os.lseek(fn, fpos, 0)
204
 
                except Exception, e:
205
 
                    raise LockError(e)
206
 
 
207
 
            def _msvc_unlock(f):
208
 
                try:
209
 
                    if type(f) == file:
210
 
                        fpos = f.tell()
211
 
                        fn = f.fileno()
212
 
                        f.seek(0)
213
 
                    else:
214
 
                        fn = f
215
 
                        fpos = os.lseek(fn, 0,0)
216
 
                        os.lseek(fn, 0,0)
217
 
 
218
 
                    try:
219
 
                        msvcrt.locking(fn, msvcrt.LK_UNLCK, -1)
220
 
                    finally:
221
 
                        os.lseek(fn, fpos, 0)
222
 
                except Exception, e:
223
 
                    raise LockError(e)
224
 
 
225
 
 
226
 
 
227
 
            WriteLock = _msvc_WriteLock
228
 
            ReadLock = _msvc_ReadLock
229
 
        except ImportError:
230
 
            raise NotImplementedError("please write a locking method "
231
 
                                      "for platform %r" % sys.platform)
232
 
 
233
 
 
234
 
 
235
 
 
236
 
 
237
 
 
238