Mercurial > hg
annotate hgext/inotify/linux/_inotify.c @ 10263:25e572394f5c stable
Update license to GPLv2+
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Tue, 19 Jan 2010 22:20:08 -0600 |
parents | 5d6659cfaa06 |
children | 08a0f04b56bd |
rev | line source |
---|---|
6239 | 1 /* |
2 * _inotify.c - Python extension interfacing to the Linux inotify subsystem | |
3 * | |
4 * Copyright 2006 Bryan O'Sullivan <bos@serpentine.com> | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of version 2.1 of the GNU Lesser General | |
10263 | 8 * Public License or any later version. |
6239 | 9 */ |
10 | |
11 #include <Python.h> | |
12 #include <alloca.h> | |
13 #include <sys/inotify.h> | |
14 #include <stdint.h> | |
15 #include <sys/ioctl.h> | |
16 #include <unistd.h> | |
17 | |
18 static PyObject *init(PyObject *self, PyObject *args) | |
19 { | |
20 PyObject *ret = NULL; | |
21 int fd = -1; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
22 |
6239 | 23 if (!PyArg_ParseTuple(args, ":init")) |
24 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
25 |
6239 | 26 Py_BEGIN_ALLOW_THREADS |
27 fd = inotify_init(); | |
28 Py_END_ALLOW_THREADS | |
29 | |
30 if (fd == -1) { | |
31 PyErr_SetFromErrno(PyExc_OSError); | |
32 goto bail; | |
33 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
34 |
6239 | 35 ret = PyInt_FromLong(fd); |
36 if (ret == NULL) | |
37 goto bail; | |
38 | |
39 goto done; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
40 |
6239 | 41 bail: |
42 if (fd != -1) | |
43 close(fd); | |
44 | |
45 Py_CLEAR(ret); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
46 |
6239 | 47 done: |
48 return ret; | |
49 } | |
50 | |
51 PyDoc_STRVAR( | |
52 init_doc, | |
53 "init() -> fd\n" | |
54 "\n" | |
55 "Initialise an inotify instance.\n" | |
56 "Return a file descriptor associated with a new inotify event queue."); | |
57 | |
58 static PyObject *add_watch(PyObject *self, PyObject *args) | |
59 { | |
60 PyObject *ret = NULL; | |
61 uint32_t mask; | |
62 int wd = -1; | |
63 char *path; | |
64 int fd; | |
65 | |
66 if (!PyArg_ParseTuple(args, "isI:add_watch", &fd, &path, &mask)) | |
67 goto bail; | |
68 | |
69 Py_BEGIN_ALLOW_THREADS | |
70 wd = inotify_add_watch(fd, path, mask); | |
71 Py_END_ALLOW_THREADS | |
72 | |
73 if (wd == -1) { | |
74 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); | |
75 goto bail; | |
76 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
77 |
6239 | 78 ret = PyInt_FromLong(wd); |
79 if (ret == NULL) | |
80 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
81 |
6239 | 82 goto done; |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
83 |
6239 | 84 bail: |
85 if (wd != -1) | |
86 inotify_rm_watch(fd, wd); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
87 |
6239 | 88 Py_CLEAR(ret); |
89 | |
90 done: | |
91 return ret; | |
92 } | |
93 | |
94 PyDoc_STRVAR( | |
95 add_watch_doc, | |
96 "add_watch(fd, path, mask) -> wd\n" | |
97 "\n" | |
98 "Add a watch to an inotify instance, or modify an existing watch.\n" | |
99 "\n" | |
100 " fd: file descriptor returned by init()\n" | |
101 " path: path to watch\n" | |
102 " mask: mask of events to watch for\n" | |
103 "\n" | |
104 "Return a unique numeric watch descriptor for the inotify instance\n" | |
105 "mapped by the file descriptor."); | |
106 | |
107 static PyObject *remove_watch(PyObject *self, PyObject *args) | |
108 { | |
109 uint32_t wd; | |
110 int fd; | |
111 int r; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
112 |
6239 | 113 if (!PyArg_ParseTuple(args, "iI:remove_watch", &fd, &wd)) |
9428
5d6659cfaa06
inotify: _inotify.c: bugfix: remove_watch has to return PyNone on success
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
6334
diff
changeset
|
114 return NULL; |
6239 | 115 |
116 Py_BEGIN_ALLOW_THREADS | |
117 r = inotify_rm_watch(fd, wd); | |
118 Py_END_ALLOW_THREADS | |
119 | |
120 if (r == -1) { | |
121 PyErr_SetFromErrno(PyExc_OSError); | |
9428
5d6659cfaa06
inotify: _inotify.c: bugfix: remove_watch has to return PyNone on success
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
6334
diff
changeset
|
122 return NULL; |
6239 | 123 } |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
124 |
6239 | 125 Py_INCREF(Py_None); |
9428
5d6659cfaa06
inotify: _inotify.c: bugfix: remove_watch has to return PyNone on success
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
6334
diff
changeset
|
126 return Py_None; |
6239 | 127 } |
128 | |
129 PyDoc_STRVAR( | |
130 remove_watch_doc, | |
131 "remove_watch(fd, wd)\n" | |
132 "\n" | |
133 " fd: file descriptor returned by init()\n" | |
134 " wd: watch descriptor returned by add_watch()\n" | |
135 "\n" | |
136 "Remove a watch associated with the watch descriptor wd from the\n" | |
137 "inotify instance associated with the file descriptor fd.\n" | |
138 "\n" | |
139 "Removing a watch causes an IN_IGNORED event to be generated for this\n" | |
140 "watch descriptor."); | |
141 | |
142 #define bit_name(x) {x, #x} | |
143 | |
144 static struct { | |
145 int bit; | |
146 const char *name; | |
147 PyObject *pyname; | |
148 } bit_names[] = { | |
149 bit_name(IN_ACCESS), | |
150 bit_name(IN_MODIFY), | |
151 bit_name(IN_ATTRIB), | |
152 bit_name(IN_CLOSE_WRITE), | |
153 bit_name(IN_CLOSE_NOWRITE), | |
154 bit_name(IN_OPEN), | |
155 bit_name(IN_MOVED_FROM), | |
156 bit_name(IN_MOVED_TO), | |
157 bit_name(IN_CREATE), | |
158 bit_name(IN_DELETE), | |
159 bit_name(IN_DELETE_SELF), | |
160 bit_name(IN_MOVE_SELF), | |
161 bit_name(IN_UNMOUNT), | |
162 bit_name(IN_Q_OVERFLOW), | |
163 bit_name(IN_IGNORED), | |
164 bit_name(IN_ONLYDIR), | |
165 bit_name(IN_DONT_FOLLOW), | |
166 bit_name(IN_MASK_ADD), | |
167 bit_name(IN_ISDIR), | |
168 bit_name(IN_ONESHOT), | |
169 {0} | |
170 }; | |
171 | |
172 static PyObject *decode_mask(int mask) | |
173 { | |
174 PyObject *ret = PyList_New(0); | |
175 int i; | |
176 | |
177 if (ret == NULL) | |
178 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
179 |
6239 | 180 for (i = 0; bit_names[i].bit; i++) { |
181 if (mask & bit_names[i].bit) { | |
182 if (bit_names[i].pyname == NULL) { | |
183 bit_names[i].pyname = PyString_FromString(bit_names[i].name); | |
184 if (bit_names[i].pyname == NULL) | |
185 goto bail; | |
186 } | |
187 Py_INCREF(bit_names[i].pyname); | |
188 if (PyList_Append(ret, bit_names[i].pyname) == -1) | |
189 goto bail; | |
190 } | |
191 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
192 |
6239 | 193 goto done; |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
194 |
6239 | 195 bail: |
196 Py_CLEAR(ret); | |
197 | |
198 done: | |
199 return ret; | |
200 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
201 |
6239 | 202 static PyObject *pydecode_mask(PyObject *self, PyObject *args) |
203 { | |
204 int mask; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
205 |
6239 | 206 if (!PyArg_ParseTuple(args, "i:decode_mask", &mask)) |
207 return NULL; | |
208 | |
209 return decode_mask(mask); | |
210 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
211 |
6239 | 212 PyDoc_STRVAR( |
213 decode_mask_doc, | |
214 "decode_mask(mask) -> list_of_strings\n" | |
215 "\n" | |
216 "Decode an inotify mask value into a list of strings that give the\n" | |
217 "name of each bit set in the mask."); | |
218 | |
219 static char doc[] = "Low-level inotify interface wrappers."; | |
220 | |
221 static void define_const(PyObject *dict, const char *name, uint32_t val) | |
222 { | |
223 PyObject *pyval = PyInt_FromLong(val); | |
224 PyObject *pyname = PyString_FromString(name); | |
225 | |
226 if (!pyname || !pyval) | |
227 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
228 |
6239 | 229 PyDict_SetItem(dict, pyname, pyval); |
230 | |
231 bail: | |
232 Py_XDECREF(pyname); | |
233 Py_XDECREF(pyval); | |
234 } | |
235 | |
236 static void define_consts(PyObject *dict) | |
237 { | |
238 define_const(dict, "IN_ACCESS", IN_ACCESS); | |
239 define_const(dict, "IN_MODIFY", IN_MODIFY); | |
240 define_const(dict, "IN_ATTRIB", IN_ATTRIB); | |
241 define_const(dict, "IN_CLOSE_WRITE", IN_CLOSE_WRITE); | |
242 define_const(dict, "IN_CLOSE_NOWRITE", IN_CLOSE_NOWRITE); | |
243 define_const(dict, "IN_OPEN", IN_OPEN); | |
244 define_const(dict, "IN_MOVED_FROM", IN_MOVED_FROM); | |
245 define_const(dict, "IN_MOVED_TO", IN_MOVED_TO); | |
246 | |
247 define_const(dict, "IN_CLOSE", IN_CLOSE); | |
248 define_const(dict, "IN_MOVE", IN_MOVE); | |
249 | |
250 define_const(dict, "IN_CREATE", IN_CREATE); | |
251 define_const(dict, "IN_DELETE", IN_DELETE); | |
252 define_const(dict, "IN_DELETE_SELF", IN_DELETE_SELF); | |
253 define_const(dict, "IN_MOVE_SELF", IN_MOVE_SELF); | |
254 define_const(dict, "IN_UNMOUNT", IN_UNMOUNT); | |
255 define_const(dict, "IN_Q_OVERFLOW", IN_Q_OVERFLOW); | |
256 define_const(dict, "IN_IGNORED", IN_IGNORED); | |
257 | |
258 define_const(dict, "IN_ONLYDIR", IN_ONLYDIR); | |
259 define_const(dict, "IN_DONT_FOLLOW", IN_DONT_FOLLOW); | |
260 define_const(dict, "IN_MASK_ADD", IN_MASK_ADD); | |
261 define_const(dict, "IN_ISDIR", IN_ISDIR); | |
262 define_const(dict, "IN_ONESHOT", IN_ONESHOT); | |
263 define_const(dict, "IN_ALL_EVENTS", IN_ALL_EVENTS); | |
264 } | |
265 | |
266 struct event { | |
267 PyObject_HEAD | |
268 PyObject *wd; | |
269 PyObject *mask; | |
270 PyObject *cookie; | |
271 PyObject *name; | |
272 }; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
273 |
6239 | 274 static PyObject *event_wd(PyObject *self, void *x) |
275 { | |
276 struct event *evt = (struct event *) self; | |
277 Py_INCREF(evt->wd); | |
278 return evt->wd; | |
279 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
280 |
6239 | 281 static PyObject *event_mask(PyObject *self, void *x) |
282 { | |
283 struct event *evt = (struct event *) self; | |
284 Py_INCREF(evt->mask); | |
285 return evt->mask; | |
286 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
287 |
6239 | 288 static PyObject *event_cookie(PyObject *self, void *x) |
289 { | |
290 struct event *evt = (struct event *) self; | |
291 Py_INCREF(evt->cookie); | |
292 return evt->cookie; | |
293 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
294 |
6239 | 295 static PyObject *event_name(PyObject *self, void *x) |
296 { | |
297 struct event *evt = (struct event *) self; | |
298 Py_INCREF(evt->name); | |
299 return evt->name; | |
300 } | |
301 | |
302 static struct PyGetSetDef event_getsets[] = { | |
303 {"wd", event_wd, NULL, | |
304 "watch descriptor"}, | |
305 {"mask", event_mask, NULL, | |
306 "event mask"}, | |
307 {"cookie", event_cookie, NULL, | |
308 "rename cookie, if rename-related event"}, | |
309 {"name", event_name, NULL, | |
310 "file name"}, | |
311 {NULL} | |
312 }; | |
313 | |
314 PyDoc_STRVAR( | |
315 event_doc, | |
316 "event: Structure describing an inotify event."); | |
317 | |
318 static PyObject *event_new(PyTypeObject *t, PyObject *a, PyObject *k) | |
319 { | |
320 return (*t->tp_alloc)(t, 0); | |
321 } | |
322 | |
323 static void event_dealloc(struct event *evt) | |
324 { | |
325 Py_XDECREF(evt->wd); | |
326 Py_XDECREF(evt->mask); | |
327 Py_XDECREF(evt->cookie); | |
328 Py_XDECREF(evt->name); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
329 |
6239 | 330 (*evt->ob_type->tp_free)(evt); |
331 } | |
332 | |
333 static PyObject *event_repr(struct event *evt) | |
334 { | |
335 int wd = PyInt_AsLong(evt->wd); | |
336 int cookie = evt->cookie == Py_None ? -1 : PyInt_AsLong(evt->cookie); | |
337 PyObject *ret = NULL, *pymasks = NULL, *pymask = NULL; | |
338 PyObject *join = NULL; | |
339 char *maskstr; | |
340 | |
341 join = PyString_FromString("|"); | |
342 if (join == NULL) | |
343 goto bail; | |
344 | |
345 pymasks = decode_mask(PyInt_AsLong(evt->mask)); | |
346 if (pymasks == NULL) | |
347 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
348 |
6239 | 349 pymask = _PyString_Join(join, pymasks); |
350 if (pymask == NULL) | |
351 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
352 |
6239 | 353 maskstr = PyString_AsString(pymask); |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
354 |
6239 | 355 if (evt->name != Py_None) { |
356 PyObject *pyname = PyString_Repr(evt->name, 1); | |
357 char *name = pyname ? PyString_AsString(pyname) : "???"; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
358 |
6239 | 359 if (cookie == -1) |
360 ret = PyString_FromFormat("event(wd=%d, mask=%s, name=%s)", | |
361 wd, maskstr, name); | |
362 else | |
363 ret = PyString_FromFormat("event(wd=%d, mask=%s, " | |
364 "cookie=0x%x, name=%s)", | |
365 wd, maskstr, cookie, name); | |
366 | |
367 Py_XDECREF(pyname); | |
368 } else { | |
369 if (cookie == -1) | |
370 ret = PyString_FromFormat("event(wd=%d, mask=%s)", | |
371 wd, maskstr); | |
372 else { | |
373 ret = PyString_FromFormat("event(wd=%d, mask=%s, cookie=0x%x)", | |
374 wd, maskstr, cookie); | |
375 } | |
376 } | |
377 | |
378 goto done; | |
379 bail: | |
380 Py_CLEAR(ret); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
381 |
6239 | 382 done: |
383 Py_XDECREF(pymask); | |
384 Py_XDECREF(pymasks); | |
385 Py_XDECREF(join); | |
386 | |
387 return ret; | |
388 } | |
389 | |
390 static PyTypeObject event_type = { | |
391 PyObject_HEAD_INIT(NULL) | |
392 0, /*ob_size*/ | |
393 "_inotify.event", /*tp_name*/ | |
394 sizeof(struct event), /*tp_basicsize*/ | |
395 0, /*tp_itemsize*/ | |
396 (destructor)event_dealloc, /*tp_dealloc*/ | |
397 0, /*tp_print*/ | |
398 0, /*tp_getattr*/ | |
399 0, /*tp_setattr*/ | |
400 0, /*tp_compare*/ | |
401 (reprfunc)event_repr, /*tp_repr*/ | |
402 0, /*tp_as_number*/ | |
403 0, /*tp_as_sequence*/ | |
404 0, /*tp_as_mapping*/ | |
405 0, /*tp_hash */ | |
406 0, /*tp_call*/ | |
407 0, /*tp_str*/ | |
408 0, /*tp_getattro*/ | |
409 0, /*tp_setattro*/ | |
410 0, /*tp_as_buffer*/ | |
411 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
412 event_doc, /* tp_doc */ | |
413 0, /* tp_traverse */ | |
414 0, /* tp_clear */ | |
415 0, /* tp_richcompare */ | |
416 0, /* tp_weaklistoffset */ | |
417 0, /* tp_iter */ | |
418 0, /* tp_iternext */ | |
419 0, /* tp_methods */ | |
420 0, /* tp_members */ | |
421 event_getsets, /* tp_getset */ | |
422 0, /* tp_base */ | |
423 0, /* tp_dict */ | |
424 0, /* tp_descr_get */ | |
425 0, /* tp_descr_set */ | |
426 0, /* tp_dictoffset */ | |
427 0, /* tp_init */ | |
428 0, /* tp_alloc */ | |
429 event_new, /* tp_new */ | |
430 }; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
431 |
6239 | 432 PyObject *read_events(PyObject *self, PyObject *args) |
433 { | |
434 PyObject *ctor_args = NULL; | |
435 PyObject *pybufsize = NULL; | |
436 PyObject *ret = NULL; | |
437 int bufsize = 65536; | |
438 char *buf = NULL; | |
439 int nread, pos; | |
440 int fd; | |
441 | |
442 if (!PyArg_ParseTuple(args, "i|O:read", &fd, &pybufsize)) | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
443 goto bail; |
6239 | 444 |
445 if (pybufsize && pybufsize != Py_None) | |
446 bufsize = PyInt_AsLong(pybufsize); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
447 |
6239 | 448 ret = PyList_New(0); |
449 if (ret == NULL) | |
450 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
451 |
6239 | 452 if (bufsize <= 0) { |
453 int r; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
454 |
6239 | 455 Py_BEGIN_ALLOW_THREADS |
456 r = ioctl(fd, FIONREAD, &bufsize); | |
457 Py_END_ALLOW_THREADS | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
458 |
6239 | 459 if (r == -1) { |
460 PyErr_SetFromErrno(PyExc_OSError); | |
461 goto bail; | |
462 } | |
463 if (bufsize == 0) | |
464 goto done; | |
465 } | |
466 else { | |
467 static long name_max; | |
468 static long name_fd = -1; | |
469 long min; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
470 |
6239 | 471 if (name_fd != fd) { |
472 name_fd = fd; | |
473 Py_BEGIN_ALLOW_THREADS | |
474 name_max = fpathconf(fd, _PC_NAME_MAX); | |
475 Py_END_ALLOW_THREADS | |
476 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
477 |
6239 | 478 min = sizeof(struct inotify_event) + name_max + 1; |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
479 |
6239 | 480 if (bufsize < min) { |
481 PyErr_Format(PyExc_ValueError, "bufsize must be at least %d", | |
482 (int) min); | |
483 goto bail; | |
484 } | |
485 } | |
486 | |
487 buf = alloca(bufsize); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
488 |
6239 | 489 Py_BEGIN_ALLOW_THREADS |
490 nread = read(fd, buf, bufsize); | |
491 Py_END_ALLOW_THREADS | |
492 | |
493 if (nread == -1) { | |
494 PyErr_SetFromErrno(PyExc_OSError); | |
495 goto bail; | |
496 } | |
497 | |
498 ctor_args = PyTuple_New(0); | |
499 | |
500 if (ctor_args == NULL) | |
501 goto bail; | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
502 |
6239 | 503 pos = 0; |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
504 |
6239 | 505 while (pos < nread) { |
506 struct inotify_event *in = (struct inotify_event *) (buf + pos); | |
507 struct event *evt; | |
508 PyObject *obj; | |
509 | |
510 obj = PyObject_CallObject((PyObject *) &event_type, ctor_args); | |
511 | |
512 if (obj == NULL) | |
513 goto bail; | |
514 | |
515 evt = (struct event *) obj; | |
516 | |
517 evt->wd = PyInt_FromLong(in->wd); | |
518 evt->mask = PyInt_FromLong(in->mask); | |
519 if (in->mask & IN_MOVE) | |
520 evt->cookie = PyInt_FromLong(in->cookie); | |
521 else { | |
522 Py_INCREF(Py_None); | |
523 evt->cookie = Py_None; | |
524 } | |
525 if (in->len) | |
526 evt->name = PyString_FromString(in->name); | |
527 else { | |
528 Py_INCREF(Py_None); | |
529 evt->name = Py_None; | |
530 } | |
531 | |
532 if (!evt->wd || !evt->mask || !evt->cookie || !evt->name) | |
533 goto mybail; | |
534 | |
535 if (PyList_Append(ret, obj) == -1) | |
536 goto mybail; | |
537 | |
538 pos += sizeof(struct inotify_event) + in->len; | |
539 continue; | |
540 | |
541 mybail: | |
542 Py_CLEAR(evt->wd); | |
543 Py_CLEAR(evt->mask); | |
544 Py_CLEAR(evt->cookie); | |
545 Py_CLEAR(evt->name); | |
546 Py_DECREF(obj); | |
547 | |
548 goto bail; | |
549 } | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
550 |
6239 | 551 goto done; |
552 | |
553 bail: | |
554 Py_CLEAR(ret); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
555 |
6239 | 556 done: |
557 Py_XDECREF(ctor_args); | |
558 | |
559 return ret; | |
560 } | |
561 | |
562 PyDoc_STRVAR( | |
563 read_doc, | |
564 "read(fd, bufsize[=65536]) -> list_of_events\n" | |
565 "\n" | |
566 "\nRead inotify events from a file descriptor.\n" | |
567 "\n" | |
568 " fd: file descriptor returned by init()\n" | |
569 " bufsize: size of buffer to read into, in bytes\n" | |
570 "\n" | |
571 "Return a list of event objects.\n" | |
572 "\n" | |
573 "If bufsize is > 0, block until events are available to be read.\n" | |
574 "Otherwise, immediately return all events that can be read without\n" | |
575 "blocking."); | |
576 | |
577 | |
578 static PyMethodDef methods[] = { | |
579 {"init", init, METH_VARARGS, init_doc}, | |
580 {"add_watch", add_watch, METH_VARARGS, add_watch_doc}, | |
581 {"remove_watch", remove_watch, METH_VARARGS, remove_watch_doc}, | |
582 {"read", read_events, METH_VARARGS, read_doc}, | |
583 {"decode_mask", pydecode_mask, METH_VARARGS, decode_mask_doc}, | |
584 {NULL}, | |
585 }; | |
586 | |
587 void init_inotify(void) | |
588 { | |
589 PyObject *mod, *dict; | |
590 | |
591 if (PyType_Ready(&event_type) == -1) | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
592 return; |
6239 | 593 |
594 mod = Py_InitModule3("_inotify", methods, doc); | |
595 | |
596 dict = PyModule_GetDict(mod); | |
6334
7016f7fb8fe3
tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
597 |
6239 | 598 if (dict) |
599 define_consts(dict); | |
600 } |