
    gR                    v   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j0                   Z ee        ej4                  eee
ed      d	ef      Z  ej4                  eeed      d
ef      Z  ej4                  ed      def      Z G d d      Z ed ej>                  ej@                  ejB                  ejD                  ejF                  ejH                  ejJ                  ejL                  ejN                  ejP                  ejR                  g      Z* G d dejV                        Z, ejZ                  e,      Z.dZ/e/e.dz   z  Z0 G d d      Z1 G d d      Z2y)    )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y)InotifyConstants                   @         i   i      i    i @  i   i   i   i   i    i   @l        c                    | |z  S N xys     Q/var/www/openai/venv/lib/python3.12/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 	
M& JKr#   r   c                    | |z  S r   r   r   s     r!   r"   r"   Z   s    Qr#   c                  2    e Zd ZdZdefdefdefdefdeffZy)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   s7    
 
u		8		Hr#   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ddZddZddZedddZddZd dZd!dZedd       Zed"d       Zy)#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_maskc               @    t               }|dk(  rt        j                          | _        t	        j
                          _        d _        d _        t        j                         \   _         _        t        t        d      rt        j                          _         j                   j#                   j                  t        j$                          j                   j#                   j                  t        j$                         d fd}| _        nd fd}| _        i  _        i  _        | _        |t.        }| _        | _        t        j4                  j7                  |      r j9                  |||       i  _        y  j;                  ||       i  _        y )	NFTpollc                 \    t         fd j                  j                         D              S )Nc              3  B   K   | ]  \  }}|j                   k(    y wr   _inotify_fd).0fd_selfs      r!   	<genexpr>z4Inotify.__init__.<locals>.do_poll.<locals>.<genexpr>   s#     S?Reb!2!1!11?Rs   )any_pollerrP   rX   s   r!   do_pollz!Inotify.__init__.<locals>.do_poll   s!    St||?P?P?RSSSr#   c                     t        j                   j                  j                  gg g       } j                  | d   v S Nr   )selectrT   _kill_r)resultrX   s    r!   	do_selectz#Inotify.__init__.<locals>.do_select   s9    (8(8$,,'GRP''6!944r#   )rL   returnbool)r   rK   _raise_errorrT   	threadingLock_lock_closed_is_readingospipera   _kill_whasattrr`   rP   r[   registerPOLLIN_check_inotify_fd_wd_for_path_path_for_wd_pathWATCHDOG_ALL_EVENTS_event_mask_is_recursivepathisdir_add_dir_watch
