5 #include <linux/inotify.h>
6 #include <linux/types.h>
8 #define EVENT_SIZE (sizeof (struct inotify_event))
9 #define BUF_LEN (1024 * (EVENT_SIZE + 16))
11 int main(int argc
, char *argv
[]) {
13 fprintf(stderr
, "%s <watchpath> <destpath>\n", argv
[0]);
17 int fd
= inotify_init();
20 perror ("inotify_init");
22 int wd
= inotify_add_watch(fd
, argv
[1], IN_CREATE
);
25 perror ("inotify_add_watch");
31 /* zero-out the fd_set */
35 * add the inotify fd to the fd_set -- of course,
36 * your application will probably want to add
37 * other file descriptors here, too
42 ret
= select (fd
+1, &rfds
, NULL
, NULL
, NULL
);
44 if (FD_ISSET (fd
, &rfds
)) {
47 len
= read (fd
, buf
, BUF_LEN
);
50 struct inotify_event
*event
;
51 event
= (struct inotify_event
*) &buf
[i
];
54 int len
= strlen(event
->name
);
56 if (len
> 7 && strncmp(&event
->name
[len
-7], ".queued", 7) == 0) {
57 char stripfile
[512], file
[1024], hardlink
[1024];
62 strncpy(stripfile
, event
->name
, len
-7);
64 snprintf(file
, 1024, "%s/%s", argv
[1], event
->name
);
66 if ((namelen
= readlink(file
, hardlink
, 1024)) < 0)
70 fprintf (stderr
, "DELETE: name=%s\n", file
);
75 strncpy(&file
[len
-7], ".busy\0", 6);
77 hardlink
[namelen
] = '\0';
79 snprintf(cmd
, 1023, "cp %s %s", hardlink
, file
);
83 fprintf (stderr
, "CP: source=%s name=%s\n", hardlink
, file
);
85 snprintf(cmd
, 1023, "%s/%s", argv
[2], stripfile
);
88 fprintf (stderr
, "MV: source=%s name=%s\n", file
, cmd
);
95 i
+= EVENT_SIZE
+ event
->len
;