
    zcK                        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 d dlmZm	Z	m
Z
 d dlmZ  ej        d          Z eed          r eed          r eed          s edej        z              ej        eee	e
d	
          def          Z  ej        eee
d	
          def          Z  ej        ed	
          def          Z G d d          Z ed ej        ej        ej        ej        ej        ej        ej        ej        ej        g	          Z G d dej                   Z! ej"        e!          Z#dZ$e$e#dz   z  Z% G d d          Z& G d d          Z'dS )    N)reduce)c_intc_char_pc_uint32)UnsupportedLibcinotify_initinotify_add_watchinotify_rm_watchz"Unsupported libc version found: %sT)	use_errnoc                       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z  Ze	e
z  ZdZdZdZdZdZdZdZdZdZ e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                     | |z  S N xys     <lib/python3.11/site-packages/watchdog/observers/inotify_c.py<lambda>zInotifyConstants.<lambda>O   s
    QU     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_CLOSE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   -   s        IIIN!GMKIINL  00Hk)G JMJ JNNKHJ F
 M" JKKKr    r   c                     | |z  S r   r   r   s     r   r   r   e   s
    Q r    c                   4    e Zd ZdZdefdefdefdefdefgZdS )inotify_event_structa  
    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@   r   sH          u"8$!"	$HHHr    r@   r   r   c                       e Zd ZdZdefdZed             Zed             Zed             Z	ed             Z
d Zd	 Zd
 Zd Zd Zd ZefdZd Zd Zed             Zed             ZdS )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.
    Fc                    t                      }|dk    rt                                           || _        t	          j                    | _        t                      | _        t                      | _	        || _
        || _        || _        t          j                            |          r|                     |||           n|                     ||           t                      | _        d S )N)r   rI   _raise_error_inotify_fd	threadingLock_lockdict_wd_for_path_path_for_wd_path_event_mask_is_recursiveospathisdir_add_dir_watch
_add_watch_moved_from_events)selfrX   	recursive
event_mask
inotify_fds        r   __init__zInotify.__init__   s    !^^
 	#  """%^%%
 !FF FF
%&7== 	.i<<<<OOD*---"&&&r    c                     | j         S )z)The event mask for this inotify instance.)rU   r]   s    r   r_   zInotify.event_mask        r    c                     | j         S )z.The path associated with the inotify instance.)rT   rc   s    r   rX   zInotify.path   s     zr    c                     | j         S )z0Whether we are watching directories recursively.)rV   rc   s    r   is_recursivezInotify.is_recursive   s     !!r    c                     | j         S )z9The file descriptor associated with the inotify instance.)rM   rc   s    r   fdz
Inotify.fd   rd   r    c                 ,    t                      | _        dS )z)Clear cached records of MOVED_FROM eventsN)rQ   r\   rc   s    r   clear_move_recordszInotify.clear_move_records   s    "&&&r    c                 P    |j         | j        v r| j        |j                  j        S dS )z
        The source path corresponding to the given MOVED_TO event.

        If the source path is outside the monitored directories, None
        is returned instead.
        N)rC   r\   src_path)r]   destination_events     r   source_for_movezInotify.source_for_move   s1     #t'>> 	*+<+CDMM4r    c                 $    || j         |j        <   dS )zf
        Save this event as the source event for future MOVED_TO events to
        reference.
        N)r\   rC   )r]   events     r   remember_move_from_eventz Inotify.remember_move_from_event   s    
 16---r    c                 |    | j         5  |                     || j                   ddd           dS # 1 swxY w Y   dS )zn
        Adds a watch for the given path.

        :param path:
            Path to begin monitoring.
        N)rP   r[   rU   )r]   rX   s     r   	add_watchzInotify.add_watch   s     Z 	4 	4OOD$"2333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   155c                     | j         5  | j                            |          }| j        |= t	          | j        |          dk    rt                                           ddd           dS # 1 swxY w Y   dS )z
        Removes a watch for the given path.

        :param path:
            Path string for which the watch will be removed.
        rK   N)rP   rR   poprS   r
   rM   rI   rL   )r]   rX   rA   s      r   remove_watchzInotify.remove_watch   s     Z 	' 	'"&&t,,B!"% 0"55; '$$&&&		' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   AA**A.1A.c                    | j         5  | j        | j        v r'| j        | j                 }t          | j        |           	 t          j        | j                   n# t          $ r Y nw xY wddd           dS # 1 swxY w Y   dS )zQ
        Closes the inotify instance and removes all associated watches.
        N)rP   rT   rR   r
   rM   rW   closeOSError)r]   rA   s     r   ry   zInotify.close   s     Z 		 		zT.. 7&tz2 !12666)****   		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s4   6A6AA6
