nfs_creat_async: plug potential memleak
[deb_libnfs.git] / lib / libnfs.c
index b19a373bc2ba05d557342ecbf3887a974a7156c7..969721e0330909f45e1906afba418d7b9c057c7e 100644 (file)
@@ -93,6 +93,7 @@ struct nfs_context {
        struct nfs_fh3 rootfh;
        uint64_t readmax;
        uint64_t writemax;
+       char *cwd;
 };
 
 void nfs_free_nfsdir(struct nfsdir *nfsdir)
@@ -142,7 +143,6 @@ struct nfs_mcb_data {
 
 static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh);
 
-
 void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth)
 {
        rpc_set_auth(nfs->rpc, auth);
@@ -334,17 +334,15 @@ struct nfs_context *nfs_init_context(void)
        if (nfs == NULL) {
                return NULL;
        }
+       memset(nfs, 0, sizeof(struct nfs_context));
+
        nfs->rpc = rpc_init_context();
        if (nfs->rpc == NULL) {
                free(nfs);
                return NULL;
        }
 
-       nfs->server = NULL;
-       nfs->export = NULL;
-
-       nfs->rootfh.data.data_len = 0;
-       nfs->rootfh.data.data_val = NULL;
+       nfs->cwd = strdup("/");
 
        return nfs;
 }
@@ -364,6 +362,11 @@ void nfs_destroy_context(struct nfs_context *nfs)
                nfs->export = NULL;
        }
 
