Update so after migration we will still try to publish the domain.
[handlervirt.git] / create.c
blob583442c0f4ceeea9b991080aa36d37179d314b7b
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);
63 stripfile[len-7] = '\0';
65 snprintf(file, 1024, "%s/%s", argv[1], event->name);
67 if ((namelen = readlink(file, hardlink, 1024)) < 0)
68 perror("readlink");
69 else {
70 char cmd[1024];
71 char *filetodelete = strdup(file);
72 len = strlen(file);
74 strncpy(&file[len-7], ".busy\0", 6);
76 hardlink[namelen] = '\0';
78 snprintf(cmd, 1023, "cp %s %s", hardlink, file);
79 cmd[1023] = '\0';
81 system(cmd);
82 fprintf (stderr, "CP: source=%s name=%s\n", hardlink, file);
84 fprintf (stderr, "DELETE: name=%s\n", filetodelete);
85 unlink(filetodelete);
86 free(filetodelete);
88 snprintf(cmd, 1023, "%s/%s", argv[2], stripfile);
89 cmd[1023] = '\0';
91 fprintf (stderr, "MV: source=%s name=%s\n", file, cmd);
93 rename(file, cmd);
98 i += EVENT_SIZE + event->len;