ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-Inotify2/Inotify2.xs
Revision: 1.7
Committed: Sat Jul 7 13:44:51 2018 UTC (5 years, 11 months ago) by root
Branch: MAIN
Changes since 1.6: +29 -24 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 { "IN_MASK_ADD" , IN_MASK_ADD },
41 { "IN_ISDIR" , IN_ISDIR },
42 { "IN_ONESHOT" , IN_ONESHOT },
43 { "IN_ALL_EVENTS" , IN_ALL_EVENTS },
44 };
45
46 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
47 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
48 }
49
50 int
51 inotify_init ()
52
53 void
54 inotify_close (int fd)
55 CODE:
56 close (fd);
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