o
    hR                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dlm
Z
mZmZ d dlmZ d dlmZ d dlmZ erJd dlmZ edZeedr^eed	r^eed
shdej Zeeejeee
eddd	efZejeeeddd
efZejedddefZG dd dZedd ejej ej!ej"ej#ej$ej%ej&ej'ej(ej)gZ*G dd dej+Z,e-e,Z.dZ/e/e.d  Z0G dd dZ1G dd dZ2dS )    )annotationsN)c_char_pc_intc_uint32)reduce)TYPE_CHECKING)UnsupportedLibcError)	Generatorinotify_initinotify_add_watchinotify_rm_watchz Unsupported libc version found: T)	use_errnoc                   @  s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZe	e
B ZdZdZdZdZdZdZdZdZdZedd eeeeeee	e
eeeegZdZdZdS )InotifyConstants                   @         i   i      i    i @  i   i   i   i   i    i   @l        c                 C     | |B S N xyr   r   P/var/www/vscode/kcb/lib/python3.10/site-packages/watchdog/observers/inotify_c.py<lambda>B       zInotifyConstants.<lambda>N)__name__
__module____qualname__	IN_ACCESS	IN_MODIFY	IN_ATTRIBIN_CLOSE_WRITEIN_CLOSE_NOWRITEIN_OPENIN_MOVED_FROMIN_MOVED_TO	IN_CREATE	IN_DELETEIN_DELETE_SELFIN_MOVE_SELFIN_MOVE
IN_UNMOUNTIN_Q_OVERFLOW
IN_IGNORED
IN_ONLYDIRIN_DONT_FOLLOWIN_EXCL_UNLINKIN_MASK_ADDIN_ISDIR
IN_ONESHOTr   IN_ALL_EVENTS
IN_CLOEXECIN_NONBLOCKr   r   r   r   r   !   sR    r   c                 C  r   r   r   r   r   r   r   r    Z   r!   r    c                   @  s2   e Zd ZdZdefdefdefdefdeffZdS )InotifyEventStructa  Structure representation of the inotify_event structure
    (used in buffer size calculations)::

        struct inotify_event {
            __s32 wd;            /* watch descriptor */
            __u32 mask;          /* watch mask */
            __u32 cookie;        /* cookie to synchronize two events */
            __u32 len;           /* length (including nulls) of name */
            char  name[0];       /* stub for possible name */
        };
    wdmaskcookielennameN)r"   r#   r$   __doc__r   r   r   _fields_r   r   r   r   r>   k   s    r>   r   r   c                   @  s   e Zd ZdZdddd:ddZed;ddZed<ddZed=ddZed;ddZ	d>ddZ
d?ddZd@d d!ZdAd"d#ZdAd$d%Zd>d&d'Zed(dBd+d,Zd>d-d.ZdCd0d1ZdDd2d3Zed>d4d5ZedEd8d9ZdS )FInotifya  Linux inotify(7) API wrapper class.

    :param path:
        The directory path for which we want an inotify object.
    :type path:
        :class:`bytes`
    :param recursive:
        ``True`` if subdirectories should be monitored; ``False`` otherwise.
    FN)	recursive
event_maskpathbytesrG   boolrH   
int | NonereturnNonec                  s  t  }|dkrt  | _t  _d _d _t	
 \ _ _ttdrHt  _ j jtj  j jtj d fdd}| _n
d fd	d
}| _i  _i  _| _|d u rat}| _| _t	j|rv j|||d n || i  _d S )NFTpollrM   rK   c                     s   t  fdd j D S )Nc                 3  s    | ]
\}}| j kV  qd S r   _inotify_fd).0fd_selfr   r   	<genexpr>   s    z4Inotify.__init__.<locals>.do_poll.<locals>.<genexpr>)any_pollerrP   r   rV   r   r   do_poll   s   z!Inotify.__init__.<locals>.do_pollc                    s$   t   j jgg g }  j| d v S Nr   )selectrR   _kill_r)resultrV   r   r   	do_select   s   z#Inotify.__init__.<locals>.do_select)rG   rM   rK   )r
   rF   _raise_errorrR   	threadingLock_lock_closed_is_readingospiper^   _kill_whasattrr]   rP   rZ   registerPOLLIN_check_inotify_fd_wd_for_path_path_for_wd_pathWATCHDOG_ALL_EVENTS_event_mask_is_recursiverI   isdir_add_dir_watch
