o
    cCK                     @   sj  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dZeedrBeedrBeedsIedej 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g	ZG dd dej Z!e"e!Z#dZ$e$e#d  Z%G dd dZ&G d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                   @   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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   `G:\Development\the-witcher-3-mod-manager\.venv\lib\site-packages\watchdog\observers\inotify_c.py<lambda>O       zInotifyConstants.<lambda>N)__name__
__module____qualname__	IN_ACCESS	IN_MODIFY	IN_ATTRIBIN_CLOSE_WRITEIN_CLOSE_NOWRITEZIN_OPENIN_MOVED_FROMIN_MOVED_TO	IN_CREATE	IN_DELETEIN_DELETE_SELFIN_MOVE_SELFIN_CLOSEIN_MOVEZ
IN_UNMOUNTZIN_Q_OVERFLOW
IN_IGNOREDZ
IN_ONLYDIRIN_DONT_FOLLOWZIN_EXCL_UNLINKZIN_MASK_ADDIN_ISDIRZ
IN_ONESHOTr   IN_ALL_EVENTSZ
IN_CLOEXECZIN_NONBLOCKr   r   r   r   r   -   sT    r   c                 C   r   r   r   r   r   r   r   r   e   r   r   c                   @   s0   e Zd Z	 defdefdefdefdefgZdS )inotify_event_structwdmaskcookielennameN)r   r    r!   r   r   r   _fields_r   r   r   r   r3   r   s    r3   r   r   c                   @   s   e Zd Z	 def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fddZdd Zdd Zedd Zed d! Zd"S )#InotifyFc                 C   s|   t  }|dkrt  || _t | _t | _t | _	|| _
|| _|| _tj|r2| ||| n| || t | _d S N)r   r:   _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)selfrI   	recursive
event_maskZ
inotify_fdr   r   r   __init__   s   
zInotify.__init__c                 C      	 | j S r   )rF   rN   r   r   r   rP         zInotify.event_maskc                 C   rR   r   )rE   rS   r   r   r   rI      rT   zInotify.pathc                 C   rR   r   )rG   rS   r   r   r   is_recursive   rT   zInotify.is_recursivec                 C   rR   r   )r>   rS   r   r   r   fd   rT   z
Inotify.fdc                 C   s   	 t  | _d S r   )rB   rM   rS   r   r   r   clear_move_records   s   zInotify.clear_move_recordsc                 C   s    	 |j | jv r| j|j  jS d S r   )r6   rM   src_path)rN   Zdestination_eventr   r   r   source_for_move   s   zInotify.source_for_movec                 C   s   	 || j |j< d S r   )rM   r6   )rN   eventr   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 r   )rA   rL   rF   )rN   rI   r   r   r   	add_watch   s   "zInotify.add_watchc                 C   sj   	 | j ' | j|}| j|= t| j|dkr#t  W d    d S W d    d S 1 s.w   Y  d S r;   )rA   rC   poprD   r	   r>   r:   r=   )rN   rI   r4   r   r   r   remove_watch   s   
"zInotify.remove_watchc              	   C   s   	 | j 5 | j| jv r| j| j }t| j| zt| j W n	 ty(   Y n	w W d    d S W d    d S 1 s<w   Y  d S r   )rA   rE   rC   r	   r>   rH   closeOSError)rN   r4   r   r   r   r_      s   	"zInotify.closec              
      sR  	  fdd}d }	 z	t  j|}W n' ty: } z|jtjkr'W Y d }~q	|jtjkr5g W  Y d }~S  d }~ww 	  j g }t	|D ]\}}}}	|dkrRqG j
| }
|	r`t j|
|	n|
}t||||	|}|jrs | 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r j
|} j| |kr j|= ||  jr|jr|jrz	  | j! W n
 ty   Y qGw |"|| qGW d    |S 1 s"w   Y  |S )Nc              
      s   g }t | D ]W\}}}|D ],}z!t j||} | j}t|tjtj	B d||}|
| W q ty:   Y qw |D ] }	t j||	} jt j| }
t|
tjd|	|}|
| q=q|S Nr   )rH   walkrI   joinrL   rF   InotifyEventr   r)   r1   appendr`   rC   dirname)rX   eventsrootdirnames	filenamesrf   	full_pathZwd_direfilenameZwd_parent_dirrS   r   r   _recursive_simulate  s,   z0Inotify.read_events.<locals>._recursive_simulateTr<   )#rH   readr>   r`   errnoZEINTREBADFrA   r:   _parse_event_bufferrD   rI   rc   rd   is_moved_fromr[   is_moved_torY   rC   rX   rU   copyitems
startswithsepencoder]   replace
is_ignoredre   is_directory	is_createrL   rF   extend)rN   Zevent_buffer_sizern   event_bufferrl   
event_listr4   r5   r6   r8   Zwd_pathrX   inotify_eventZmove_src_pathZmoved_wdrE   _wdZ_move_to_pathrI   r   rS   r   read_events   s|   








55zInotify.read_eventsc           	      C   s   	 t j|sttjt tj|| || |r>t |D ] \}}}|D ]}t j	||}t j
|r6q&| || q&qd S d S r   )rH   rI   rJ   r`   rp   ENOTDIRstrerrorrL   rb   rc   islink)	rN   rI   rO   r5   rh   ri   _rf   rk   r   r   r   rK   _  s   zInotify._add_dir_watchc                 C   s8   	 t | j||}|dkrt  || j|< || j|< |S r;   )r   r>   r:   r=   rC   rD   )rN   rI   r5   r4   r   r   r   rL   v  s   	

zInotify._add_watchc                  C   sT   	 t  } | tjkrttjd| tjkrttjd| tjkr"d S t| t| )Nzinotify watch limit reachedzinotify instance limit reached)	ctypes	get_errnorp   ENOSPCr`   EMFILEEACCESrH   r   )errr   r   r   r=     s   


