
    $gO                    *   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rd dlmZ  ej        d          Z eed          r eed	          r eed
          sdej         Z ee            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'        ej(        ej)        g          Z* G d dej+                  Z, ej-        e,          Z.dZ/e/e.dz   z  Z0 G d d          Z1 G 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                      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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     \/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/watchdog/observers/inotify_c.py<lambda>zInotifyConstants.<lambda>B   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_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 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"   Z   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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!   rB   rB   k   sH        
 
 
u		8		HHHr#   rB   r   r   c                     e Zd ZdZdddd)dZed*d            Zed+d            Zed,d            Zed*d            Z	d-dZ
d.dZd/dZd0dZd0dZd-dZedd1d Zd-d!Zd2d#Zd3d$Zed-d%            Zed4d(            ZdS )5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bytesrL   boolrM   
int | NonereturnNonec                  t                      }|dk    rt                                           || _        t	          j                    | _        d| _        d| _        t          j
                    \  | _        | _        i | _        i | _        || _        |t           }|| _        || _        t          j                            |          r|                     |||           n|                     ||           i | _        d S )NFT)rL   )r   rK   _raise_error_inotify_fd	threadingLock_lock_closed_waiting_to_readospipe_kill_r_kill_w_wd_for_path_path_for_wd_pathWATCHDOG_ALL_EVENTS_event_mask_is_recursiverN   isdir_add_dir_watch
