1 from __future__
import absolute_import
, division
, print_function
, unicode_literals
7 # constant from Linux include/uapi/linux/limits.h
10 # constants from Linux include/uapi/linux/inotify.h
11 IN_MODIFY
= 0x00000002
12 IN_ATTRIB
= 0x00000004
13 IN_CLOSE_WRITE
= 0x00000008
14 IN_MOVED_FROM
= 0x00000040
15 IN_MOVED_TO
= 0x00000080
16 IN_CREATE
= 0x00000100
17 IN_DELETE
= 0x00000200
19 IN_Q_OVERFLOW
= 0x00004000
21 IN_ONLYDIR
= 0x01000000
22 IN_EXCL_UNLINK
= 0x04000000
26 class inotify_event(ctypes
.Structure
):
29 ('mask', ctypes
.c_uint32
),
30 ('cookie', ctypes
.c_uint32
),
31 ('len', ctypes
.c_uint32
),
35 MAX_EVENT_SIZE
= ctypes
.sizeof(inotify_event
) + NAME_MAX
+ 1
38 def _errcheck(result
, func
, arguments
):
41 err
= ctypes
.get_errno()
42 if err
== errno
.EINTR
:
43 return func(*arguments
)
44 raise OSError(err
, os
.strerror(err
))
48 _libc
= ctypes
.CDLL(ctypes
.util
.find_library('c'), use_errno
=True)
50 init
= _libc
.inotify_init
51 add_watch
= _libc
.inotify_add_watch
52 rm_watch
= _libc
.inotify_rm_watch
53 except AttributeError:
54 raise ImportError('Could not load inotify functions from libc')
57 _read
.argtypes
= [ctypes
.c_int
, ctypes
.c_void_p
, ctypes
.c_size_t
]
58 _read
.errcheck
= _errcheck
61 init
.errcheck
= _errcheck
63 add_watch
.argtypes
= [ctypes
.c_int
, ctypes
.c_char_p
, ctypes
.c_uint32
]
64 add_watch
.errcheck
= _errcheck
66 rm_watch
.argtypes
= [ctypes
.c_int
, ctypes
.c_int
]
67 rm_watch
.errcheck
= _errcheck
70 def read_events(inotify_fd
, count
=64):
71 buf
= ctypes
.create_string_buffer(MAX_EVENT_SIZE
* count
)
72 num
= _read(inotify_fd
, buf
, ctypes
.sizeof(buf
))
74 addr
= ctypes
.addressof(buf
)
76 assert num
>= ctypes
.sizeof(inotify_event
)
77 event
= inotify_event
.from_address(addr
)
78 addr
+= ctypes
.sizeof(inotify_event
)
79 num
-= ctypes
.sizeof(inotify_event
)
81 assert num
>= event
.len
82 name
= ctypes
.string_at(addr
)
87 yield event
.wd
, event
.mask
, event
.cookie
, name