o
    ˷e	                  	   @   s  d Z ddlZdZdd Zejdkrd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rFe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#dS zddl$Z$e$jZe$jZe$jZW n e%e&fy   d Z ZZdd Z"dd Z#Y dS w 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   N/var/www/ideatree/venv/lib/python3.10/site-packages/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 )_OFFSETOffset
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_offset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 )
OVERLAPPED_offset_unionInternalInternalHigh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/   )r0   r1   r   r)   UnlockFileExr   r3   )r
   r5   r6   r7   r   r   r   r   Y   s   r   c                 C      dS )NFr   r
   r4   r   r   r   r   k      c                 C   r9   NTr   r	   r   r   r   r   o   r;   c                 C   s,   zt t| | W dS  ty   Y dS w )NTF)fcntlflockr   BlockingIOErrorr:   r   r   r   r   u   s   c                 C   s   t t| t j dS r<   )r=   r>   r   LOCK_UNr	   r   r   r   r   |   s   )'__doc__os__all__r   namer0   ctypesr   r   r   r   r   r   r   r   r   ctypes.wintypesr   r   r   r   r   r   r.   r(   r   r$   r)   LPOVERLAPPEDkernel32r2   restypeargtypesr8   r   r   r=   ImportErrorAttributeErrorr   r   r   r   <module>   sN    
,	
