Removing mac addies is now supported
[handlervirt.git] / create.c
blobdef5eee0cbb2e3e61d1c639ff912ecc32f334c32
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
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[]) {
12 if (argc != 3) {
13 fprintf(stderr, "%s <watchpath> <destpath>\n", argv[0]);
14 exit(-1);
17 int fd = inotify_init();
19 if (fd < 0)
20 perror ("inotify_init");
22 int wd = inotify_add_watch(fd, argv[1], IN_CREATE);
24 if (wd < 0)
25 perror ("inotify_add_watch");
27 char buf[BUF_LEN];
28 fd_set rfds;
29 int ret;
31 /* zero-out the fd_set */
32 FD_ZERO (&rfds);
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
39 FD_SET (fd, &rfds);
41 while(1) {
42 ret = select (fd+1, &rfds, NULL, NULL, NULL);
44 if (FD_ISSET (fd, &rfds)) {
45 int len, i = 0;
47 len = read (fd, buf, BUF_LEN);
49 while (i < len) {
50 struct inotify_event *event;
51 event = (struct inotify_event *) &buf[i];
53 if (event->len) {
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];
58 int namelen;
60 stripfile[0] = '\0';
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)
67 perror("readlink");
68 else {
69 char cmd[1024];
70 fprintf (stderr, "DELETE: name=%s\n", file);
71 unlink(file);
73 len = strlen(file);
75 strncpy(&file[len-7], ".busy\0", 6);
77 hardlink[namelen] = '\0';
79 snprintf(cmd, 1023, "cp %s %s", hardlink, file);
80 cmd[1023] = '\0';
82 system(cmd);
83 fprintf (stderr, "CP: source=%s name=%s\n", hardlink, file);
85 snprintf(cmd, 1023, "%s/%s", argv[2], stripfile);
86 cmd[1023] = '\0';
88 fprintf (stderr, "MV: source=%s name=%s\n", file, cmd);
90 rename(file, cmd);
95 i += EVENT_SIZE + event->len;