Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 1 | # Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """Exclusive filelocking for all supported platforms.""" |
| 5 | |
| 6 | from __future__ import print_function |
| 7 | |
| 8 | import contextlib |
| 9 | import logging |
| 10 | import os |
| 11 | import sys |
| 12 | import time |
| 13 | |
| 14 | |
| 15 | class LockError(Exception): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 16 | pass |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | if sys.platform.startswith('win'): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 20 | # Windows implementation |
| 21 | import win32imports |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 22 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 23 | BYTES_TO_LOCK = 1 |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 24 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 25 | def _open_file(lockfile): |
| 26 | return win32imports.Handle( |
| 27 | win32imports.CreateFileW( |
| 28 | lockfile, # lpFileName |
| 29 | win32imports.GENERIC_WRITE, # dwDesiredAccess |
| 30 | 0, # dwShareMode=prevent others from opening file |
| 31 | None, # lpSecurityAttributes |
| 32 | win32imports.CREATE_ALWAYS, # dwCreationDisposition |
| 33 | win32imports.FILE_ATTRIBUTE_NORMAL, # dwFlagsAndAttributes |
| 34 | None # hTemplateFile |
| 35 | )) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 36 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 37 | def _close_file(handle): |
| 38 | # CloseHandle releases lock too. |
| 39 | win32imports.CloseHandle(handle) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 40 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 41 | def _lock_file(handle): |
| 42 | ret = win32imports.LockFileEx( |
| 43 | handle, # hFile |
| 44 | win32imports.LOCKFILE_FAIL_IMMEDIATELY |
| 45 | | win32imports.LOCKFILE_EXCLUSIVE_LOCK, # dwFlags |
| 46 | 0, #dwReserved |
| 47 | BYTES_TO_LOCK, # nNumberOfBytesToLockLow |
| 48 | 0, # nNumberOfBytesToLockHigh |
| 49 | win32imports.Overlapped() # lpOverlapped |
| 50 | ) |
| 51 | # LockFileEx returns result as bool, which is converted into an integer |
| 52 | # (1 == successful; 0 == not successful) |
| 53 | if ret == 0: |
| 54 | error_code = win32imports.GetLastError() |
| 55 | raise OSError('Failed to lock handle (error code: %d).' % |
| 56 | error_code) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 57 | else: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 58 | # Unix implementation |
| 59 | import fcntl |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 60 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 61 | def _open_file(lockfile): |
| 62 | open_flags = (os.O_CREAT | os.O_WRONLY) |
| 63 | return os.open(lockfile, open_flags, 0o644) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 64 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 65 | def _close_file(fd): |
| 66 | os.close(fd) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 67 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 68 | def _lock_file(fd): |
| 69 | fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def _try_lock(lockfile): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 73 | f = _open_file(lockfile) |
| 74 | try: |
| 75 | _lock_file(f) |
| 76 | except Exception: |
| 77 | _close_file(f) |
| 78 | raise |
| 79 | return lambda: _close_file(f) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def _lock(path, timeout=0): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 83 | """_lock returns function to release the lock if locking was successful. |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 84 | |
| 85 | _lock also implements simple retry logic.""" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 86 | elapsed = 0 |
| 87 | while True: |
| 88 | try: |
| 89 | return _try_lock(path + '.locked') |
| 90 | except (OSError, IOError) as e: |
| 91 | if elapsed < timeout: |
| 92 | sleep_time = min(10, timeout - elapsed) |
| 93 | logging.info( |
| 94 | 'Could not create git cache lockfile; ' |
| 95 | 'will retry after sleep(%d).', sleep_time) |
| 96 | elapsed += sleep_time |
| 97 | time.sleep(sleep_time) |
| 98 | continue |
| 99 | raise LockError("Error locking %s (err: %s)" % (path, str(e))) |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | @contextlib.contextmanager |
| 103 | def lock(path, timeout=0): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 104 | """Get exclusive lock to path. |
Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 105 | |
| 106 | Usage: |
| 107 | import lockfile |
| 108 | with lockfile.lock(path, timeout): |
| 109 | # Do something |
| 110 | pass |
| 111 | |
| 112 | """ |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 113 | release_fn = _lock(path, timeout) |
| 114 | try: |
| 115 | yield |
| 116 | finally: |
| 117 | release_fn() |