_add_watch_moved_from_events)rW   rI   rG   rH   
inotify_fdr[   r`   r   rV   r   __init__   s6   



zInotify.__init__intc                 C     | j S )z)The event mask for this inotify instance.)rs   rV   r   r   r   rH         zInotify.event_maskc                 C  r|   )z.The path associated with the inotify instance.)rq   rV   r   r   r   rI      r}   zInotify.pathc                 C  r|   )z0Whether we are watching directories recursively.)rt   rV   r   r   r   is_recursive   r}   zInotify.is_recursivec                 C  r|   )z9The file descriptor associated with the inotify instance.rQ   rV   r   r   r   rT      r}   z
Inotify.fdc                 C  s
   i | _ dS )z)Clear cached records of MOVED_FROM eventsN)rx   rV   r   r   r   clear_move_records   s   
zInotify.clear_move_recordsdestination_eventInotifyEventbytes | Nonec                 C  s   |j | jv r| j|j  jS dS )zThe source path corresponding to the given MOVED_TO event.

        If the source path is outside the monitored directories, None
        is returned instead.
        N)rA   rx   src_path)rW   r   r   r   r   source_for_move   s   zInotify.source_for_moveeventc                 C  s   || j |j< dS )z]Save this event as the source event for future MOVED_TO events to
        reference.
        N)rx   rA   )rW   r   r   r   r   remember_move_from_event   s   z Inotify.remember_move_from_eventc                 C  s:   | j  | || j W d   dS 1 sw   Y  dS )zeAdds a watch for the given path.

        :param path:
            Path to begin monitoring.
        N)re   rw   rs   )rW   rI   r   r   r   	add_watch   s   "zInotify.add_watchc                 C  sh   | j ' | j|}| j|= t| j|dkr"t  W d   dS W d   dS 1 s-w   Y  dS )zRemoves a watch for the given path.

        :param path:
            Path string for which the watch will be removed.
        rO   N)re   ro   poprp   r   rR   rF   rb   )rW   rI   r?   r   r   r   remove_watch   s   
"zInotify.remove_watchc                 C  s   | j @ | js3d| _| j| jv r| j| j }t| j| | jr't| j	d n| 
  W d   dS W d   dS W d   dS 1 sFw   Y  dS )z?Closes the inotify instance and removes all associated watches.T   !N)re   rf   rq   ro   r   rR   rg   rh   writerj   _close_resources)rW   r?   r   r   r   close   s   
"zInotify.close)event_buffer_sizer   list[InotifyEvent]c             
     s  d fdd}d}	 zW j   jrg W  d	   W S d _W d	   n1 s)w   Y    r9t j|} j  d
 _ jrQ   g W  d	   W S W d	   n1 s[w   Y  W n' ty } z|j	t	j
kruW Y d	}~q	|j	t	jkrg W  Y d	}~S  d	}~ww 	  j  g }t|D ]\}}}}	|dkrq j| }
|	rtj|
|	n|
}t||||	|}|jr | nb|jr# |}| jv r j| } j|= | j|j< |j j|<  jr j D ]%}||tjj  r j|}|||j}| j|< | j|< qtj|
|	}t||||	|}|jr9 j|} j| |kr9 j|= | |  jre|j!re|j"rez	 #| j$ W n
 ty]   Y qw |%|| qW d	   |S 1 srw   Y  |S )z*Reads events from inotify and yields them.r   rJ   rM   r   c              
     s   g }t | D ]a\}}}|D ]6}tt' t j||} | j}t	|t
jt
jB d||}|| W d    n1 s?w   Y  q|D ] }	t j||	} jt j| }
t	|
t
jd|	|}|| qGq|S r\   )rh   walk
contextlibsuppressOSErrorrI   joinrw   rs   r   r   r-   r9   appendro   dirname)r   eventsrootdirnames	filenamesr   	full_pathwd_direfilenamewd_parent_dirrV   r   r   _recursive_simulate  s:   
z0Inotify.read_events.<locals>._recursive_simulate    TNFrO   )r   rJ   rM   r   )&re   rf   rg   rn   rh   readrR   r   r   errnoEINTREBADFrF   _parse_event_bufferrp   rI   r   r   is_moved_fromr   is_moved_tor   ro   r   r~   copy
startswithsepencoder   replace
is_ignoredr   is_directory	is_createrw   rs   extend)rW   r   r   event_bufferr   
event_listr?   r@   rA   rC   wd_pathr   inotify_eventmove_src_pathmoved_wdrq   _move_to_pathrI   r   rV   r   read_events  s   






33zInotify.read_eventsc                 C  s(   t | j t | j t | j d S r   )rh   r   rR   r^   rj   rV   r   r   r   r     s   zInotify._close_resourcesr@   c          	      C  s   t j|sttjt tj|| || |r=t |D ] \}}}|D ]}t j	||}t j
|r5q%| || q%qdS dS )a&  Adds a watch (optionally recursively) for the given directory path
        to monitor events specified by the mask.

        :param path:
            Path to monitor
        :param recursive:
            ``True`` to monitor recursively.
        :param mask:
            Event bit mask.
        N)rh   rI   ru   r   r   ENOTDIRstrerrorrw   r   r   islink)	rW   rI   r@   rG   r   r   rU   r   r   r   r   r   rv     s   zInotify._add_dir_watchc                 C  s6   t | j||}|dkrt  || j|< || j|< |S )zAdds a watch for the given path to monitor events specified by the
        mask.

        :param path:
            Path to monitor
        :param mask:
            Event bit mask.
        rO   )r   rR   rF   rb   ro   rp   )rW   rI   r@   r?   r   r   r   rw     s   	

zInotify._add_watchc                  C  sR   t  } | tjkrttjd| tjkrttjd| tjkr't| t| dS )z#Raises errors for inotify failures.zinotify watch limit reachedzinotify instance limit reachedN)	ctypes	get_errnor   ENOSPCr   EMFILEEACCESrh   r   )errr   r   r   rb     s   


zInotify._raise_errorr   &Generator[tuple[int, int, int, bytes]]c                 c  s|    d}|d t | kr<td| |\}}}}| |d |d |  d}|d| 7 }||||fV  |d t | ksdS dS )a  Parses an event buffer of ``inotify_event`` structs returned by
        inotify::

            struct inotify_event {
                __s32 wd;            /* watch descriptor */
                __u32 mask;          /* watch mask */
                __u32 cookie;        /* cookie to synchronize two events */
                __u32 len;           /* length (including nulls) of name */
                char  name[0];       /* stub for possible name */
            };

        The ``cookie`` member of this struct is used to pair two related
        events, for example, it pairs an IN_MOVED_FROM event with an
        IN_MOVED_TO event.
        r   r   iIII    N)rB   structunpack_fromrstrip)r   ir?   r@   rA   lengthrC   r   r   r   r     s   zInotify._parse_event_buffer)rI   rJ   rG   rK   rH   rL   rM   rN   rM   r{   rM   rJ   ra   )rM   rN   )r   r   rM   r   )r   r   rM   rN   )rI   rJ   rM   rN   )r   r{   rM   r   )rI   rJ   r@   r{   rG   rK   rM   rN   )rI   rJ   r@   r{   rM   r{   )r   rJ   rM   r   )r"   r#   r$   rD   rz   propertyrH   rI   r~   rT   r   r   r   r   r   r   DEFAULT_EVENT_BUFFER_SIZEr   r   rv   rw   staticmethodrb   r   r   r   r   r   rF      s2    
-




	

t

rF   c                   @  sv  e Zd ZdZdGddZedHddZedIddZedIddZedIddZ	edHddZ
edJddZedJddZedJddZedJddZedJd d!ZedJd"d#ZedJd$d%ZedJd&d'ZedJd(d)ZedJd*d+ZedJd,d-ZedJd.d/ZedJd0d1ZedJd2d3ZedJd4d5ZedKd7d8ZdLd;d<ZdLd=d>ZdId?d@ZedMdBdCZdNdDdEZ dFS )Or   a	  Inotify event struct wrapper.

    :param wd:
        Watch descriptor
    :param mask:
        Event mask
    :param cookie:
        Event cookie
    :param name:
        Base name of the event source path.
    :param src_path:
        Full event source path.
    r?   r{   r@   rA   rC   rJ   r   rM   rN   c                 C  s"   || _ || _|| _|| _|| _d S r   )_wd_mask_cookie_name	_src_path)rW   r?   r@   rA   rC   r   r   r   r   rz     s
   
zInotifyEvent.__init__c                 C  r|   r   )r   rV   r   r   r   r        zInotifyEvent.src_pathc                 C  r|   r   )r   rV   r   r   r   r?     r   zInotifyEvent.wdc                 C  r|   r   )r   rV   r   r   r   r@     r   zInotifyEvent.maskc                 C  r|   r   )r   rV   r   r   r   rA     r   zInotifyEvent.cookiec                 C  r|   r   )r   rV   r   r   r   rC     r   zInotifyEvent.namerK   c                 C     | j tj@ dkS r\   )r   r   r&   rV   r   r   r   	is_modify     zInotifyEvent.is_modifyc                 C  r   r\   )r   r   r(   rV   r   r   r   is_close_write  r   zInotifyEvent.is_close_writec                 C  r   r\   )r   r   r)   rV   r   r   r   is_close_nowrite  r   zInotifyEvent.is_close_nowritec                 C  r   r\   )r   r   r*   rV   r   r   r   is_open
  r   zInotifyEvent.is_openc                 C  r   r\   )r   r   r%   rV   r   r   r   	is_access  r   zInotifyEvent.is_accessc                 C  r   r\   )r   r   r.   rV   r   r   r   	is_delete  r   zInotifyEvent.is_deletec                 C  r   r\   )r   r   r/   rV   r   r   r   is_delete_self  r   zInotifyEvent.is_delete_selfc                 C  r   r\   )r   r   r-   rV   r   r   r   r     r   zInotifyEvent.is_createc                 C  r   r\   )r   r   r+   rV   r   r   r   r     r   zInotifyEvent.is_moved_fromc                 C  r   r\   )r   r   r,   rV   r   r   r   r   "  r   zInotifyEvent.is_moved_toc                 C  r   r\   )r   r   r1   rV   r   r   r   is_move&  r   zInotifyEvent.is_movec                 C  r   r\   )r   r   r0   rV   r   r   r   is_move_self*  r   zInotifyEvent.is_move_selfc                 C  r   r\   )r   r   r'   rV   r   r   r   	is_attrib.  r   zInotifyEvent.is_attribc                 C  r   r\   )r   r   r4   rV   r   r   r   r   2  r   zInotifyEvent.is_ignoredc                 C  s   | j p| jp| jtj@ dkS r\   )r   r   r   r   r9   rV   r   r   r   r   6  s   zInotifyEvent.is_directory"tuple[bytes, int, int, int, bytes]c                 C  s   | j | j| j| j| jfS r   )r   r   r   r   r   rV   r   r   r   key=  s   zInotifyEvent.keyr   objectc                 C  s   t |tstS | j|jkS r   
isinstancer   NotImplementedr   rW   r   r   r   r   __eq__A     
zInotifyEvent.__eq__c                 C  s   t |tstS | j|jkS r   r   r   r   r   r   __ne__F  r   zInotifyEvent.__ne__c                 C  s
   t | jS r   )hashr   rV   r   r   r   __hash__K  s   
zInotifyEvent.__hash__strc                 C  sJ   g }t tD ]}|dr|dvrtt|}| |@ r|| qd|S )NIN_>   r1   r;   |)dirr   r   getattrr   r   )r@   maskscc_valr   r   r   _get_mask_stringN  s   


zInotifyEvent._get_mask_stringc                 C  sF   dt | j d| jd| j d| | j d| j dt| j	dS )N<z: src_path=z, wd=z, mask=z	, cookie=z, name=>)
typer"   r   r?   r   r@   rA   rh   fsdecoderC   rV   r   r   r   __repr__X  s   

zInotifyEvent.__repr__N)r?   r{   r@   r{   rA   r{   rC   rJ   r   rJ   rM   rN   r   r   ra   )rM   r   )r   r   rM   rK   )r@   r{   rM   r   )rM   r   )!r"   r#   r$   rD   rz   r   r   r?   r@   rA   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sf    



	r   )3
__future__r   r   r   ctypes.utilr   rh   r]   r   rc   r   r   r   	functoolsr   typingr   watchdog.utilsr   collections.abcr	   CDLLlibcrk   r   error	CFUNCTYPEr   r   r
   r   r&   r'   r+   r,   r-   r.   r/   r6   r(   r)   r*   rr   	Structurer>   sizeof
EVENT_SIZEDEFAULT_NUM_EVENTSr   rF   r   r   r   r   r   <module>   s\    
8
  P