U
    mf	                  	   @   s  d Z ddlZdZdd ZejdkrddlZddlmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZmZmZ dZd	Zd
ZeeeekreZneZeZG dd deZG dd de	ZG dd deZeeZejjZee_eeeeeege_ ejj!Z!ee!_eeeeege!_ dd Z"dd Z#nfzddl$Z$e$jZe$jZe$jZW n6 e%e&fk
rr   d Z ZZdd Z"dd Z#Y nX dd Z"dd Z#dS )am  
Portable file locking utilities.

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]).

[1] https://code.activestate.com/recipes/65203/
[2] https://sourceforge.net/p/roundup/code/ci/default/tree/roundup/backends/portalocker.py  # NOQA
[3] https://sourceforge.net/p/roundup/code/ci/default/tree/COPYING.txt

Example Usage::

    >>> from django.core.files import locks
    >>> with open('./file', 'wb') as f:
    ...     locks.lock(f, locks.LOCK_EX)
    ...     f.write('Django')
    N)LOCK_EXLOCK_SHLOCK_NBlockunlockc                 C   s   t | dr|  S | S )zCGet a filedescriptor from something which could be a file or an fd.fileno)hasattrr   f r   ;/tmp/pip-unpacked-wheel-siwebuq3/django/core/files/locks.py_fd   s    r   nt)	POINTER	StructureUnionbyrefc_int64c_ulongc_void_psizeofwindll)BOOLDWORDHANDLE      c                   @   s   e Zd ZdefdefgZdS )_OFFSETZOffsetZ
OffsetHighN)__name__
__module____qualname__r   _fields_r   r   r   r   r   9   s   r   c                   @   s"   e Zd ZdgZdefdefgZdS )_OFFSET_UNION_offsetZPointerN)r   r   r    _anonymous_r   PVOIDr!   r   r   r   r   r"   <   s   r"   c                   @   s.   e Zd ZdgZdefdefdefdefgZdS )
OVERLAPPEDZ_offset_unionZInternalZInternalHighZhEventN)r   r   r    r$   	ULONG_PTRr"   r   r!   r   r   r   r   r&   @   s   r&   c                 C   s2   t t| }t }t||dddt|}t|S Nr   l      )msvcrtget_osfhandler   r&   
LockFileExr   bool)r
   flagshfile
overlappedretr   r   r   r   S   s    r   c                 C   s0   t t| }t }t|dddt|}t|S r(   )r)   r*   r   r&   UnlockFileExr   r,   )r
   r.   r/   r0   r   r   r   r   Y   s    r   c                 C   s   dS )NFr   r
   r-   r   r   r   r   k   s    c                 C   s   dS NTr   r	   r   r   r   r   o   s    c                 C   s2   zt t| | W dS  tk
r,   Y dS X d S )NTF)fcntlflockr   BlockingIOErrorr2   r   r   r   r   u   s
    c                 C   s   t t| t j dS r3   )r4   r5   r   ZLOCK_UNr	   r   r   r   r   |   s    )'__doc__os__all__r   namer)   ctypesr   r   r   r   r   r   r   r   r   Zctypes.wintypesr   r   r   r   r   r   r'   r%   r   r"   r&   ZLPOVERLAPPEDkernel32r+   restypeargtypesr1   r   r   r4   ImportErrorAttributeErrorr   r   r   r   <module>   sJ   ,	

