OLD | NEW |
1 # Copyright 2014 Google Inc. All rights reserved. | 1 # Copyright 2011 Google Inc. |
2 # | 2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
6 # | 6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # | 8 # |
9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
13 # limitations under the License. | 13 # limitations under the License. |
14 | 14 |
15 """Locked file interface that should work on Unix and Windows pythons. | 15 """Locked file interface that should work on Unix and Windows pythons. |
16 | 16 |
17 This module first tries to use fcntl locking to ensure serialized access | 17 This module first tries to use fcntl locking to ensure serialized access |
18 to a file, then falls back on a lock file if that is unavialable. | 18 to a file, then falls back on a lock file if that is unavialable. |
19 | 19 |
20 Usage:: | 20 Usage: |
21 | |
22 f = LockedFile('filename', 'r+b', 'rb') | 21 f = LockedFile('filename', 'r+b', 'rb') |
23 f.open_and_lock() | 22 f.open_and_lock() |
24 if f.is_locked(): | 23 if f.is_locked(): |
25 print('Acquired filename with r+b mode') | 24 print 'Acquired filename with r+b mode' |
26 f.file_handle().write('locked data') | 25 f.file_handle().write('locked data') |
27 else: | 26 else: |
28 print('Acquired filename with rb mode') | 27 print 'Aquired filename with rb mode' |
29 f.unlock_and_close() | 28 f.unlock_and_close() |
30 | |
31 """ | 29 """ |
32 | 30 |
33 from __future__ import print_function | |
34 | |
35 __author__ = 'cache@google.com (David T McWherter)' | 31 __author__ = 'cache@google.com (David T McWherter)' |
36 | 32 |
37 import errno | 33 import errno |
38 import logging | 34 import logging |
39 import os | 35 import os |
40 import time | 36 import time |
41 | 37 |
42 from . import util | 38 from . import util |
43 | 39 |
44 logger = logging.getLogger(__name__) | 40 logger = logging.getLogger(__name__) |
(...skipping 22 matching lines...) Expand all Loading... |
67 Args: | 63 Args: |
68 filename: string, The pathname of the file. | 64 filename: string, The pathname of the file. |
69 mode: string, The preferred mode to access the file with. | 65 mode: string, The preferred mode to access the file with. |
70 fallback_mode: string, The mode to use if locking fails. | 66 fallback_mode: string, The mode to use if locking fails. |
71 """ | 67 """ |
72 self._locked = False | 68 self._locked = False |
73 self._filename = filename | 69 self._filename = filename |
74 self._mode = mode | 70 self._mode = mode |
75 self._fallback_mode = fallback_mode | 71 self._fallback_mode = fallback_mode |
76 self._fh = None | 72 self._fh = None |
77 self._lock_fd = None | |
78 | 73 |
79 def is_locked(self): | 74 def is_locked(self): |
80 """Was the file locked.""" | 75 """Was the file locked.""" |
81 return self._locked | 76 return self._locked |
82 | 77 |
83 def file_handle(self): | 78 def file_handle(self): |
84 """The file handle to the file. Valid only after opened.""" | 79 """The file handle to the file. Valid only after opened.""" |
85 return self._fh | 80 return self._fh |
86 | 81 |
87 def filename(self): | 82 def filename(self): |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 115 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
121 """ | 116 """ |
122 if self._locked: | 117 if self._locked: |
123 raise AlreadyLockedException('File %s is already locked' % | 118 raise AlreadyLockedException('File %s is already locked' % |
124 self._filename) | 119 self._filename) |
125 self._locked = False | 120 self._locked = False |
126 | 121 |
127 validate_file(self._filename) | 122 validate_file(self._filename) |
128 try: | 123 try: |
129 self._fh = open(self._filename, self._mode) | 124 self._fh = open(self._filename, self._mode) |
130 except IOError as e: | 125 except IOError, e: |
131 # If we can't access with _mode, try _fallback_mode and don't lock. | 126 # If we can't access with _mode, try _fallback_mode and don't lock. |
132 if e.errno == errno.EACCES: | 127 if e.errno == errno.EACCES: |
133 self._fh = open(self._filename, self._fallback_mode) | 128 self._fh = open(self._filename, self._fallback_mode) |
134 return | 129 return |
135 | 130 |
136 lock_filename = self._posix_lockfile(self._filename) | 131 lock_filename = self._posix_lockfile(self._filename) |
137 start_time = time.time() | 132 start_time = time.time() |
138 while True: | 133 while True: |
139 try: | 134 try: |
140 self._lock_fd = os.open(lock_filename, | 135 self._lock_fd = os.open(lock_filename, |
141 os.O_CREAT|os.O_EXCL|os.O_RDWR) | 136 os.O_CREAT|os.O_EXCL|os.O_RDWR) |
142 self._locked = True | 137 self._locked = True |
143 break | 138 break |
144 | 139 |
145 except OSError as e: | 140 except OSError, e: |
146 if e.errno != errno.EEXIST: | 141 if e.errno != errno.EEXIST: |
147 raise | 142 raise |
148 if (time.time() - start_time) >= timeout: | 143 if (time.time() - start_time) >= timeout: |
149 logger.warn('Could not acquire lock %s in %s seconds', | 144 logger.warn('Could not acquire lock %s in %s seconds' % ( |
150 lock_filename, timeout) | 145 lock_filename, timeout)) |
151 # Close the file and open in fallback_mode. | 146 # Close the file and open in fallback_mode. |
152 if self._fh: | 147 if self._fh: |
153 self._fh.close() | 148 self._fh.close() |
154 self._fh = open(self._filename, self._fallback_mode) | 149 self._fh = open(self._filename, self._fallback_mode) |
155 return | 150 return |
156 time.sleep(delay) | 151 time.sleep(delay) |
157 | 152 |
158 def unlock_and_close(self): | 153 def unlock_and_close(self): |
159 """Unlock a file by removing the .lock file, and close the handle.""" | 154 """Unlock a file by removing the .lock file, and close the handle.""" |
160 if self._locked: | 155 if self._locked: |
(...skipping 29 matching lines...) Expand all Loading... |
190 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 185 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
191 """ | 186 """ |
192 if self._locked: | 187 if self._locked: |
193 raise AlreadyLockedException('File %s is already locked' % | 188 raise AlreadyLockedException('File %s is already locked' % |
194 self._filename) | 189 self._filename) |
195 start_time = time.time() | 190 start_time = time.time() |
196 | 191 |
197 validate_file(self._filename) | 192 validate_file(self._filename) |
198 try: | 193 try: |
199 self._fh = open(self._filename, self._mode) | 194 self._fh = open(self._filename, self._mode) |
200 except IOError as e: | 195 except IOError, e: |
201 # If we can't access with _mode, try _fallback_mode and don't lock. | 196 # If we can't access with _mode, try _fallback_mode and don't lock. |
202 if e.errno in (errno.EPERM, errno.EACCES): | 197 if e.errno == errno.EACCES: |
203 self._fh = open(self._filename, self._fallback_mode) | 198 self._fh = open(self._filename, self._fallback_mode) |
204 return | 199 return |
205 | 200 |
206 # We opened in _mode, try to lock the file. | 201 # We opened in _mode, try to lock the file. |
207 while True: | 202 while True: |
208 try: | 203 try: |
209 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_EX) | 204 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_EX) |
210 self._locked = True | 205 self._locked = True |
211 return | 206 return |
212 except IOError as e: | 207 except IOError, e: |
213 # If not retrying, then just pass on the error. | 208 # If not retrying, then just pass on the error. |
214 if timeout == 0: | 209 if timeout == 0: |
215 raise | 210 raise e |
216 if e.errno != errno.EACCES: | 211 if e.errno != errno.EACCES: |
217 raise | 212 raise e |
218 # We could not acquire the lock. Try again. | 213 # We could not acquire the lock. Try again. |
219 if (time.time() - start_time) >= timeout: | 214 if (time.time() - start_time) >= timeout: |
220 logger.warn('Could not lock %s in %s seconds', | 215 logger.warn('Could not lock %s in %s seconds' % ( |
221 self._filename, timeout) | 216 self._filename, timeout)) |
222 if self._fh: | 217 if self._fh: |
223 self._fh.close() | 218 self._fh.close() |
224 self._fh = open(self._filename, self._fallback_mode) | 219 self._fh = open(self._filename, self._fallback_mode) |
225 return | 220 return |
226 time.sleep(delay) | 221 time.sleep(delay) |
227 | 222 |
228 def unlock_and_close(self): | 223 def unlock_and_close(self): |
229 """Close and unlock the file using the fcntl.lockf primitive.""" | 224 """Close and unlock the file using the fcntl.lockf primitive.""" |
230 if self._locked: | 225 if self._locked: |
231 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN) | 226 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 260 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
266 """ | 261 """ |
267 if self._locked: | 262 if self._locked: |
268 raise AlreadyLockedException('File %s is already locked' % | 263 raise AlreadyLockedException('File %s is already locked' % |
269 self._filename) | 264 self._filename) |
270 start_time = time.time() | 265 start_time = time.time() |
271 | 266 |
272 validate_file(self._filename) | 267 validate_file(self._filename) |
273 try: | 268 try: |
274 self._fh = open(self._filename, self._mode) | 269 self._fh = open(self._filename, self._mode) |
275 except IOError as e: | 270 except IOError, e: |
276 # If we can't access with _mode, try _fallback_mode and don't lock. | 271 # If we can't access with _mode, try _fallback_mode and don't lock. |
277 if e.errno == errno.EACCES: | 272 if e.errno == errno.EACCES: |
278 self._fh = open(self._filename, self._fallback_mode) | 273 self._fh = open(self._filename, self._fallback_mode) |
279 return | 274 return |
280 | 275 |
281 # We opened in _mode, try to lock the file. | 276 # We opened in _mode, try to lock the file. |
282 while True: | 277 while True: |
283 try: | 278 try: |
284 hfile = win32file._get_osfhandle(self._fh.fileno()) | 279 hfile = win32file._get_osfhandle(self._fh.fileno()) |
285 win32file.LockFileEx( | 280 win32file.LockFileEx( |
286 hfile, | 281 hfile, |
287 (win32con.LOCKFILE_FAIL_IMMEDIATELY| | 282 (win32con.LOCKFILE_FAIL_IMMEDIATELY| |
288 win32con.LOCKFILE_EXCLUSIVE_LOCK), 0, -0x10000, | 283 win32con.LOCKFILE_EXCLUSIVE_LOCK), 0, -0x10000, |
289 pywintypes.OVERLAPPED()) | 284 pywintypes.OVERLAPPED()) |
290 self._locked = True | 285 self._locked = True |
291 return | 286 return |
292 except pywintypes.error as e: | 287 except pywintypes.error, e: |
293 if timeout == 0: | 288 if timeout == 0: |
294 raise | 289 raise e |
295 | 290 |
296 # If the error is not that the file is already in use, raise. | 291 # If the error is not that the file is already in use, raise. |
297 if e[0] != _Win32Opener.FILE_IN_USE_ERROR: | 292 if e[0] != _Win32Opener.FILE_IN_USE_ERROR: |
298 raise | 293 raise |
299 | 294 |
300 # We could not acquire the lock. Try again. | 295 # We could not acquire the lock. Try again. |
301 if (time.time() - start_time) >= timeout: | 296 if (time.time() - start_time) >= timeout: |
302 logger.warn('Could not lock %s in %s seconds' % ( | 297 logger.warn('Could not lock %s in %s seconds' % ( |
303 self._filename, timeout)) | 298 self._filename, timeout)) |
304 if self._fh: | 299 if self._fh: |
305 self._fh.close() | 300 self._fh.close() |
306 self._fh = open(self._filename, self._fallback_mode) | 301 self._fh = open(self._filename, self._fallback_mode) |
307 return | 302 return |
308 time.sleep(delay) | 303 time.sleep(delay) |
309 | 304 |
310 def unlock_and_close(self): | 305 def unlock_and_close(self): |
311 """Close and unlock the file using the win32 primitive.""" | 306 """Close and unlock the file using the win32 primitive.""" |
312 if self._locked: | 307 if self._locked: |
313 try: | 308 try: |
314 hfile = win32file._get_osfhandle(self._fh.fileno()) | 309 hfile = win32file._get_osfhandle(self._fh.fileno()) |
315 win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) | 310 win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) |
316 except pywintypes.error as e: | 311 except pywintypes.error, e: |
317 if e[0] != _Win32Opener.FILE_ALREADY_UNLOCKED_ERROR: | 312 if e[0] != _Win32Opener.FILE_ALREADY_UNLOCKED_ERROR: |
318 raise | 313 raise |
319 self._locked = False | 314 self._locked = False |
320 if self._fh: | 315 if self._fh: |
321 self._fh.close() | 316 self._fh.close() |
322 except ImportError: | 317 except ImportError: |
323 _Win32Opener = None | 318 _Win32Opener = None |
324 | 319 |
325 | 320 |
326 class LockedFile(object): | 321 class LockedFile(object): |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 delay: float, The number of seconds to wait between retry attempts. | 363 delay: float, The number of seconds to wait between retry attempts. |
369 | 364 |
370 Raises: | 365 Raises: |
371 AlreadyLockedException: if the lock is already acquired. | 366 AlreadyLockedException: if the lock is already acquired. |
372 IOError: if the open fails. | 367 IOError: if the open fails. |
373 """ | 368 """ |
374 self._opener.open_and_lock(timeout, delay) | 369 self._opener.open_and_lock(timeout, delay) |
375 | 370 |
376 def unlock_and_close(self): | 371 def unlock_and_close(self): |
377 """Unlock and close a file.""" | 372 """Unlock and close a file.""" |
378 self._opener.unlock_and_close() | 373 self._opener.unlock_and_close() |
OLD | NEW |