A&#A6%A&&A66A:=A:c           	           fd}d}	 	 t          j         j        |          }n-# t          $ r }|j        t          j        k    rY d}~A d}~ww xY w	  j        5  g }t                              |          D ]i\  }}}}	|dk    r j	        |         }
|	r t           j
                            |
|	          n|
}t          ||||	|          }|j        r                     |           nF|j        r>                     |          }| j        v r j        |         } j        |= | j        |j        <   |j         j	        |<    j        r j                                                                        D ]\  }}|                    |t           j
        j                                        z             rI j                            |          }|                    ||j                  }| j        |<   | j	        |<   t           j
                            |
|	          }t          ||||	|          }|j        r5 j	                            |          } j        |         |k    r j        |= |                    |            j        rZ|j        rS|j        rL	                      | j                    n# t          $ r Y Gw xY w|!                     ||                     k	 ddd           n# 1 swxY w Y   |S )z<
        Reads events from inotify and yields them.
        c           	      h   g }t          j        |           D ]\  }}}|D ]}	 t           j                            ||          }                    |j                  }t          |t          j        t          j	        z  d||          }|
                    |           ~# t          $ r Y w xY w|D ]~}	t           j                            ||	          }j        t           j                            |                   }
t          |
t          j        d|	|          }|
                    |           |S Nr   )rW   walkrX   joinr[   rU   InotifyEventr   r,   r9   appendrz   rR   dirname)rm   eventsrootdirnames	filenamesr   	full_pathwd_direfilenamewd_parent_dirr]   s              r   _recursive_simulatez0Inotify.read_events.<locals>._recursive_simulate  sI   F-/WX->-> % %)h	'  G$&GLLw$?$?	!%D<L!M!M("$4$>AQAZ$Z\]_fhqs sa(((("    ) % %H "T8 < <I$($5bgooi6P6P$QM$%'7'A1hPY[ [AMM!$$$$% Ms   A:B