_add_watch_moved_from_events)rX   rz   rL   rM   
inotify_fdr]   rc   s   `      r!   __init__zInotify.__init__   sD   !^
  "%^^%
%'WWY"dl 66"!;;=DLLL!!$"2"2FMMBLL!!$,,>T &-D"5 &/D" /1.0
,J%&77==jIF <> OOD*-;=r#   c                    | j                   S )z)The event mask for this inotify instance.)rx   r\   s    r!   rM   zInotify.event_mask        r#   c                    | j                   S )z.The path associated with the inotify instance.)rv   r\   s    r!   rz   zInotify.path   s     zzr#   c                    | j                   S )z0Whether we are watching directories recursively.)ry   r\   s    r!   is_recursivezInotify.is_recursive   s     !!!r#   c                    | j                   S )z9The file descriptor associated with the inotify instance.rS   r\   s    r!   rV   z
Inotify.fd   r   r#   c                    i | _         y)z)Clear cached records of MOVED_FROM eventsN)r~   r\   s    r!   clear_move_recordszInotify.clear_move_records   s
    "$r#   c                z    |j                   | j                  v r#| j                  |j                      j                  S y)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   r~   src_path)rX   destination_events     r!   source_for_movezInotify.source_for_move   s9     ##t'>'>>**+<+C+CDMMMr#   c                6    || j                   |j                  <   y)z]Save this event as the source event for future MOVED_TO events to
        reference.
        N)r~   rE   )rX   events     r!   remember_move_from_eventz Inotify.remember_move_from_event   s     16-r#   c                ~    | j                   5  | j                  || j                         ddd       y# 1 sw Y   yxY w)zeAdds a watch for the given path.

        :param path:
            Path to begin monitoring.
        N)rj   r}   rx   )rX   rz   s     r!   	add_watchzInotify.add_watch   s)     ZZOOD$"2"23 ZZs   3<c                    | j                   5  | j                  j                  |      }| j                  |= t	        | j
                  |      dk(  rt        j                          ddd       y# 1 sw Y   yxY w)zRemoves a watch for the given path.

        :param path:
            Path string for which the watch will be removed.
        rO   N)rj   rt   popru   r   rT   rK   rg   )rX   rz   rC   s      r!   remove_watchzInotify.remove_watch   sZ     ZZ""&&t,B!!"% 0 0"5;$$&	 ZZs   AA,,A5c                t   | j                   5  | j                  sd| _        | j                  | j                  v r/| j                  | j                     }t	        | j
                  |       | j                  r!t        j                  | j                  d       n| j                          ddd       y# 1 sw Y   yxY w)z?Closes the inotify instance and removes all associated watches.T   !N)rj   rk   rv   rt   r   rT   rl   rm   writero   _close_resources)rX   rC   s     r!   closezInotify.close   s    ZZ<<#::!2!22**4::6B$T%5%5r:## HHT\\40))+ ZZs   BB..B7)event_buffer_sizec          	          d fd}d}	 	  j                   5   j                  rg cddd       S d _        ddd        j                         r t	        j
                   j                  |      } j                   5  d _         j                  r j                          g cddd       S 	 ddd       	  j                   5  g }t        j                  |      D ]  \  }}}}	|dk(  r j                  |   }
|	r t        j                  j!                  |
|	      n|
}t#        ||||	|      }|j$                  r j'                  |       ne|j(                  rX j+                  |      }| j,                  v r	 j,                  |   } j,                  |= | j,                  |j.                  <   |j.                   j                  |<    j0                  r j,                  j3                         D ]  }|j5                  |t        j                  j6                  j9                         z         s> j,                  j;                  |      }|j=                  ||j.                        }| j,                  |<   | j                  |<    t        j                  j!                  |
|	      }t#        ||||	|      }|j>                  r: j                  j;                  |      } j,                  |   |k(  r j,                  |= |jA                  |        j0                  s:|jB                  sH|jD                  sV	  jG                  | jH                         |jK                   ||              	 ddd       |S # 1 sw Y   ;xY w# 1 sw Y   xY w# t        $ rM}|j                  t        j                  k(  rY d}~|j                  t        j                  k(  rg cY d}~S  d}~ww xY w# t        $ r Y w xY w# 1 sw Y   S xY w)z*Reads events from inotify and yields them.c           
        g }t        j                  |       D ],  \  }}}|D ]  }t        j                  t              5  t         j
                  j                  ||      }j                  |j                        }t        |t        j                  t        j                  z  d||      }|j                  |       d d d         |D ]|  }	t         j
                  j                  ||	      }j                  t         j
                  j                  |         }
t        |
t        j                  d|	|      }|j                  |       ~ / |S # 1 sw Y   2xY wr_   )rm   walk
contextlibsuppressOSErrorrz   joinr}   rx   InotifyEventr   r/   r;   appendrt   dirname)r   eventsrootdirnames	filenamesr   	full_pathwd_direfilenamewd_parent_dirrX   s              r!   _recursive_simulatez0Inotify.read_events.<locals>._recursive_simulate  s   F-/WWX->)h	'G#,,W5$&GGLLw$?	!%D<L<L!M(",669I9R9RR#% a( 65  ( !*H "T8 <I$($5$5bggooi6P$QM$%(22 !A MM!$ !* .?0 M- 65s   A<EEr#   TNFrO   )r   bytesre   list[InotifyEvent])&rj   rk   rl   rs   rm   readrT   r   r   errnoEINTREBADFrK   _parse_event_bufferru   rz   r   r   is_moved_fromr   is_moved_tor   rt   r   r   copy
startswithsepencoder   replace
is_ignoredr   is_directory	is_creater}   rx   extend)rX   r   r   event_bufferr   
event_listrC   rD   rE   rG   wd_pathr   inotify_eventmove_src_pathmoved_wdrv   _move_to_pathrz   s   `                 r!   read_eventszInotify.read_events  sg   	8 ZZ||!  Z (,D$	   ))+#%774+;+;=N#OLZZ',D$||--/!  Z $   ZZJ*1*E*El*S&D&$8++B/:>277<<6G ,RvtX N ..11-@"..$($8$8$GM$(9(99#'#4#4]#C --m<DL))-*@*@A6C6L6L))(3,,)-):):)?)?)A#(#3#3MBGGKKDVDVDX4X#Y/3/@/@/D/DU/KH49MM-Q^QgQg4hMGOD$5$5m$DBOD$5$5h$? *B  "ww||GT:H$0T64$RM ++,,004D((."4 --d3!!-0$$)C)CH_H_!$2B2BC %%&9(&CD_ +T f Y  Z  Z  77ekk)77ekk)Ip # ! !] f s   N
 M0	N
 M0AN
 ;%M= 	N
 +N
  E&O3'C)O3O3 O3/O#O30M:5N
 =NN
 
	O O6OO OO #	O0,O3/O00O33O=c                    t        j                  | j                         t        j                  | j                         t        j                  | j                         y r   )rm   r   rT   ra   ro   r\   s    r!   r   zInotify._close_resources  s4    
