fix Makefiles cleanup
[archipelago] / xseg / peers / user / pfiled.c
index 36c9ff7..605f920 100644 (file)
@@ -135,7 +135,7 @@ static void log_io(char *msg, struct io *io)
        data[dend] = 0;
 
        fprintf(stderr,
-               "%s: fd:%u, op:%u offset: %llu size: %lu retval: %lu, reqstate: %u\n"
+               "%s: fd:%u, op:%u offset: %llu size: %lu retval: %lu, reqstate: %u, serviced: %u\n"
                "target[%u]: '%s', data[%llu]:\n%s------------------\n\n",
                msg,
                (unsigned int)io->fdcacheidx, /* this is cacheidx not fd */
@@ -144,6 +144,7 @@ static void log_io(char *msg, struct io *io)
                (unsigned long)io->req->size,
                (unsigned long)io->retval,
                (unsigned int)io->req->state,
+               (unsigned long)io->req->serviced,
                (unsigned int)io->req->targetlen, target,
                (unsigned long long)io->req->datalen, data);
 }
@@ -200,24 +201,17 @@ static int create_path(char *buf, char *path, char *target, uint32_t targetlen,
        int i;
        struct stat st;
        uint32_t pathlen = strlen(path);
-       char *start;
 
        strncpy(buf, path, pathlen);
 
-       start = strchr(target, ':');
-       if (start == NULL)
-               start = target;
-       else 
-               start++;
-
        for (i = 0; i < 9; i+= 3) {
-               buf[pathlen + i] = start[i - (i/3)];
-               buf[pathlen + i +1] = start[i + 1 - (i/3)];
+               buf[pathlen + i] = target[i - (i/3)];
+               buf[pathlen + i +1] = target[i + 1 - (i/3)];
                buf[pathlen + i + 2] = '/';
                if (mkdirs == 1) {
                        buf[pathlen + i + 3] = '\0';
                        if (stat(buf, &st) < 0) 
-                               if (mkdir(buf, 0600) < 0) {
+                               if (mkdir(buf, 0700) < 0) {
                                        perror(buf);
                                        return errno;
                                }
@@ -426,7 +420,7 @@ static void handle_read_write(struct pfiled *pfiled, struct io *io)
                                req->datalen = req->serviced;
                        }
                        else if (r == 0) {
-                               /* reached end of file. zero out the rest data buffer */
+                               fprintf(stderr, "write returned 0\n");
                                memset(data + req->serviced, 0, req->datalen - req->serviced);
                                req->serviced = req->datalen;
                        }
@@ -494,6 +488,7 @@ static void handle_copy(struct pfiled *pfiled, struct io *io)
        char *data = xseg_get_data(pfiled->xseg, req);
        struct xseg_request_copy *xcopy = (struct xseg_request_copy *)data;
        struct stat st;
+       //FIXME is 256 enough?
        char *buf = malloc(256);
        int n, src, dst;
 
@@ -511,7 +506,8 @@ static void handle_copy(struct pfiled *pfiled, struct io *io)
 
        src = open(buf, O_RDWR);
        if (src < 0) {
-               fprintf(stderr, "fail in src\n");
+               XSEGLOG("fail in src %s\n", buf);
+               perror("open src");
                fail(pfiled, io);
                return;
        }
@@ -568,6 +564,74 @@ static void handle_delete(struct pfiled *pfiled, struct io *io)
        return;
 }
 
+static void handle_open(struct pfiled *pfiled, struct io *io)
+{
+       struct xseg_request *req = io->req;
+       char *buf = malloc(MAX_FILENAME_SIZE + strlen("_lock"));
+       char *pathname = malloc(MAX_PATH_SIZE + MAX_FILENAME_SIZE + strlen("_lock"));
+       int fd;
+       char *target = xseg_get_target(pfiled->xseg, req);
+
+       if (!buf || !pathname) {
+               fail(pfiled, io);
+               return;
+       }
+
+       strncpy(buf, target, req->targetlen);
+       strncpy(buf+req->targetlen, "_lock", strlen("_lock"));
+
+       if (create_path(pathname, pfiled->vpath, buf, req->targetlen + strlen("_lock"), 1) < 0) {
+               goto out_fail;
+       }
+
+       fd = open(pathname, O_CREAT | O_EXCL, S_IRWXU | S_IRUSR);
+       if (fd < 0)
+               goto out_fail;
+       
+       close(fd);
+       free(buf);
+       free(pathname);
+       complete(pfiled, io);
+       return;
+
+out_fail:
+       free(buf);
+       free(pathname);
+       fail(pfiled, io);
+       return;
+}
+
+static void handle_close(struct pfiled *pfiled, struct io *io)
+{
+       struct xseg_request *req = io->req;
+       char *buf = malloc(MAX_FILENAME_SIZE + strlen("_lock"));
+       char *pathname = malloc(MAX_PATH_SIZE + MAX_FILENAME_SIZE + strlen("_lock"));
+       char *target = xseg_get_target(pfiled->xseg, req);
+
+       if (!buf || !pathname) {
+               fail(pfiled, io);
+               return;
+       }
+
+       strncpy(buf, target, req->targetlen);
+       strncpy(buf+req->targetlen, "_lock", strlen("_lock"));
+
+       if (create_path(pathname, pfiled->vpath, buf, req->targetlen + strlen("_lock"), 1) < 0) {
+               goto out_fail;
+       }
+       unlink(pathname);
+       free(buf);
+       free(pathname);
+       complete(pfiled, io);
+       return;
+
+out_fail:
+       free(buf);
+       free(pathname);
+       fail(pfiled, io);
+       return;
+}
+
 static void dispatch(struct pfiled *pfiled, struct io *io)
 {
        if (cmdline_verbose) { 
@@ -585,6 +649,10 @@ static void dispatch(struct pfiled *pfiled, struct io *io)
                handle_copy(pfiled, io); break;
        case X_DELETE:
                handle_delete(pfiled, io); break;
+       case X_OPEN:
+               handle_open(pfiled, io); break;
+       case X_CLOSE:
+               handle_close(pfiled, io); break;
 //     case X_SNAPSHOT:
        case X_SYNC:
        default:
@@ -623,7 +691,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(pfiled);
@@ -770,7 +838,7 @@ static int pfiled_init(struct pfiled *pfiled)
                goto out_with_xseginit;
        }
 
-       pfiled->xport = xseg_bind_port(pfiled->xseg, cmdline_portno);
+       pfiled->xport = xseg_bind_port(pfiled->xseg, cmdline_portno, NULL);
        if (!pfiled->xport) {
                ret = -EIO;
                perr(PE, 0, "could not bind to xseg port %ld", cmdline_portno);