1 |
#define PERL_NO_GET_CONTEXT |
2 |
|
3 |
#include "EXTERN.h" |
4 |
#include "perl.h" |
5 |
#include "XSUB.h" |
6 |
|
7 |
#include <unistd.h> |
8 |
#include <fcntl.h> |
9 |
|
10 |
#include <sys/inotify.h> |
11 |
|
12 |
#ifndef IN_EXCL_UNLINK |
13 |
#define IN_EXCL_UNLINK 0 |
14 |
#endif |
15 |
|
16 |
MODULE = Linux::Inotify2 PACKAGE = Linux::Inotify2 |
17 |
|
18 |
PROTOTYPES: ENABLE |
19 |
|
20 |
BOOT: |
21 |
{ |
22 |
HV *stash = GvSTASH (CvGV (cv)); |
23 |
|
24 |
static const struct civ { const char *name; IV iv; } *civ, const_iv[] = { |
25 |
{ "IN_ACCESS" , IN_ACCESS }, |
26 |
{ "IN_MODIFY" , IN_MODIFY }, |
27 |
{ "IN_ATTRIB" , IN_ATTRIB }, |
28 |
{ "IN_CLOSE_WRITE" , IN_CLOSE_WRITE }, |
29 |
{ "IN_CLOSE_NOWRITE", IN_CLOSE_NOWRITE }, |
30 |
{ "IN_OPEN" , IN_OPEN }, |
31 |
{ "IN_MOVED_FROM" , IN_MOVED_FROM }, |
32 |
{ "IN_MOVED_TO" , IN_MOVED_TO }, |
33 |
{ "IN_CREATE" , IN_CREATE }, |
34 |
{ "IN_DELETE" , IN_DELETE }, |
35 |
{ "IN_DELETE_SELF" , IN_DELETE_SELF }, |
36 |
{ "IN_MOVE_SELF" , IN_MOVE_SELF }, |
37 |
{ "IN_UNMOUNT" , IN_UNMOUNT }, |
38 |
{ "IN_Q_OVERFLOW" , IN_Q_OVERFLOW }, |
39 |
{ "IN_IGNORED" , IN_IGNORED }, |
40 |
{ "IN_CLOSE" , IN_CLOSE }, |
41 |
{ "IN_MOVE" , IN_MOVE }, |
42 |
{ "IN_ONLYDIR" , IN_ONLYDIR }, |
43 |
{ "IN_DONT_FOLLOW" , IN_DONT_FOLLOW }, |
44 |
{ "IN_EXCL_UNLINK" , IN_EXCL_UNLINK }, |
45 |
{ "IN_MASK_ADD" , IN_MASK_ADD }, |
46 |
{ "IN_ISDIR" , IN_ISDIR }, |
47 |
{ "IN_ONESHOT" , IN_ONESHOT }, |
48 |
{ "IN_ALL_EVENTS" , IN_ALL_EVENTS }, |
49 |
}; |
50 |
|
51 |
for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
52 |
newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
53 |
} |
54 |
|
55 |
int |
56 |
inotify_init () |
57 |
|
58 |
int |
59 |
inotify_add_watch (int fd, char *name, U32 mask) |
60 |
|
61 |
int |
62 |
inotify_rm_watch (int fd, U32 wd) |
63 |
|
64 |
int |
65 |
inotify_blocking (int fd, I32 blocking) |
66 |
CODE: |
67 |
fcntl (fd, F_SETFL, blocking ? 0 : O_NONBLOCK); |
68 |
|
69 |
void |
70 |
inotify_read (int fd, int size = 8192) |
71 |
PPCODE: |
72 |
{ |
73 |
char buf [size], *cur, *end; |
74 |
int got = read (fd, buf, size); |
75 |
|
76 |
if (got < 0) |
77 |
if (errno != EAGAIN && errno != EINTR) |
78 |
croak ("Linux::Inotify2: read error while reading events"); |
79 |
else |
80 |
XSRETURN_EMPTY; |
81 |
|
82 |
cur = buf; |
83 |
end = buf + got; |
84 |
|
85 |
while (cur < end) |
86 |
{ |
87 |
struct inotify_event *ev = (struct inotify_event *)cur; |
88 |
cur += sizeof (struct inotify_event) + ev->len; |
89 |
|
90 |
while (ev->len > 0 && !ev->name [ev->len - 1]) |
91 |
--ev->len; |
92 |
|
93 |
HV *hv = newHV (); |
94 |
hv_store (hv, "wd", sizeof ("wd") - 1, newSViv (ev->wd), 0); |
95 |
hv_store (hv, "mask", sizeof ("mask") - 1, newSViv (ev->mask), 0); |
96 |
hv_store (hv, "cookie", sizeof ("cookie") - 1, newSViv (ev->cookie), 0); |
97 |
hv_store (hv, "name", sizeof ("name") - 1, newSVpvn (ev->name, ev->len), 0); |
98 |
|
99 |
XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
100 |
} |
101 |
} |
102 |
|