B,+B,NTrK   )"rW   readrM   rz   errnoEINTRrP   rI   _parse_event_bufferrS   rX   r   r   is_moved_fromrr   is_moved_toro   rR   rm   rg   copyitems
startswithsepencoderv   replace
is_ignoredr   is_directory	is_creater[   rU   extend)r]   event_buffer_sizer   event_bufferr   
event_listrA   rB   rC   rE   wd_pathrm   inotify_eventmove_src_pathmoved_wdrT   _wd_move_to_pathrX   s   `                  r   read_eventszInotify.read_events   s   	 	 	 	 	( 	!wt'79JKK   7ek) HHHH	
 Z 4	E 4	EJ*1*E*El*S*S 2E 2E&D&$8 +B/:>K27<<666G ,RvtX N N . S11-@@@@". S$($8$8$G$GM$(99 P#'#4]#C -m<DL)-*@A6C6L)(3, P.2.?.D.D.F.F.L.L.N.N P P
s#(#3#3MBGKDVDVDXDX4X#Y#Y !P/3/@/D/DU/K/KH49MM-Q^Qg4h4hMGOD$5m$DBOD$5h$?!w||GT::H$0T64$R$RM + ,0044D(."4 4 -d3!!-000% E-*D E)3E!$2BCCCC" ! ! ! ! %%&9&9(&C&CDDDe2E4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	E 4	El sQ   & 
AA
AAH:K0J21K02
K <K0?K  #K00K47K4c                    t           j                            |          s7t          t          j        t          j        t          j                  |          |                     ||           |rvt          j        |          D ]c\  }}}|D ]X}t           j        	                    ||          }t           j        
                    |          rB|                     ||           Ybd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)rW   rX   rY   rz   r   ENOTDIRstrerrorr[   r~   r   islink)	r]   rX   r^   rB   r   r   _r   r   s	            r   rZ   zInotify._add_dir_watch^  s     w}}T"" 	K%-U])C)CTJJJd### 	5%'WT]] 5 5!h' 5 5G "T7 ; ;Iw~~i00 ! OOIt4444	5	5 	55 5r    c                     t          | j        ||          }|dk    rt                                           || j        |<   || j        |<   |S )z
        Adds a watch for the given path to monitor events specified by the
        mask.

        :param path:
            Path to monitor
        :param mask:
            Event bit mask.
        rK   )r	   rM   rI   rL   rR   rS   )r]   rX   rB   rA   s       r   r[   zInotify._add_watchu  sU     t/t<<8 	#  """"$$ $"	r    c                  8   t          j                    } | t          j        k    rt	          t          j        d          | t          j        k    rt	          t          j        d          | t          j        k    rdS t	          | t          j        |                     )z5
        Raises errors for inotify failures.
        zinotify watch limit reachedzinotify instance limit reachedN)	ctypes	get_errnor   ENOSPCrz   EMFILEEACCESrW   r   )errs    r   rL   zInotify._raise_error  s    
   %, 		1%,(EFFFEL  	1%,(HIIIEL  	1 D#r{3//000r    c              #     K   d}|dz   t          |           k    rit          j        d| |          \  }}}}| |dz   |dz   |z                                d          }|d|z   z  }||||fV  |dz   t          |           k    gd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)rD   structunpack_fromrstrip)r   irA   rB   rC   lengthrE   s          r   r   zInotify._parse_event_buffer  s      $ "fL))) 	)'-'9&,PQ'R'R$BffBq2v 67>>uEEDfAdFD((((	 "fL))) 	) 	) 	) 	) 	)r    N)r!   r"   r#   rF   WATCHDOG_ALL_EVENTSra   propertyr_   rX   rg   ri   rk   ro   rr   rt   rw   ry   DEFAULT_EVENT_BUFFER_SIZEr   rZ   r[   staticmethodrL   r   r   r    r   rI   rI      ss       	 	 (-9L ) ) ) )*     X    X " " X"     X ) ) )
 
 
6 6 64 4 4' ' '   -F ^ ^ ^ ^B5 5 5.  " 1 1 \1  ) ) \) ) )r    rI   c                      e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Zd Zd Zed             Zd ZdS )r   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.
    c                 L    || _         || _        || _        || _        || _        d S r   )r   _mask_cookie_name	_src_path)r]   rA   rB   rC   rE   rm   s         r   ra   zInotifyEvent.__init__  s)    

!r    c                     | j         S r   )r   rc   s    r   rm   zInotifyEvent.src_path  s
    ~r    c                     | j         S r   )r   rc   s    r   rA   zInotifyEvent.wd  s	    xr    c                     | j         S r   )r   rc   s    r   rB   zInotifyEvent.mask  
    zr    c                     | j         S r   )r   rc   s    r   rC   zInotifyEvent.cookie  s
    |r    c                     | j         S r   )r   rc   s    r   rE   zInotifyEvent.name  r   r    c                 2    | j         t          j        z  dk    S r}   )r   r   r%   rc   s    r   	is_modifyzInotifyEvent.is_modify      z,66::r    c                 2    | j         t          j        z  dk    S r}   )r   r   r'   rc   s    r   is_close_writezInotifyEvent.is_close_write      z,;;a??r    c                 2    | j         t          j        z  dk    S r}   )r   r   r(   rc   s    r   is_close_nowritezInotifyEvent.is_close_nowrite  s    z,==AAr    c                 2    | j         t          j        z  dk    S r}   )r   r   r$   rc   s    r   	is_accesszInotifyEvent.is_access  r   r    c                 2    | j         t          j        z  dk    S r}   )r   r   r-   rc   s    r   	is_deletezInotifyEvent.is_delete  r   r    c                 2    | j         t          j        z  dk    S r}   )r   r   r.   rc   s    r   is_delete_selfzInotifyEvent.is_delete_self  r   r    c                 2    | j         t          j        z  dk    S r}   )r   r   r,   rc   s    r   r   zInotifyEvent.is_create  r   r    c                 2    | j         t          j        z  dk    S r}   )r   r   r*   rc   s    r   r   zInotifyEvent.is_moved_from  s    z,::Q>>r    c                 2    | j         t          j        z  dk    S r}   )r   r   r+   rc   s    r   r   zInotifyEvent.is_moved_to  s    z,881<<r    c                 2    | j         t          j        z  dk    S r}   )r   r   r1   rc   s    r   is_movezInotifyEvent.is_move   s    z,44q88r    c                 2    | j         t          j        z  dk    S r}   )r   r   r/   rc   s    r   is_move_selfzInotifyEvent.is_move_self  s    z,99A==r    c                 2    | j         t          j        z  dk    S r}   )r   r   r&   rc   s    r   	is_attribzInotifyEvent.is_attrib  r   r    c                 2    | j         t          j        z  dk    S r}   )r   r   r4   rc   s    r   r   zInotifyEvent.is_ignored  s    z,77!;;r    c                 N    | j         p| j        p| j        t          j        z  dk    S r}   )r   r   r   r   r9   rc   s    r   r   zInotifyEvent.is_directory  s1    
 # >t'8 >: 0 99A=	?r    c                 B    | j         | j        | j        | j        | j        fS r   )r   r   r   r   r   rc   s    r   keyzInotifyEvent.key  s    ~txT\4:MMr    c                 "    | j         |j         k    S r   r   r]   r   s     r   __eq__zInotifyEvent.__eq__      x=,,,r    c                 "    | j         |j         k    S r   r   r   s     r   __ne__zInotifyEvent.__ne__  r   r    c                 *    t          | j                  S r   )hashr   rc   s    r   __hash__zInotifyEvent.__hash__"  s    DH~~r    c                     g }t          t                    D ]J}|                    d          r3|dvr/t          t          |          }| |z  r|                    |           Kd                    |          }|S )NIN_)r;   r0   r1   |)dirr   r   getattrr   r   )rB   maskscc_valmask_strings        r   _get_mask_stringzInotifyEvent._get_mask_string%  s    %&& 	$ 	$A||E"" $q0X'X $ 0!44%< $LLOOOhhuoor    c           	          |                      | j                  }d}|t          |           j        | j        | j        || j        t          j        | j	                  fz  S )Nz5<%s: src_path=%r, wd=%d, mask=%s, cookie=%d, name=%s>)
r   rB   typer!   rm   rA   rC   rW   fsdecoderE   )r]   r   ss      r   __repr__zInotifyEvent.__repr__0  sV    ++DI66CDJJ'KTY!7!79 9 	9r    N) r!   r"   r#   rF   ra   r   rm   rA   rB   rC   rE   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     s        " " "   X   X   X   X   X ; ; X; @ @ X@ B B XB ; ; X; ; ; X; @ @ X@ ; ; X; ? ? X? = = X= 9 9 X9 > > X> ; ; X; < < X< ? ? X? N N XN- - -- - -     \9 9 9 9 9r    r   )(rW   r   r   rN   r   ctypes.util	functoolsr   r   r   r   watchdog.utilsr   CDLLlibchasattrr   	CFUNCTYPEr	   r
   r   r   r%   r&   r*   r+   r,   r-   r.   r6   r'   r   	Structurer@   sizeof
EVENT_SIZEDEFAULT_NUM_EVENTSr   rI   r   r   r    r   <module>r     s  $ 
			                  , , , , , , , , , , * * * * * *v{4wt^$$ MGD-..MGD,--M />K
L
LLV$F$UE8XQUVVV$! !  L#6#E5(dKKK     7v666T 3 3 3 3 3 3 3 3n f""&$""'''
  $ $ $ $ $6+ $ $ $( V]/00
 .*r/B c) c) c) c) c) c) c) c)L	C9 C9 C9 C9 C9 C9 C9 C9 C9 C9r    