]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mount.ceph: track mon string and path inside ceph_mount_info
authorJeff Layton <jlayton@redhat.com>
Tue, 20 Aug 2019 20:27:14 +0000 (16:27 -0400)
committerJeff Layton <jlayton@redhat.com>
Fri, 13 Sep 2019 12:14:48 +0000 (08:14 -0400)
...and separate the parsing and resolution into two separate
functions.

Signed-off-by: Jeff Layton <jlayton@redhat.com>
src/mount/mount.ceph.c

index 83eeb30723add7b5bf9ab02aa0e9c38b1805e8d3..cb72d65d9ed94a7124f46d0742d3351f12dae581 100644 (file)
@@ -25,6 +25,8 @@ static const char * const EMPTY_STRING = "";
 struct ceph_mount_info {
        unsigned long   cmi_flags;
        char            *cmi_name;
+       char            *cmi_path;
+       char            *cmi_mons;
        char            *cmi_opts;
        int             cmi_opts_len;
        char            cmi_secret[SECRET_BUFSIZE];
@@ -51,49 +53,46 @@ void mount_ceph_debug(const char *fmt, ...)
        }
 }
 
-static char *mount_resolve_src(const char *orig_str)
+static int parse_src(const char *orig_str, struct ceph_mount_info *cmi)
 {
-       int len, pos;
+       size_t len;
        char *mount_path;
-       char *src;
-       char *buf = strdup(orig_str);
-       if (!buf) {
-               fprintf(stderr, "%s: failed to allocate memory\n", __func__);
-               return NULL;
-       }
 
-       mount_path = strstr(buf, ":/");
+       mount_path = strstr(orig_str, ":/");
        if (!mount_path) {
                fprintf(stderr, "source mount path was not specified\n");
-               free(buf);
-               return NULL;
+               return -EINVAL;
        }
-       if (mount_path == buf) {
+       len = mount_path - orig_str;
+       if (len == 0) {
                fprintf(stderr, "server address expected\n");
-               free(buf);
-               return NULL;
+               return -EINVAL;
        }
 
-       *mount_path = '\0';
+       cmi->cmi_mons = strndup(orig_str, len);
+       if (!cmi->cmi_mons)
+               return -ENOMEM;
+
        mount_path++;
+       cmi->cmi_path = strdup(mount_path);
+       if (!cmi->cmi_path)
+               return -ENOMEM;
+       return 0;
+}
 
-       if (!*mount_path) {
-               fprintf(stderr, "incorrect source mount path\n");
-               free(buf);
-               return NULL;
-       }
+static char *finalize_src(struct ceph_mount_info *cmi)
+{
+       int pos, len;
+       char *src;
 
-       src = resolve_addrs(buf);
-       if (!src) {
-               free(buf);
+       src = resolve_addrs(cmi->cmi_mons);
+       if (!src)
                return NULL;
-       }
 
        len = strlen(src);
        pos = safe_cat(&src, &len, len, ":");
-       safe_cat(&src, &len, pos, mount_path);
+       safe_cat(&src, &len, pos, cmi->cmi_path);
 
-       free(buf);
        return src;
 }
 
@@ -311,6 +310,8 @@ static void ceph_mount_info_free(struct ceph_mount_info *cmi)
 {
        free(cmi->cmi_opts);
        free(cmi->cmi_name);
+       free(cmi->cmi_path);
+       free(cmi->cmi_mons);
 }
 
 static int finalize_options(struct ceph_mount_info *cmi)
@@ -348,16 +349,16 @@ int main(int argc, char *argv[])
                goto out;
        }
 
-       rsrc = mount_resolve_src(src);
-       if (!rsrc) {
-               printf(stderr, "failed to resolve source\n");
+       retval = parse_options(opts, &cmi);
+       if (retval) {
+               fprintf(stderr, "failed to parse ceph_options: %d\n", retval);
                retval = EX_USAGE;
                goto out;
        }
 
-       retval = parse_options(opts, &cmi);
+       retval = parse_src(src, &cmi);
        if (retval) {
-               fprintf(stderr, "failed to parse ceph_options: %d\n", retval);
+               fprintf(stderr, "unable to parse mount source: %d\n", retval);
                retval = EX_USAGE;
                goto out;
        }
@@ -365,6 +366,13 @@ int main(int argc, char *argv[])
        /* Ensure the ceph key_type is available */
        modprobe();
 
+       rsrc = finalize_src(&cmi);
+       if (!rsrc) {
+               fprintf(stderr, "failed to resolve source\n");
+               retval = EX_USAGE;
+               goto out;
+       }
+
        retval = finalize_options(&cmi);
        if (retval) {
                fprintf(stderr, "couldn't finalize options: %d\n", retval);