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