+       if (nfs->cwd) {
+               free(nfs->cwd);
+               nfs->cwd = NULL;
+       }
+
        if (nfs->rootfh.data.data_val != NULL) {
                free(nfs->rootfh.data.data_val);
                nfs->rootfh.data.data_val = NULL;
@@ -419,7 +422,7 @@ static void rpc_connect_program_3_cb(struct rpc_context *rpc, int status, void *
 
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
-       if (status == RPC_STATUS_ERROR) {       
+       if (status == RPC_STATUS_ERROR) {
                data->cb(rpc, status, command_data, data->private_data);
                free_rpc_cb_data(data);
                return;
@@ -771,7 +774,7 @@ static void nfs_mount_3_cb(struct rpc_context *rpc, int status, void *command_da
 
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
-       if (status == RPC_STATUS_ERROR) {       
+       if (status == RPC_STATUS_ERROR) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
                free_nfs_cb_data(data);
                return;
@@ -932,7 +935,7 @@ static void nfs_lookup_path_1_cb(struct rpc_context *rpc, int status, void *comm
 
 static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh)
 {
-       char *path, *str;
+       char *path, *slash;
        LOOKUP3args args;
 
        while (*data->path == '/') {
@@ -940,10 +943,16 @@ static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb
        }
 
        path = data->path;
-       str = strchr(path, '/');
-       if (str != NULL) {
-               *str = 0;
-               data->path = str+1;
+       slash = strchr(path, '/');
+       if (slash != NULL) {
+               /* Clear slash so that path is a zero terminated string for
+                * the current path component. Set it back to '/' again later
+                * when we are finished referencing this component so that
+                * data->saved_path will still point to the full
+                * normalized path.
+                */
+               *slash = 0;
+               data->path = slash+1;
        } else {
                while (*data->path != 0) {
                      data->path++;
@@ -960,6 +969,9 @@ static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb
                        return -1;
                }
                memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len);
+               if (slash != NULL) {
+                       *slash = '/';
+               }
                data->continue_cb(nfs, data);
                return 0;
        }
@@ -975,6 +987,109 @@ static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb
                free_nfs_cb_data(data);
                return -1;
        }
+       if (slash != NULL) {
+               *slash = '/';
+       }
+       return 0;
+}
+
+static int nfs_normalize_path(struct nfs_context *nfs, char *path)
+{
+       char *str;
+       int len;
+
+       /* // -> / */
+       while ((str = strstr(path, "//"))) {
+               while(*str) {
+                       *str = *(str + 1);
+                       str++;
+               }
+       }
+
+       /* /./ -> / */
+       while ((str = strstr(path, "/./"))) {
+               while(*(str + 1)) {
+                       *str = *(str + 2);
+                       str++;
+               }
+       }
+
+       /* ^/../ -> error */
+       if (!strncmp(path, "/../", 4)) {
+               rpc_set_error(nfs->rpc,
+                       "Absolute path starts with '/../' "
+                       "during normalization");
+               return -1;
+       }
+
+       /* ^[^/] -> error */
+       if (path[0] != '/') {
+               rpc_set_error(nfs->rpc,
+                       "Absolute path does not start with '/'");
+               return -1;
+       }
+
+       /* /string/../ -> / */
+       while ((str = strstr(path, "/../"))) {
+               char *tmp;
+
+               if (!strncmp(path, "/../", 4)) {
+                       rpc_set_error(nfs->rpc,
+                               "Absolute path starts with '/../' "
+                               "during normalization");
+                       return -1;
+               }
+
+               tmp = str - 1;
+               while (*tmp != '/') {
+                       tmp--;
+               }
+               str += 3;
+               while((*(tmp++) = *(str++)) != '\0')
+                       ;
+       }
+
+       /* /$ -> \0 */
+       len = strlen(path);
+       if (len >= 1) {
+               if (path[len - 1] == '/') {
+                       path[len - 1] = '\0';
+                       len--;
+               }
+       }
+       if (path[0] == '\0') {
+               rpc_set_error(nfs->rpc,
+                       "Absolute path became '' "
+                       "during normalization");
+               return -1;
+       }
+
+       /* /.$ -> \0 */
+       if (len >= 2) {
+               if (!strcmp(&path[len - 2], "/.")) {
+                       path[len - 2] = '\0';
+                       len -= 2;
+               }
+       }
+
+       /* ^/..$ -> error */
+       if (!strcmp(path, "/..")) {
+               rpc_set_error(nfs->rpc,
+                       "Absolute path is '/..' "
+                       "during normalization");
+               return -1;
+       }
+
+       /* /string/..$ -> / */
+       if (len >= 3) {
+               if (!strcmp(&path[len - 3], "/..")) {
+                       char *tmp = &path[len - 3];
+                       while (*--tmp != '/')
+                               ;
+                       *tmp = '\0';
+               }
+       }
+
        return 0;
 }
 
@@ -982,14 +1097,16 @@ static int nfs_lookuppath_async(struct nfs_context *nfs, const char *path, nfs_c
 {
        struct nfs_cb_data *data;
 
-       if (path[0] != 0 && path[0] != '/') {
-               rpc_set_error(nfs->rpc, "Pathname is not absolute %s", path);
-               return -1;
+       if (path[0] == '\0') {
+               path = ".";
        }
 
        data = malloc(sizeof(struct nfs_cb_data));
        if (data == NULL) {
-               rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
+               rpc_set_error(nfs->rpc, "out of memory: failed to allocate "
+                       "nfs_cb_data structure");
+               if (free_continue_data)
+                       free_continue_data(continue_data);
                return -1;
        }
        memset(data, 0, sizeof(struct nfs_cb_data));
@@ -1000,12 +1117,29 @@ static int nfs_lookuppath_async(struct nfs_context *nfs, const char *path, nfs_c
        data->free_continue_data = free_continue_data;
        data->continue_int       = continue_int;
        data->private_data       = private_data;
-       data->saved_path         = strdup(path);
+       if (path[0] == '/') {
+               data->saved_path = strdup(path);
+       } else {
+               data->saved_path = malloc(strlen(path) + strlen(nfs->cwd) + 2);
+               if (data->saved_path == NULL) {
+                       rpc_set_error(nfs->rpc, "out of memory: failed to "
+                               "malloc path string");
+                       free_nfs_cb_data(data);
+                       return -1;
+               }
+               sprintf(data->saved_path, "%s/%s", nfs->cwd, path);
+       }
+
        if (data->saved_path == NULL) {
                rpc_set_error(nfs->rpc, "out of memory: failed to copy path string");
                free_nfs_cb_data(data);
                return -1;
        }
+       if (nfs_normalize_path(nfs, data->saved_path) != 0) {
+               free_nfs_cb_data(data);
+               return -1;
+       }
+
        data->path = data->saved_path;
 
        if (nfs_lookup_path_async_internal(nfs, data, &nfs->rootfh) != 0) {
@@ -1071,7 +1205,7 @@ static void nfs_stat_1_cb(struct rpc_context *rpc, int status, void *command_dat
 #ifndef WIN32
         st.st_blksize = 4096;
         st.st_blocks  = res->GETATTR3res_u.resok.obj_attributes.size / 4096;
-#endif//WIN32        
+#endif//WIN32
         st.st_atime   = res->GETATTR3res_u.resok.obj_attributes.atime.seconds;
         st.st_mtime   = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds;
         st.st_ctime   = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds;
@@ -1108,11 +1242,58 @@ int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *p
 
 
 
-
-
 /*
  * Async open()
  */
+static void nfs_open_trunc_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
+{
+       struct nfs_cb_data *data = private_data;
+       struct nfs_context *nfs = data->nfs;
+       struct nfsfh *nfsfh;
+       SETATTR3res *res;
+
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+       if (status == RPC_STATUS_ERROR) {
+               data->cb(-EFAULT, nfs, command_data, data->private_data);
+               free_nfs_cb_data(data);
+               return;
+       }
+       if (status == RPC_STATUS_CANCEL) {
+               data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
+               free_nfs_cb_data(data);
+               return;
+       }
+
+       res = command_data;
+       if (res->status != NFS3_OK) {
+               rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
+               data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
+               free_nfs_cb_data(data);
+               return;
+       }
+
+       nfsfh = malloc(sizeof(struct nfsfh));
+       if (nfsfh == NULL) {
+               rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
+               data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+               free_nfs_cb_data(data);
+               return;
+       }
+       memset(nfsfh, 0, sizeof(struct nfsfh));
+
+       if (data->continue_int & O_SYNC) {
+               nfsfh->is_sync = 1;
+       }
+
+       /* steal the filehandle */
+       nfsfh->fh = data->fh;
+       data->fh.data.data_val = NULL;
+
+       data->cb(0, nfs, nfsfh, data->private_data);
+       free_nfs_cb_data(data);
+}
+
 static void nfs_open_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
 {
        ACCESS3res *res;
@@ -1166,6 +1347,28 @@ static void nfs_open_cb(struct rpc_context *rpc, int status, void *command_data,
                return;
        }
 
+       /* Try to truncate it if we were requested to */
+       if ((data->continue_int & O_TRUNC) &&
+           (data->continue_int & (O_RDWR|O_WRONLY))) {
+               SETATTR3args args;
+
+               memset(&args, 0, sizeof(SETATTR3args));
+               args.object = data->fh;
+               args.new_attributes.size.set_it = 1;
+               args.new_attributes.size.set_size3_u.size = 0;
+
+               if (rpc_nfs3_setattr_async(nfs->rpc, nfs_open_trunc_cb, &args,
+                               data) != 0) {
+                       rpc_set_error(nfs->rpc, "RPC error: Failed to send "
+                               "SETATTR call for %s", data->path);
+                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc),
+                               data->private_data);
+                       free_nfs_cb_data(data);
+                       return;
+               }
+               return;
+       }
+
        nfsfh = malloc(sizeof(struct nfsfh));
        if (nfsfh == NULL) {
                rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
@@ -1207,17 +1410,19 @@ static int nfs_open_continue_internal(struct nfs_context *nfs, struct nfs_cb_dat
        args.access = nfsmode;
 
        if (rpc_nfs3_access_async(nfs->rpc, nfs_open_cb, &args, data) != 0) {
-               rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
-               data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+               rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS "
+                               "call for %s", data->path);
+               data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc),
+                               data->private_data);
                free_nfs_cb_data(data);
                return -1;
        }
        return 0;
 }
 
-int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
+int nfs_open_async(struct nfs_context *nfs, const char *path, int flags, nfs_cb cb, void *private_data)
 {
-       if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, mode) != 0) {
+       if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, flags) != 0) {
                rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
                return -1;
        }
@@ -1226,7 +1431,31 @@ int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb c
 }
 
 
+/*
+ * Async chdir()
+ */
+static int nfs_chdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
+{
+       /* steal saved_path */
+       free(nfs->cwd);
+       nfs->cwd = data->saved_path;
+       data->saved_path = NULL;
+
+       data->cb(0, nfs, NULL, data->private_data);
+       free_nfs_cb_data(data);
+
+       return 0;
+}
+
+int nfs_chdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
+{
+       if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chdir_continue_internal, NULL, NULL, 0) != 0) {
+               rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
+               return -1;
+       }
 
+       return 0;
+}
 
 
 /*
@@ -1620,7 +1849,7 @@ int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count
 /*
  * close
  */
+
 int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
 {
        if (nfsfh->fh.data.data_val != NULL){
@@ -1843,7 +2072,7 @@ static void nfs_mkdir_cb(struct rpc_context *rpc, int status, void *command_data
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        char *str = data->continue_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        str = &str[strlen(str) + 1];
@@ -1933,7 +2162,7 @@ static void nfs_rmdir_cb(struct rpc_context *rpc, int status, void *command_data
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        char *str = data->continue_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        str = &str[strlen(str) + 1];
@@ -2142,6 +2371,7 @@ int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb
        ptr = strrchr(new_path, '/');
        if (ptr == NULL) {
                rpc_set_error(nfs->rpc, "Invalid path %s", path);
+               free(new_path);
                return -1;
        }
        *ptr = 0;
@@ -2167,7 +2397,7 @@ static void nfs_unlink_cb(struct rpc_context *rpc, int status, void *command_dat
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        char *str = data->continue_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        str = &str[strlen(str) + 1];
@@ -2265,7 +2495,7 @@ static void nfs_mknod_cb(struct rpc_context *rpc, int status, void *command_data
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        char *str = data->continue_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        str = &str[strlen(str) + 1];
@@ -2298,7 +2528,7 @@ static int nfs_mknod_continue_internal(struct nfs_context *nfs, struct nfs_cb_da
        struct mknod_cb_data *cb_data = data->continue_data;
        char *str = cb_data->path;
        MKNOD3args args;
-       
+
        str = &str[strlen(str) + 1];
 
        args.where.dir = data->fh;
@@ -2356,13 +2586,14 @@ int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev
        cb_data->path = strdup(path);
        if (cb_data->path == NULL) {
                rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
-               free(cb_data);          
+               free(cb_data);
                return -1;
        }
 
        ptr = strrchr(cb_data->path, '/');
        if (ptr == NULL) {
                rpc_set_error(nfs->rpc, "Invalid path %s", path);
+               free_mknod_cb_data(cb_data);
                return -1;
        }
        *ptr = 0;
@@ -2467,7 +2698,7 @@ static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_d
        struct entry3 *entry;
        uint64_t cookie = 0;
        struct rdpe_cb_data *rdpe_cb_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        if (status == RPC_STATUS_ERROR) {
@@ -2594,7 +2825,7 @@ static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_da
        struct nfsdir *nfsdir = data->continue_data;
        struct entryplus3 *entry;
        uint64_t cookie = 0;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){
@@ -2649,6 +2880,7 @@ static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_da
                nfsdirent->name = strdup(entry->name);
                if (nfsdirent->name == NULL) {
                        data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
+                       free(nfsdirent);
                        nfs_free_nfsdir(nfsdir);
                        data->continue_data = NULL;
                        free_nfs_cb_data(data);
@@ -2754,15 +2986,24 @@ struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir
 }
 
 
+/*
+ * closedir()
+ */
 void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
 {
        nfs_free_nfsdir(nfsdir);
 }
 
 
-
-
-
+/*
+ * getcwd()
+ */
+void nfs_getcwd(struct nfs_context *nfs, const char **cwd)
+{
+       if (cwd) {
+               *cwd = nfs->cwd;
+       }
+}
 
 
 /*
@@ -2956,7 +3197,7 @@ static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command
                return;
        }
 
-       
+
        data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data);
        free_nfs_cb_data(data);
 }
@@ -3468,7 +3709,7 @@ static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_da
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        struct nfs_symlink_data *symlink_data = data->continue_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        if (status == RPC_STATUS_ERROR) {
@@ -3604,7 +3845,7 @@ static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_dat
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        struct nfs_rename_data *rename_data = data->continue_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        if (status == RPC_STATUS_ERROR) {
@@ -3765,7 +4006,7 @@ static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data,
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        struct nfs_link_data *link_data = data->continue_data;
-       
+
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
        if (status == RPC_STATUS_ERROR) {
@@ -3928,7 +4169,7 @@ void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
                free(nfs->rpc->error_string);
        }
        nfs->rpc->error_string = str;
-    va_end(ap);
+       va_end(ap);
 }
 
 
@@ -3982,7 +4223,7 @@ static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command
        /* Dont want any more callbacks even if the socket is closed */
        rpc->connect_cb = NULL;
 
-       if (status == RPC_STATUS_ERROR) {       
+       if (status == RPC_STATUS_ERROR) {
                data->cb(rpc, -EFAULT, command_data, data->private_data);
                free_mount_cb_data(data);
                return;
@@ -4102,7 +4343,7 @@ int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb c
        if (data->server == NULL) {
                free_mount_cb_data(data);
                return -1;
-       }       
+       }
        if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) {
                free_mount_cb_data(data);
                return -1;