2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
Portable file locking utilities.
|
|
|
|
|
2013-09-18 09:34:45 +08:00
|
|
|
Based partially on an example by Jonathan Feignberg in the Python
|
|
|
|
Cookbook [1] (licensed under the Python Software License) and a ctypes port by
|
|
|
|
Anatoly Techtonik for Roundup [2] (license [3]).
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-09-18 09:34:45 +08:00
|
|
|
[1] http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
|
2018-09-26 14:48:47 +08:00
|
|
|
[2] https://sourceforge.net/p/roundup/code/ci/default/tree/roundup/backends/portalocker.py
|
|
|
|
[3] https://sourceforge.net/p/roundup/code/ci/default/tree/COPYING.txt
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
Example Usage::
|
|
|
|
|
|
|
|
>>> from django.core.files import locks
|
2012-05-05 20:01:38 +08:00
|
|
|
>>> with open('./file', 'wb') as f:
|
2012-05-05 22:54:30 +08:00
|
|
|
... locks.lock(f, locks.LOCK_EX)
|
|
|
|
... f.write('Django')
|
2008-07-01 23:10:51 +08:00
|
|
|
"""
|
2013-09-18 09:34:45 +08:00
|
|
|
import os
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-10-25 01:30:03 +08:00
|
|
|
__all__ = ('LOCK_EX', 'LOCK_SH', 'LOCK_NB', 'lock', 'unlock')
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-09-18 09:34:45 +08:00
|
|
|
def _fd(f):
|
2008-08-12 00:51:18 +08:00
|
|
|
"""Get a filedescriptor from something which could be a file or an fd."""
|
2013-05-27 10:47:50 +08:00
|
|
|
return f.fileno() if hasattr(f, 'fileno') else f
|
2008-08-12 00:51:18 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-09-18 09:34:45 +08:00
|
|
|
if os.name == 'nt':
|
|
|
|
import msvcrt
|
|
|
|
from ctypes import (sizeof, c_ulong, c_void_p, c_int64,
|
|
|
|
Structure, Union, POINTER, windll, byref)
|
|
|
|
from ctypes.wintypes import BOOL, DWORD, HANDLE
|
|
|
|
|
|
|
|
LOCK_SH = 0 # the default
|
|
|
|
LOCK_NB = 0x1 # LOCKFILE_FAIL_IMMEDIATELY
|
|
|
|
LOCK_EX = 0x2 # LOCKFILE_EXCLUSIVE_LOCK
|
|
|
|
|
|
|
|
# --- Adapted from the pyserial project ---
|
|
|
|
# detect size of ULONG_PTR
|
|
|
|
if sizeof(c_ulong) != sizeof(c_void_p):
|
|
|
|
ULONG_PTR = c_int64
|
|
|
|
else:
|
|
|
|
ULONG_PTR = c_ulong
|
|
|
|
PVOID = c_void_p
|
|
|
|
|
|
|
|
# --- Union inside Structure by stackoverflow:3480240 ---
|
|
|
|
class _OFFSET(Structure):
|
|
|
|
_fields_ = [
|
|
|
|
('Offset', DWORD),
|
|
|
|
('OffsetHigh', DWORD)]
|
|
|
|
|
|
|
|
class _OFFSET_UNION(Union):
|
|
|
|
_anonymous_ = ['_offset']
|
|
|
|
_fields_ = [
|
|
|
|
('_offset', _OFFSET),
|
|
|
|
('Pointer', PVOID)]
|
|
|
|
|
|
|
|
class OVERLAPPED(Structure):
|
|
|
|
_anonymous_ = ['_offset_union']
|
|
|
|
_fields_ = [
|
|
|
|
('Internal', ULONG_PTR),
|
|
|
|
('InternalHigh', ULONG_PTR),
|
|
|
|
('_offset_union', _OFFSET_UNION),
|
|
|
|
('hEvent', HANDLE)]
|
|
|
|
|
|
|
|
LPOVERLAPPED = POINTER(OVERLAPPED)
|
|
|
|
|
|
|
|
# --- Define function prototypes for extra safety ---
|
|
|
|
LockFileEx = windll.kernel32.LockFileEx
|
|
|
|
LockFileEx.restype = BOOL
|
|
|
|
LockFileEx.argtypes = [HANDLE, DWORD, DWORD, DWORD, DWORD, LPOVERLAPPED]
|
|
|
|
UnlockFileEx = windll.kernel32.UnlockFileEx
|
|
|
|
UnlockFileEx.restype = BOOL
|
|
|
|
UnlockFileEx.argtypes = [HANDLE, DWORD, DWORD, DWORD, LPOVERLAPPED]
|
|
|
|
|
|
|
|
def lock(f, flags):
|
|
|
|
hfile = msvcrt.get_osfhandle(_fd(f))
|
|
|
|
overlapped = OVERLAPPED()
|
|
|
|
ret = LockFileEx(hfile, flags, 0, 0, 0xFFFF0000, byref(overlapped))
|
|
|
|
return bool(ret)
|
|
|
|
|
|
|
|
def unlock(f):
|
|
|
|
hfile = msvcrt.get_osfhandle(_fd(f))
|
|
|
|
overlapped = OVERLAPPED()
|
|
|
|
ret = UnlockFileEx(hfile, 0, 0, 0xFFFF0000, byref(overlapped))
|
|
|
|
return bool(ret)
|
2014-03-17 18:46:58 +08:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
import fcntl
|
|
|
|
LOCK_SH = fcntl.LOCK_SH # shared lock
|
|
|
|
LOCK_NB = fcntl.LOCK_NB # non-blocking
|
|
|
|
LOCK_EX = fcntl.LOCK_EX
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
# File locking is not supported.
|
|
|
|
LOCK_EX = LOCK_SH = LOCK_NB = 0
|
|
|
|
|
|
|
|
# Dummy functions that don't do anything.
|
|
|
|
def lock(f, flags):
|
|
|
|
# File is not locked
|
|
|
|
return False
|
|
|
|
|
|
|
|
def unlock(f):
|
|
|
|
# File is unlocked
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
def lock(f, flags):
|
|
|
|
ret = fcntl.flock(_fd(f), flags)
|
2017-09-14 09:20:29 +08:00
|
|
|
return ret == 0
|
2013-09-18 09:34:45 +08:00
|
|
|
|
2014-03-17 18:46:58 +08:00
|
|
|
def unlock(f):
|
|
|
|
ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
|
2017-09-14 09:20:29 +08:00
|
|
|
return ret == 0
|