o
    ¯b¨  ã                   @   s¾   d Z ddlZddlZddlmZ ddlmZ G dd„ deƒZde	fdd	„Z
d
e	dede	de	fdd„Zd
e	de	ddfdd„Zdejddfdd„Zej d¡ZesSedƒ‚ej e¡Zeeƒ dS )zŒ
Very low-level ctypes-based interface to Linux inotify(7).

ctypes and a version of libc which supports inotify system calls are
required.
é    N)Úcast)ÚFilePathc                   @   s   e Zd ZdZdS )ÚINotifyErrorzR
    Unify all the possible exceptions that can be raised by the INotify API.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r	   r	   ú9/usr/lib/python3/dist-packages/twisted/python/_inotify.pyr      s    r   Úreturnc                  C   s"   t tt ¡ ƒ} | dk rtdƒ‚| S )zO
    Create an inotify instance and return the associated file descriptor.
    r   zINotify initialization error.)r   ÚintÚlibcÚinotify_initr   )Úfdr	   r	   r
   Úinit   s   r   r   ÚpathÚmaskc                 C   s<   t tt | | ¡ j|¡ƒ}|dk rtd|›d|›dƒ‚|S )a/  
    Add a watch for the given path to the inotify file descriptor, and return
    the watch descriptor.

    @param fd: The file descriptor returned by C{libc.inotify_init}.
    @param path: The path to watch via inotify.
    @param mask: Bitmask specifying the events that inotify should monitor.
    r   zFailed to add watch on 'z' - (ú))r   r   r   Úinotify_add_watchÚasBytesModer   r   )r   r   r   Úwdr	   r	   r
   Úadd#   s   	r   r   c                 C   s   t  | |¡ dS )zM
    Remove the given watch descriptor from the inotify file descriptor.
    N)r   Úinotify_rm_watch)r   r   r	   r	   r
   Úremove2   s   r   r   c                 C   sp   dD ]}t | |dƒdu rtdƒ‚qg | j_tj| j_tjtjg| j_tj| j_tjtjtj	g| j
_tj| j
_dS )zµ
    Initialize the module, checking if the expected APIs exist and setting the
    argtypes and restype for C{inotify_init}, C{inotify_add_watch}, and
    C{inotify_rm_watch}.
    )r   r   r   Nzlibc6 2.4 or higher needed)ÚgetattrÚImportErrorr   ÚargtypesÚctypesÚc_intÚrestyper   Úc_char_pÚc_uint32r   )r   Úfunctionr	   r	   r
   ÚinitializeModuleM   s   ÿ

r#   ÚczCan't find C library.)r   r   Úctypes.utilÚtypingr   Útwisted.python.filepathr   Ú	Exceptionr   r   r   r   r   ÚCDLLr#   ÚutilÚfind_libraryÚnamer   ÚcdllÚLoadLibraryr   r	   r	   r	   r
   Ú<module>   s   
