/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-06-01 04:09:38 UTC
  • Revision ID: mbp@sourcefrog.net-20050601040938-d905145b57ae017f
- unify two defintions of LockError

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
 
 
33
 
 
34
import sys, os
 
35
 
 
36
import bzrlib
 
37
from trace import mutter, note, warning
 
38
from errors import LockError
 
39
 
 
40
try:
 
41
    import fcntl
 
42
    LOCK_SH = fcntl.LOCK_SH
 
43
    LOCK_EX = fcntl.LOCK_EX
 
44
    LOCK_NB = fcntl.LOCK_NB
 
45
    def lock(f, flags):
 
46
        try:
 
47
            fcntl.flock(f, flags)
 
48
        except Exception, e:
 
49
            raise LockError(e)
 
50
 
 
51
    def unlock(f):
 
52
        try:
 
53
            fcntl.flock(f, fcntl.LOCK_UN)
 
54
        except Exception, e:
 
55
            raise LockError(e)
 
56
 
 
57
except ImportError:
 
58
    try:
 
59
        import win32con, win32file, pywintypes
 
60
        LOCK_SH = 0 # the default
 
61
        LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
 
62
        LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
 
63
 
 
64
        def lock(f, flags):
 
65
            try:
 
66
                if type(f) == file:
 
67
                    hfile = win32file._get_osfhandle(f.fileno())
 
68
                else:
 
69
                    hfile = win32file._get_osfhandle(f)
 
70
                overlapped = pywintypes.OVERLAPPED()
 
71
                win32file.LockFileEx(hfile, flags, 0, 0x7fff0000, overlapped)
 
72
            except Exception, e:
 
73
                raise LockError(e)
 
74
 
 
75
        def unlock(f):
 
76
            try:
 
77
                if type(f) == file:
 
78
                    hfile = win32file._get_osfhandle(f.fileno())
 
79
                else:
 
80
                    hfile = win32file._get_osfhandle(f)
 
81
                overlapped = pywintypes.OVERLAPPED()
 
82
                win32file.UnlockFileEx(hfile, 0, 0x7fff0000, overlapped)
 
83
            except Exception, e:
 
84
                raise LockError(e)
 
85
    except ImportError:
 
86
        try:
 
87
            import msvcrt
 
88
            # Unfortunately, msvcrt.locking() doesn't distinguish between
 
89
            # read locks and write locks. Also, the way the combinations
 
90
            # work to get non-blocking is not the same, so we
 
91
            # have to write extra special functions here.
 
92
 
 
93
            LOCK_SH = 1
 
94
            LOCK_EX = 2
 
95
            LOCK_NB = 4
 
96
 
 
97
            def lock(f, flags):
 
98
                try:
 
99
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
 
100
                    # according to the comments, LK_RLCK is open the lock for writing.
 
101
 
 
102
                    # Unfortunately, msvcrt.locking() also has the side effect that it
 
103
                    # will only block for 10 seconds at most, and then it will throw an
 
104
                    # exception, this isn't terrible, though.
 
105
                    if type(f) == file:
 
106
                        fpos = f.tell()
 
107
                        fn = f.fileno()
 
108
                        f.seek(0)
 
109
                    else:
 
110
                        fn = f
 
111
                        fpos = os.lseek(fn, 0,0)
 
112
                        os.lseek(fn, 0,0)
 
113
                    
 
114
                    if flags & LOCK_SH:
 
115
                        if flags & LOCK_NB:
 
116
                            lock_mode = msvcrt.LK_NBLCK
 
117
                        else:
 
118
                            lock_mode = msvcrt.LK_LOCK
 
119
                    elif flags & LOCK_EX:
 
120
                        if flags & LOCK_NB:
 
121
                            lock_mode = msvcrt.LK_NBRLCK
 
122
                        else:
 
123
                            lock_mode = msvcrt.LK_RLCK
 
124
                    else:
 
125
                        raise ValueError('Invalid lock mode: %r' % flags)
 
126
                    try:
 
127
                        msvcrt.locking(fn, lock_mode, -1)
 
128
                    finally:
 
129
                        os.lseek(fn, fpos, 0)
 
130
                except Exception, e:
 
131
                    raise LockError(e)
 
132
 
 
133
            def unlock(f):
 
134
                try:
 
135
                    if type(f) == file:
 
136
                        fpos = f.tell()
 
137
                        fn = f.fileno()
 
138
                        f.seek(0)
 
139
                    else:
 
140
                        fn = f
 
141
                        fpos = os.lseek(fn, 0,0)
 
142
                        os.lseek(fn, 0,0)
 
143
 
 
144
                    try:
 
145
                        msvcrt.locking(fn, msvcrt.LK_UNLCK, -1)
 
146
                    finally:
 
147
                        os.lseek(fn, fpos, 0)
 
148
                except Exception, e:
 
149
                    raise LockError(e)
 
150
        except ImportError:
 
151
            from warnings import Warning
 
152
            
 
153
            warning("please write a locking method for platform %r" % sys.platform)
 
154
 
 
155
            # Creating no-op lock/unlock for now
 
156
            def lock(f, flags):
 
157
                pass
 
158
            def unlock(f):
 
159
                pass
 
160