U
    
W[  ã                   @   st   d Z ddlZddlZG dd„ deƒZdd„ Zdd„ Zd	d
„ Zdd„ Zej	 
d¡Zes\e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.
é    Nc                   @   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   c                  C   s   t  ¡ } | dk rtdƒ‚| S )zO
    Create an inotify instance and return the associated file descriptor.
    r   zINotify initialization error.)ÚlibcÚinotify_initr   )Úfdr   r   r   Úinit   s    r   c                 C   s0   t  | | ¡ j|¡}|dk r,td||f ƒ‚|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}.
    @type fd: L{int}

    @param path: The path to watch via inotify.
    @type path: L{twisted.python.filepath.FilePath}

    @param mask: Bitmask specifying the events that inotify should monitor.
    @type mask: L{int}
    r   z"Failed to add watch on '%r' - (%r))r	   Úinotify_add_watchZasBytesModeÚpathr   )r   r   ÚmaskÚwdr   r   r   Úadd#   s    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   Úremove8   s    r   c                 C   sp   dD ]}t | |dƒdk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
   ZargtypesÚctypesZc_intZrestyper   Zc_char_pZc_uint32r   )r	   Zfunctionr   r   r   ÚinitializeModuleT   s    

 ÿ
  ÿr   ÚczCan't find C library.)r   r   Zctypes.utilÚ	Exceptionr   r   r   r   r   ÚutilZfind_libraryÚnamer   ZcdllZLoadLibraryr	   r   r   r   r   Ú<module>   s   