o
    :Zc                  	   @   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 )aw  
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] http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
[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

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   M/var/www/chikooza/env/lib/python3.10/site-packages/django/core/files/locks.py_fd   s   r   nt)	sizeofc_ulongc_void_pc_int64	StructureUnionPOINTERwindllbyref)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   0   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$   5   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   M   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   S   s   r   c                 C      dS )NFr   )r
   r4   r   r   r   r   c      c                 C   r9   )NTr   r	   r   r   r   r   g   r:   c                 C   s   t t| |}|dkS Nr   )fcntlflockr   )r
   r4   r7   r   r   r   r   k   s   c                 C   s   t t| t j}|dkS r;   )r<   r=   r   LOCK_UN)r
   r7   r   r   r   r   o   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    
,