_add_watch_moved_from_events)selfrN   rL   rM   
inotify_fds        r!   __init__zInotify.__init__   s    !^^
  """%^%%
 $%'WYY"dl /1.0
,J%&7== 	.jIFFFFOOD*---;=r#   intc                    | j         S )z)The event mask for this inotify instance.)re   rk   s    r!   rM   zInotify.event_mask        r#   c                    | j         S )z.The path associated with the inotify instance.)rc   rp   s    r!   rN   zInotify.path   s     zr#   c                    | j         S )z0Whether we are watching directories recursively.)rf   rp   s    r!   is_recursivezInotify.is_recursive   s     !!r#   c                    | j         S )z9The file descriptor associated with the inotify instance.)rW   rp   s    r!   fdz
Inotify.fd   rq   r#   c                    i | _         dS )z)Clear cached records of MOVED_FROM eventsN)rj   rp   s    r!   clear_move_recordszInotify.clear_move_records   s    "$r#   destination_eventInotifyEventbytes | Nonec                P    |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)rE   rj   src_path)rk   ry   s     r!   source_for_movezInotify.source_for_move   s/     #t'>>>*+<+CDMMtr#   eventc                $    || j         |j        <   dS )z]Save this event as the source event for future MOVED_TO events to
        reference.
        N)rj   rE   )rk   r   s     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 )zeAdds a watch for the given path.

        :param path:
            Path to begin monitoring.
        N)rZ   ri   re   )rk   rN   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.
        rU   N)rZ   ra   poprb   r   rW   rK   rV   )rk   rN   rC   s      r!   remove_watchzInotify.remove_watch   s     Z 	' 	'"&&t,,B!"% 0"55;;$$&&&		' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   AA**A.1A.c                8   | j         5  | j        srd| _        | j        | j        v r'| j        | j                 }t	          | j        |           | j        rt          j        | j	        d           n| 
                                 ddd           dS # 1 swxY w Y   dS )z?Closes the inotify instance and removes all associated watches.T   !N)rZ   r[   rc   ra   r   rW   r\   r]   writer`   _close_resources)rk   rC   s     r!   closezInotify.close   s    Z 	, 	,< ,#:!222*4:6B$T%5r:::( , HT\40000))+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   A:BBB)event_buffer_sizer   list[InotifyEvent]c          	     `    d
 fd}d}	 	  j         5   j        rg cddd           S d _        ddd           n# 1 swxY w Y   t          j         j         j        gg g             j         5  d _         j        r"                                  g cddd           S 	 ddd           n# 1 swxY w Y   t          j         j        |          }nI# t          $ r<}|j
        t          j        k    rY d}~|j
        t          j        k    rg cY d}~S  d}~ww xY w	  j         5  g }t                              |          D ]R\  }}}}	|d	k    r j        |         }
|	r t          j                            |
|	          n|
}t%          ||||	|          }|j        r                     |           n1|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         r3 j                            |          } j        |         |k    r j        |= |!                    |            j        rZ|j"        rS|j#        rL	  $                    | j%                   n# t          $ r Y 0w xY w|&                     ||                     T	 ddd           n# 1 swxY w Y   |S )z*Reads events from inotify and yields them.r}   rO   rR   r   c           
        g }t          j        |           D ]8\  }}}|D ]}t          j        t                    5  t           j                            ||          }                    |j                  }t          |t          j        t          j        z  d||          }|                    |           d d d            n# 1 swxY w Y   |D ]~}	t           j                            ||	          }j        t           j                            |                   }
t          |
t          j        d|	|          }|                    |           :|S Nr   )r]   walk
contextlibsuppressOSErrorrN   joinri   re   rz   r   r/   r;   appendra   dirname)r}   eventsrootdirnames	filenamesr   	full_pathwd_direfilenamewd_parent_dirrk   s              r!   _recursive_simulatez0Inotify.read_events.<locals>._recursive_simulate  s   F-/WX->-> % %)h	' ) )G#,W55 
) 
)$&GLLw$?$?	!%D<L!M!M(",69I9RR#%  a(((
) 
) 
) 
) 
) 
) 
) 
) 
) 
) 
) 
) 
) 
) 
) !* 
% 
%H "T8 < <I$($5bgooi6P6P$QM$%(2 ! A MM!$$$$
% Ms   A;CC
CNTFrU   )r}   rO   rR   r   )'rZ   r[   r\   selectrW   r_   r   r]   readr   errnoEINTREBADFrK   _parse_event_bufferrb   rN   r   rz   is_moved_fromr   is_moved_tor~   ra   r}   rt   copy
startswithsepencoder   replace
is_ignoredr   is_directory	is_createri   re   extend)rk   r   r   event_bufferr   
event_listrC   rD   rE   rG   wd_pathr}   inotify_eventmove_src_pathmoved_wdrc   _move_to_pathrN   s   `                 r!   read_eventszInotify.read_events   s8   	 	 	 	 	 	8 	Z 1 1| "!1 1 1 1 1 1 1 1 -1D)	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 t/>BGGGZ " ",1D)| "--///!" " " " " " " """ " " " " " " " " " " " " " "  "wt'79JKK   7ek))HHHH7ek))IIIIII Z 1	E 1	EJ*1*E*El*S*S /E /E&D&$88+B/:>K27<<666G ,RvtX N N . S11-@@@@". S$($8$8$G$GM$(999#'#4]#C -m<DL)-*@A6C6L)(3, P)-):)?)?)A)A P P#(#3#3MBGKDVDVDXDX4X#Y#Y !P/3/@/D/DU/K/KH49MM-Q^Qg4h4hMGOD$5m$DBOD$5h$?!w||GT::H$0T64$R$RM + 4,0044D(."44 -d3!!-000$ E)C EH_ E!$2BCCCC" ! ! ! ! %%&9&9(&C&CDDD_/E1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	E 1	Ef s   C 	<C <C A  C A ,C 0$B.C "C .B22C 5B26C 
DD8DDDD%H#N#	M%$N#%
M3/N#2M33#N##N'*N'c                    t          j        | j                   t          j        | j                   t          j        | j                   d S r   )r]   r   rW   r_   r`   rp   s    r!   r   zInotify._close_resourcesp  s?    
!"""

r#   rD   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)r]   rN   rg   r   r   ENOTDIRstrerrorri   r   r   islink)	rk   rN   rD   rL   r   r   _r   r   s	            r!   rh   zInotify._add_dir_watchv  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 )zAdds a watch for the given path to monitor events specified by the
        mask.

        :param path:
            Path to monitor
        :param mask:
            Event bit mask.
        rU   )r   rW   rK   rV   ra   rb   )rk   rN   rD   rC   s       r!   ri   zInotify._add_watch  sR     t/t<<88  """"$$ $"	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"t	          | t          j        |                     dS )z#Raises errors for inotify failures.zinotify watch limit reachedzinotify instance limit reachedN)	ctypes	get_errnor   ENOSPCr   EMFILEEACCESr]   r   )errs    r!   rV   zInotify._raise_error  s       %,%,(EFFF%,%,(HIII%,#r{3//000 r#   r   &Generator[tuple[int, int, int, bytes]]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)rF   structunpack_fromrstrip)r   irC   rD   rE   lengthrG   s          r!   r   zInotify._parse_event_buffer  s      " "fL))))'-'9&,PQ'R'R$BffBR& 89@@GGDfAdFD((((	 "fL))))))))r#   )rN   rO   rL   rP   rM   rQ   rR   rS   rR   rn   rR   rO   rR   rP   )rR   rS   )ry   rz   rR   r{   )r   rz   rR   rS   )rN   rO   rR   rS   )r   rn   rR   r   )rN   rO   rD   rn   rL   rP   rR   rS   )rN   rO   rD   rn   rR   rn   )r   rO   rR   r   )r$   r%   r&   rH   rm   propertyrM   rN   rt   rv   rx   r~   r   r   r   r   DEFAULT_EVENT_BUFFER_SIZEr   r   rh   ri   staticmethodrV   r   r   r#   r!   rK   rK      s         :?Y] > > > > > >6       X     X " " " X"       X % % % %	 	 	 	6 6 6 64 4 4 4