!!"

r#   c                  t         j                  j                  |      s<t        t        j
                  t        j                  t        j
                        |      | j                  ||       |rxt        j                  |      D ]_  \  }}}|D ]T  }t         j                  j                  ||      }t         j                  j                  |      rC| j                  ||       V a yy)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)rm   rz   r{   r   r   ENOTDIRstrerrorr}   r   r   islink)	rX   rz   rD   rL   r   r   rW   r   r   s	            r!   r|   zInotify._add_dir_watch  s     ww}}T"%--U]])CTJJd#%'WWT]!h'G "T7 ;Iww~~i0 OOIt4	  ( &3 r#   c                    t        | j                  ||      }|dk(  rt        j                          || 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   rT   rK   rg   rt   ru   )rX   rz   rD   rC   s       r!   r}   zInotify._add_watch  sP     t//t<8  ""$$ $"	r#   c                 D   t        j                         } | t        j                  k(  rt	        t        j                  d      | t        j
                  k(  rt	        t        j
                  d      | t        j                  k7  rt	        | t        j                  |             y)z#Raises errors for inotify failures.zinotify watch limit reachedzinotify instance limit reachedN)	ctypes	get_errnor   ENOSPCr   EMFILEEACCESrm   r   )errs    r!   rg   zInotify._raise_error  su      %,,%,,(EFF%,,%,,(HII%,,#r{{3/00 r#   c              #     K   d}|dz   t        |       k  r\t        j                  d| |      \  }}}}| |dz   |dz   |z    j                  d      }|d|z   z  }||||f |dz   t        |       k  r[yyw)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'9&,PQ'R$BffBR&9@@GDfAdFD((	 "fL))s   A.A31A3)rz   r   rL   rf   rM   z
int | Nonere   Nonere   intre   r   rd   )re   r   )r   r   re   zbytes | None)r   r   re   r   )rz   r   re   r   )r   r   re   r   )rz   r   rD   r   rL   rf   re   r   )rz   r   rD   r   re   r   )r   r   re   z&Generator[tuple[int, int, int, bytes]])r$   r%   r&   rH   r   propertyrM   rz   r   rV   r   r   r   r   r   r   DEFAULT_EVENT_BUFFER_SIZEr   r   r|   r}   staticmethodrg   r   r   r#   r!   rK   rK      s     :?Y] +>Z       " "    %	64
'," 7P rh5,  1 1 ) )r#   rK   c                     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d"d       Zd#dZd#dZd dZed$d       Zd%dZ y)&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                J    || _         || _        || _        || _        || _        y r   )_wd_mask_cookie_name	_src_path)rX   rC   rD   rE   rG   r   s         r!   r   zInotifyEvent.__init__  s%    

