o
    ô<âg  ã                	   @   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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
dƒZejZee_eeeeeege_ e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   úJ/var/www/html/venv/lib/python3.10/site-packages/django/core/files/locks.pyÚ_fd   s   r   Únt)	ÚPOINTERÚ	StructureÚUnionÚWinDLLÚbyrefÚc_int64Úc_ulongÚc_void_pÚsizeof)Ú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   :   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)   A   s    ür)   Úkernel32c                 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   U   s   r   c                 C   s0   t  t| ƒ¡}tƒ }t|dddt|ƒƒ}t|ƒS r0   )r1   r2   r   r)   ÚUnlockFileExr   r4   )r
   r6   r7   r8   r   r   r   r   [   s   r   c                 C   ó   dS )NFr   ©r
   r5   r   r   r   r   m   ó   c                 C   r:   ©NTr   r	   r   r   r   r   q   r<   c                 C   s,   zt  t| ƒ|¡ W dS  ty   Y dS w )NTF)ÚfcntlÚflockr   ÚBlockingIOErrorr;   r   r   r   r   w   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   Únamer1   Úctypesr   r   r   r   r   r   r   r   r   Úctypes.wintypesr   r   r   r   r   r   r.   r(   r   r$   r)   ÚLPOVERLAPPEDr/   r3   ÚrestypeÚargtypesr9   r   r   r>   ÚImportErrorÚAttributeErrorr   r   r   r   Ú<module>   sP    
,	
÷