' 
' 
' 
', , , ," 7P s s s s s sj   5 5 5 5,     1 1 1 \1 ) ) ) \) ) )r#   rK   c                  J   e Zd ZdZd,dZed-d            Zed.d            Zed.d            Zed.d            Z	ed-d            Z
ed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d            Zed/d             Zed0d"            Zd1d%Zd1d&Zd.d'Zed2d)            Zd3d*Z d+S )4rz   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.
    rC   rn   rD   rE   rG   rO   r}   rR   rS   c                L    || _         || _        || _        || _        || _        d S r   )_wd_mask_cookie_name	_src_path)rk   rC   rD   rE   rG   r}   s         r!   rm   zInotifyEvent.__init__  s)    

!r#   c                    | j         S r   )r   rp   s    r!   r}   zInotifyEvent.src_path  s
    ~r#   c                    | j         S r   )r   rp   s    r!   rC   zInotifyEvent.wd  s	    xr#   c                    | j         S r   )r   rp   s    r!   rD   zInotifyEvent.mask  
    zr#   c                    | j         S r   )r   rp   s    r!   rE   zInotifyEvent.cookie  s
    |r#   c                    | j         S r   )r   rp   s    r!   rG   zInotifyEvent.name  r   r#   rP   c                2    | j         t          j        z  dk    S r   )r   r   r(   rp   s    r!   	is_modifyzInotifyEvent.is_modify      z,66::r#   c                2    | j         t          j        z  dk    S r   )r   r   r*   rp   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+   rp   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,   rp   s    r!   is_openzInotifyEvent.is_open      z,44q88r#   c                2    | j         t          j        z  dk    S r   )r   r   r'   rp   s    r!   	is_accesszInotifyEvent.is_access  r   r#   c                2    | j         t          j        z  dk    S r   )r   r   r0   rp   s    r!   	is_deletezInotifyEvent.is_delete  r   r#   c                2    | j         t          j        z  dk    S r   )r   r   r1   rp   s    r!   is_delete_selfzInotifyEvent.is_delete_self  r   r#   c                2    | j         t          j        z  dk    S r   )r   r   r/   rp   s    r!   r   zInotifyEvent.is_create	  r   r#   c                2    | j         t          j        z  dk    S r   )r   r   r-   rp   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.   rp   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   r3   rp   s    r!   is_movezInotifyEvent.is_move  r   r#   c                2    | j         t          j        z  dk    S r   )r   r   r2   rp   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)   rp   s    r!   	is_attribzInotifyEvent.is_attrib  r   r#   c                2    | j         t          j        z  dk    S r   )r   r   r6   rp   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   r;   rp   s    r!   r   zInotifyEvent.is_directory%  s+    
 "ed&7e4:HXHa;ade;eer#   "tuple[bytes, int, int, int, bytes]c                B    | j         | j        | j        | j        | j        fS r   )r   r   r   r   r   rp   s    r!   keyzInotifyEvent.key,  s    ~txT\4:MMr#   r   objectc                Z    t          |t                    st          S | j        |j        k    S r   