!r#   c                    | j                   S r   )r   r\   s    r!   r   zInotifyEvent.src_path  s    ~~r#   c                    | j                   S r   )r   r\   s    r!   rC   zInotifyEvent.wd  s    xxr#   c                    | j                   S r   )r   r\   s    r!   rD   zInotifyEvent.mask      zzr#   c                    | j                   S r   )r   r\   s    r!   rE   zInotifyEvent.cookie  s    ||r#   c                    | j                   S r   )r   r\   s    r!   rG   zInotifyEvent.name  r   r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r(   r\   s    r!   	is_modifyzInotifyEvent.is_modify      zz,666::r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r*   r\   s    r!   is_close_writezInotifyEvent.is_close_write      zz,;;;a??r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r+   r\   s    r!   is_close_nowritezInotifyEvent.is_close_nowrite  s    zz,===AAr#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r,   r\   s    r!   is_openzInotifyEvent.is_open
      zz,444q88r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r'   r\   s    r!   	is_accesszInotifyEvent.is_access  r   r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r0   r\   s    r!   	is_deletezInotifyEvent.is_delete  r   r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r1   r\   s    r!   is_delete_selfzInotifyEvent.is_delete_self  r   r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r/   r\   s    r!   r   zInotifyEvent.is_create  r   r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r-   r\   s    r!   r   zInotifyEvent.is_moved_from  s    zz,:::Q>>r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r.   r\   s    r!   r   zInotifyEvent.is_moved_to"  s    zz,8881<<r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r3   r\   s    r!   is_movezInotifyEvent.is_move&  r   r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r2   r\   s    r!   is_move_selfzInotifyEvent.is_move_self*  s    zz,999A==r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r)   r\   s    r!   	is_attribzInotifyEvent.is_attrib.  r   r#   c                B    | j                   t        j                  z  dkD  S r_   )r   r   r6   r\   s    r!   r   zInotifyEvent.is_ignored2  s    zz,777!;;r#   c                z    | j                   xs. | j                  xs  | j                  t        j                  z  dkD  S r_   )r  r	  r   r   r;   r\   s    r!   r   zInotifyEvent.is_directory6  s6    
 ""ed&7&7e4::HXHaHa;ade;eer#   c                t    | j                   | j                  | j                  | j                  | j                  fS r   )r   r   r   r   r   r\   s    r!   keyzInotifyEvent.key=  s'    ~~txxT\\4::MMr#   c                `    t        |t              st        S | j                  |j                  k(  S r   
isinstancer   NotImplementedr  rX   r   s     r!   __eq__zInotifyEvent.__eq__A  '    -6!!xx=,,,,r#   c                `    t        |t              st        S | j                  |j                  k7  S r   r  r  s     r!   __ne__zInotifyEvent.__ne__F  r  r#   c                ,    t        | j                        S r   )hashr  r\   s    r!   __hash__zInotifyEvent.__hash__K  s    DHH~r#   c                    g }t        t              D ]@  }|j                  d      s|dvst        t        |      }| |z  s0|j	                  |       B dj                  |      S )NIN_>   r3   r=   |)dirr   r   getattrr   r   )rD   maskscc_vals       r!   _get_mask_stringzInotifyEvent._get_mask_stringN  sZ    %&A||E"q0L'L 0!4%<LLO	 '
 xxr#   c                    dt        |       j                   d| j                  d| j                   d| j	                  | 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   rm   fsdecoderG   r\   s    r!   __repr__zInotifyEvent.__repr__X  ss    T
##$K/@dggY O**49956i} M[[+.a1	
r#   N)rC   r   rD   r   rE   r   rG   r   r   r   re   r   r   r   rd   )re   z"tuple[bytes, int, int, int, bytes])r   objectre   rf   )rD   r   re   str)re   r,  )!r$   r%   r&   rH   r   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!   r   r     s   "           ; ; @ @ B B 9 9 ; ; ; ; @ @ ; ; ? ? = = 9 9 > > ; ; < < f f N N-
-
  
r#   r   )3
__future__r   r   r   ctypes.utilr   rm   r`   r   rh   r   r   r   	functoolsr   typingr   watchdog.utilsr	   collections.abcr
   CDLLlibcrp   r   error	CFUNCTYPEr   r   r   r   r(   r)   r-   r.   r/   r0   r1   r8   r*   r+   r,   rw   	StructurerB   sizeof
EVENT_SIZEDEFAULT_NUM_EVENTSr   rK   r   r   r#   r!   <module>r;     s   "     	    , ,    /)v{{4t^$GD:M,NV]^bdvVw.tzzl;E
u
%%V$F$$UE8XQUVXkmqWrs K#6##E5(dKM_aeLfg 6v67MN4 4p """"&&$$""""''''''))   $)) , V]]-.
 .*r/B K) K)\
I
 I
r#   