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