isinstancerz   NotImplementedr  rk   r   s     r!   __eq__zInotifyEvent.__eq__0  *    -66 	"!!x=,,,r#   c                Z    t          |t                    st          S | j        |j        k    S r   r  r	  s     r!   __ne__zInotifyEvent.__ne__5  r  r#   c                *    t          | j                  S r   )hashr  rp   s    r!   __hash__zInotifyEvent.__hash__:  s    DH~~r#   strc                    g }t          t                    D ]J}|                    d          r3|dvr/t          t          |          }| |z  r|                    |           Kd                    |          S )NIN_>   r3   r=   |)dirr   r   getattrr   r   )rD   maskscc_vals       r!   _get_mask_stringzInotifyEvent._get_mask_string=  sz    %&& 	$ 	$A||E"" $q0L'L'L 0!44%< $LLOOOxxr#   c                    dt          |           j         d| j        d| j         d|                     | j                   d| j         dt          j        | j	                  dS )N<z: src_path=z, wd=z, mask=z	, cookie=z, name=>)
typer$   r}   rC   r  rD   rE   r]   fsdecoderG   rp   s    r!   __repr__zInotifyEvent.__repr__G  s    1T

# 1 1 1 1dg 1 1**49551 1@D1 1[++1 1 1	
r#   N)rC   rn   rD   rn   rE   rn   rG   rO   r}   rO   rR   rS   r   r   r   )rR   r  )r   r  rR   rP   )rD   rn   rR   r  )rR   r  )!r$   r%   r&   rH   rm   r   r}   rC   rD   rE   rG   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!   rz   rz     sV        " " " "    X    X    X    X    X ; ; ; X; @ @ @ X@ B B B XB 9 9 9 X9 ; ; ; X; ; ; ; X; @ @ @ X@ ; ; ; X; ? ? ? X? = = = X= 9 9 9 X9 > > > X> ; ; ; X; < < < X< f f f Xf N N N XN- - - -
- - - -
       \
 
 
 
 
 
r#   rz   )3
__future__r   r   r   ctypes.utilr   r]   r   r   rX   r   r   r   	functoolsr   typingr   watchdog.utilsr	   collections.abcr
   CDLLlibchasattrr   error	CFUNCTYPEr   r   r   r   r(   r)   r-   r.   r/   r0   r1   r8   r*   r+   r,   rd   	StructurerB   sizeof
EVENT_SIZEDEFAULT_NUM_EVENTSr   rK   rz   r   r#   r!   <module>r0     s   " " " " " "           				       , , , , , , , , , ,                   / / / / / / *))))))v{4wt^$$ &GGD:M,N,N &V]V]^bdvVwVw &;tz;;E

u
%
%%V$F$UE8XQUVVVXkmqWrss K#6#E5(dKKKM_aeLfgg 6v6667MNN4 4 4 4 4 4 4 4p f""&$""''')   $    )   , V]-..
 .*r/B z) z) z) z) z) z) z) z)z	I
 I
 I
 I
 I
 I
 I
 I
 I
 I
r#   