fix bug in mt-mapperd
[archipelago] / xseg / peers / user / filed.c
index 0be531b..b9666d4 100644 (file)
@@ -432,6 +432,7 @@ static void handle_copy(struct store *store, struct io *io)
         struct xseg_request_copy *xcopy = (struct xseg_request_copy *) xseg_get_data(store->xseg, req);
         struct stat st;
         int n, src, dst;
+       char buf[XSEG_MAX_TARGETLEN+1];
        char *target = xseg_get_target(store->xseg, req);
 
         dst = dir_open(store, io, target, req->targetlen, 1);
@@ -441,11 +442,23 @@ static void handle_copy(struct store *store, struct io *io)
                 return;
         }
 
-       src = openat(store->dirfd, xcopy->target, O_RDWR);      
+       strncpy(buf, xcopy->target, xcopy->targetlen);
+       buf[xcopy->targetlen] = 0;
+       src = openat(store->dirfd, buf, O_RDWR);        
         if (src < 0) {
-                fprintf(stderr, "fail in src\n");
-                fail(store, io);
-                return;
+               if (errno == ENOENT){
+                       src = openat(store->dirfd, buf, 
+                                       O_RDWR | O_CREAT, 0600);
+                       if (src < 0 ) {
+                               fprintf(stderr, "fail in src\n");
+                               fail(store, io);
+                               return;
+                       }       
+               } else {
+                       fprintf(stderr, "fail in src\n");
+                       fail(store, io);
+                       return;
+               }
         }
 
         fstat(src, &st);
@@ -468,6 +481,34 @@ out:
         close(src);
 }
 
+static void handle_delete(struct store *store, struct io *io)
+{
+       struct xseg_request *req = io->req;
+       int fd;
+       char *target = xseg_get_target(store->xseg, req);
+       
+       fd = dir_open(store, io, target, req->targetlen, 0);
+       if (fd < 0) {
+               fprintf(stderr, "fail in dir_open\n");
+               fail(store, io);
+               return;
+       }
+
+       /* 'invalidate' cache entry */
+       if (io->fdcacheidx >= 0) {
+               store->fdcache[io->fdcacheidx].fd = -1;
+       }
+
+       close(fd);
+       char buf[MAX_FILENAME_SIZE + 1];
+       strncpy(buf, target, req->targetlen);
+       buf[req->targetlen] = 0;
+       unlinkat(store->dirfd, buf, 0);
+
+       complete(store, io);
+
+       return;
+}
 
 static void dispatch(struct store *store, struct io *io)
 {
@@ -480,6 +521,8 @@ static void dispatch(struct store *store, struct io *io)
                handle_read_write(store, io); break;
        case X_INFO:
                handle_info(store, io); break;
+       case X_DELETE:
+               handle_delete(store, io); break;
        case X_COPY:
                handle_copy(store, io); break;
        case X_SYNC:
@@ -519,7 +562,7 @@ void *io_loop(void *arg)
 
        for (;;) {
                accepted = NULL;
-               accepted = xseg_accept(xseg, portno);
+               accepted = xseg_accept(xseg, portno, 0);
                if (accepted) {
                        io->req = accepted;
                        wake_up_next_iothread(store);
@@ -663,7 +706,7 @@ malloc_fail:
        if (!store->xseg)
                return -1;
 
-       store->xport = xseg_bind_port(store->xseg, portno);
+       store->xport = xseg_bind_port(store->xseg, portno, NULL);
        if (!store->xport) {
                printf("cannot bind to port %ld\n", portno);
                return -1;