File Coverage

Inotify2.xs
Criterion Covered Total %
statement 22 24 91.6
branch 12 20 60.0
condition n/a
subroutine n/a
pod n/a
total 34 44 77.2


line stmt bran cond sub pod time code
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 2           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 50 100         for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
52 48           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 1 50         fcntl (fd, F_SETFL, blocking ? 0 : O_NONBLOCK);
68              
69             void
70             inotify_read (int fd, int size = 8192)
71             PPCODE:
72 2           {
73 2           char buf [size], *cur, *end;
74 2           int got = read (fd, buf, size);
75              
76 2 100         if (got < 0)
77 1 50         if (errno != EAGAIN && errno != EINTR)
    0          
78 0           croak ("Linux::Inotify2: read error while reading events");
79             else
80 1           XSRETURN_EMPTY;
81              
82 1           cur = buf;
83 1           end = buf + got;
84              
85 4 100         while (cur < end)
    100          
86             {
87 2           struct inotify_event *ev = (struct inotify_event *)cur;
88 2           cur += sizeof (struct inotify_event) + ev->len;
89              
90 2 50         while (ev->len > 0 && !ev->name [ev->len - 1])
    0          
91 0           --ev->len;
92            
93 2           HV *hv = newHV ();
94 2           hv_store (hv, "wd", sizeof ("wd") - 1, newSViv (ev->wd), 0);
95 2           hv_store (hv, "mask", sizeof ("mask") - 1, newSViv (ev->mask), 0);
96 2           hv_store (hv, "cookie", sizeof ("cookie") - 1, newSViv (ev->cookie), 0);
97 2           hv_store (hv, "name", sizeof ("name") - 1, newSVpvn (ev->name, ev->len), 0);
98              
99 2 50         XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
100             }
101             }
102