zInotify._raise_errorc                 c   s~    	 d}|d t | kr=td| |\}}}}| |d |d |  d}|d| 7 }||||fV  |d t | ksd S d S )Nr   r   ZiIII    )r7   structunpack_fromrstrip)r   ir4   r5   r6   lengthr8   r   r   r   rr     s   zInotify._parse_event_bufferN)r   r    r!   WATCHDOG_ALL_EVENTSrQ   propertyrP   rI   rU   rV   rW   rY   r[   r\   r^   r_   DEFAULT_EVENT_BUFFER_SIZEr   rK   rL   staticmethodr=   rr   r   r   r   r   r:      s0    




b
r:   c                   @   s2  e 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d+d, Zd-d. Zd/d0 Zed1d2 Zd3d4 Zd5S )6rd   c                 C   s"   || _ || _|| _|| _|| _d S r   )r   _mask_cookie_name	_src_path)rN   r4   r5   r6   r8   rX   r   r   r   rQ     s
   
zInotifyEvent.__init__c                 C      | j S r   )r   rS   r   r   r   rX        zInotifyEvent.src_pathc                 C   r   r   )r   rS   r   r   r   r4     r   zInotifyEvent.wdc                 C   r   r   )r   rS   r   r   r   r5     r   zInotifyEvent.maskc                 C   r   r   )r   rS   r   r   r   r6     r   zInotifyEvent.cookiec                 C   r   r   )r   rS   r   r   r   r8     r   zInotifyEvent.namec                 C      | j tj@ dkS ra   )r   r   r#   rS   r   r   r   	is_modify     zInotifyEvent.is_modifyc                 C   r   ra   )r   r   r%   rS   r   r   r   is_close_write  r   zInotifyEvent.is_close_writec                 C   r   ra   )r   r   r&   rS   r   r   r   is_close_nowrite  r   zInotifyEvent.is_close_nowritec                 C   r   ra   )r   r   r"   rS   r   r   r   	is_access  r   zInotifyEvent.is_accessc                 C   r   ra   )r   r   r*   rS   r   r   r   	is_delete  r   zInotifyEvent.is_deletec                 C   r   ra   )r   r   r+   rS   r   r   r   is_delete_self  r   zInotifyEvent.is_delete_selfc                 C   r   ra   )r   r   r)   rS   r   r   r   r}     r   zInotifyEvent.is_createc                 C   r   ra   )r   r   r'   rS   r   r   r   rs     r   zInotifyEvent.is_moved_fromc                 C   r   ra   )r   r   r(   rS   r   r   r   rt     r   zInotifyEvent.is_moved_toc                 C   r   ra   )r   r   r.   rS   r   r   r   is_move  r   zInotifyEvent.is_movec                 C   r   ra   )r   r   r,   rS   r   r   r   is_move_self  r   zInotifyEvent.is_move_selfc                 C   r   ra   )r   r   r$   rS   r   r   r   	is_attrib	  r   zInotifyEvent.is_attribc                 C   r   ra   )r   r   r/   rS   r   r   r   r{     r   zInotifyEvent.is_ignoredc                 C   s   | j p| jp| jtj@ dkS ra   )r   r   r   r   r1   rS   r   r   r   r|     s   zInotifyEvent.is_directoryc                 C   s   | j | j| j| j| jfS r   )r   r   r   r   r   rS   r   r   r   key  s   zInotifyEvent.keyc                 C   s   | j |j kS r   r   rN   r   r   r   r   __eq__     zInotifyEvent.__eq__c                 C   s   | j |j kS r   r   r   r   r   r   __ne__   r   zInotifyEvent.__ne__c                 C   s
   t | jS r   )hashr   rS   r   r   r   __hash__#  s   
zInotifyEvent.__hash__c                 C   sN   g }t tD ]}|dr|dvrtt|}| |@ r|| qd|}|S )NZIN_)r2   r-   r.   |)dirr   rw   getattrre   rc   )r5   maskscZc_valmask_stringr   r   r   _get_mask_string&  s   


zInotifyEvent._get_mask_stringc              	   C   s8   |  | j}d}|t| j| j| j|| jt| j	f S )Nz5<%s: src_path=%r, wd=%d, mask=%s, cookie=%d, name=%s>)
r   r5   typer   rX   r4   r6   rH   fsdecoder8   )rN   r   sr   r   r   __repr__1  s
   zInotifyEvent.__repr__N)r   r    r!   rQ   r   rX   r4   r5   r6   r8   r   r   r   r   r   r   r}   rs   rt   r   r   r   r{   r|   r   r   r   r   r   r   r   r   r   r   r   rd     sb    





















rd   )(rH   rp   r   r?   r   ctypes.util	functoolsr   r   r   r   watchdog.utilsr   CDLLlibchasattrr   	CFUNCTYPEr   r	   r   r   r#   r$   r'   r(   r)   r*   r+   r0   r%   r   	Structurer3   sizeof
EVENT_SIZEDEFAULT_NUM_EVENTSr   r:   rd   r   r   r   r   <module>   s^   

7
  )