]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: (optlist) keep parsed options without quotes
authorKarel Zak <kzak@redhat.com>
Mon, 9 Jan 2023 12:58:45 +0000 (13:58 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 9 Jan 2023 12:58:45 +0000 (13:58 +0100)
Some mount options uses quotes due to commas in the value, for
example (SELinux) mount -o 'context="foo,bar"'.

Let's keep this parsed values without quotes in options list. It will
make things more simple.

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/mountP.h
libmount/src/optlist.c
libmount/src/optstr.c

index a34004836288173188e088cb6fbdb384f826f925..964b8967356c57653e2d4cc5186f5600dc3b92cf 100644 (file)
@@ -487,7 +487,7 @@ extern int mnt_optstr_remove_option_at(char **optstr, char *begin, char *end);
 
 extern int mnt_buffer_append_option(struct ul_buffer *buf,
                         const char *name, size_t namesz,
-                        const char *val, size_t valsz);
+                        const char *val, size_t valsz, int quoted);
 
 /* optlist.h */
 struct libmnt_opt;
index 2561f1913cd48a32878203499a1ea7f4fc7cf17e..a226028ee868ab6dc399242c9bf07c3a07533619 100644 (file)
@@ -43,7 +43,8 @@ struct libmnt_opt {
 
        enum libmnt_optsrc      src;
 
-       unsigned int external : 1;      /* visible for external helpers only */
+       unsigned int external : 1,      /* visible for external helpers only */
+                    quoted : 1;        /* name="value" */
 };
 
 struct libmnt_optlist {
@@ -375,6 +376,11 @@ static struct libmnt_opt *optlist_new_opt(struct libmnt_optlist *ls,
        opt->ent = ent;
 
        if (valsz) {
+               if (*value == '"' && *(value + valsz - 1) == '"') {
+                       opt->quoted = 1;
+                       value++;
+                       valsz -= 2;
+               }
                opt->value = strndup(value, valsz);
                if (!opt->value)
                        goto fail;
@@ -840,7 +846,7 @@ int mnt_optlist_strdup_optstr(struct libmnt_optlist *ls, char **optstr,
                 what == MNT_OL_FLTR_ALL ||
                 what == MNT_OL_FLTR_HELPERS)) {
 
-               rc = mnt_buffer_append_option(&buf, "rw", 2, NULL, 0);
+               rc = mnt_buffer_append_option(&buf, "rw", 2, NULL, 0, 0);
                if (rc)
                        goto fail;
                xx_wanted = 1;
@@ -860,7 +866,8 @@ int mnt_optlist_strdup_optstr(struct libmnt_optlist *ls, char **optstr,
                rc = mnt_buffer_append_option(&buf,
                                        opt->name, strlen(opt->name),
                                        opt->value,
-                                       opt->value ? strlen(opt->value) : 0);
+                                       opt->value ? strlen(opt->value) : 0,
+                                       opt->quoted);
                if (rc)
                        goto fail;
        }
@@ -943,6 +950,7 @@ struct libmnt_optlist *mnt_copy_optlist(struct libmnt_optlist *ls)
                if (no) {
                        no->src = opt->src;
                        no->external = opt->external;
+                       no->quoted = opt->quoted;
                }
        }
 
@@ -1060,26 +1068,8 @@ int mnt_opt_set_u64value(struct libmnt_opt *opt, uint64_t num)
 
 int mnt_opt_set_quoted_value(struct libmnt_opt *opt, const char *str)
 {
-       char *value = NULL;
-
-       if (str && *str) {
-               size_t len = strlen(str);
-               char *p;
-
-               assert(len);
-               p = value = malloc(len + 3);
-               if (!value)
-                       return -ENOMEM;
-               *p++ = '"';
-               memcpy(p, str, len);
-               p += len;
-               *p = '"';
-       }
-
-       free(opt->value);
-       opt->value = value;
-
-       return 0;
+       opt->quoted = 1;
+       return mnt_opt_set_value(opt, str);
 }
 
 int mnt_opt_set_external(struct libmnt_opt *opt, int enable)
index cfac346fe1ba86503cd6d7df1d389e0d50fe48cc..a8b56e212f71ee3f98965e13c81b740fe623e788 100644 (file)
@@ -100,7 +100,8 @@ int mnt_optstr_next_option(char **optstr, char **name, size_t *namesz,
 
 int mnt_buffer_append_option(struct ul_buffer *buf,
                        const char *name, size_t namesz,
-                       const char *val, size_t valsz)
+                       const char *val, size_t valsz,
+                       int quoted)
 {
        int rc = 0;
 
@@ -112,8 +113,14 @@ int mnt_buffer_append_option(struct ul_buffer *buf,
                /* we need to append '=' is value is empty string, see
                 * 727c689908c5e68c92aa1dd65e0d3bdb6d91c1e5 */
                rc = ul_buffer_append_data(buf, "=", 1);
-               if (!rc && valsz)
-                       rc = ul_buffer_append_data(buf, val, valsz);
+               if (!rc && valsz) {
+                       if (quoted)
+                               rc = ul_buffer_append_data(buf, "\"", 1);
+                       if (!rc)
+                               rc = ul_buffer_append_data(buf, val, valsz);
+                       if (quoted)
+                               rc = ul_buffer_append_data(buf, "\"", 1);
+               }
        }
        return rc;
 }
@@ -145,7 +152,7 @@ int mnt_optstr_append_option(char **optstr, const char *name, const char *value)
        ul_buffer_refer_string(&buf, *optstr);
        ul_buffer_set_chunksize(&buf, osz + nsz + vsz + 3);     /* to call realloc() only once */
 
-       rc = mnt_buffer_append_option(&buf, name, nsz, value, vsz);
+       rc = mnt_buffer_append_option(&buf, name, nsz, value, vsz, 0);
        if (!rc)
                *optstr = ul_buffer_get_data(&buf, NULL, NULL);
        else if (osz == 0)
@@ -179,7 +186,7 @@ int mnt_optstr_prepend_option(char **optstr, const char *name, const char *value
 
        ul_buffer_set_chunksize(&buf, osz + nsz + vsz + 3);   /* to call realloc() only once */
 
-       rc = mnt_buffer_append_option(&buf, name, nsz, value, vsz);
+       rc = mnt_buffer_append_option(&buf, name, nsz, value, vsz, 0);
        if (*optstr && !rc) {
                rc = ul_buffer_append_data(&buf, ",", 1);
                if (!rc)
@@ -481,7 +488,7 @@ int mnt_split_optstr(const char *optstr, char **user, char **vfs,
                if (buf) {
                        if (ul_buffer_is_empty(buf))
                                ul_buffer_set_chunksize(buf, chunsz);
-                       rc = mnt_buffer_append_option(buf, name, namesz, val, valsz);
+                       rc = mnt_buffer_append_option(buf, name, namesz, val, valsz, 0);
                }
                if (rc)
                        break;
@@ -551,7 +558,7 @@ int mnt_optstr_get_options(const char *optstr, char **subset,
                if (valsz && mnt_optmap_entry_novalue(ent))
                        continue;
 
-               rc = mnt_buffer_append_option(&buf, name, namesz, val, valsz);
+               rc = mnt_buffer_append_option(&buf, name, namesz, val, valsz, 0);
                if (rc)
                        break;
        }
@@ -768,7 +775,7 @@ int mnt_optstr_apply_flags(char **optstr, unsigned long flags,
                        } else
                                sz = strlen(ent->name);
 
-                       rc = mnt_buffer_append_option(&buf, ent->name, sz, NULL, 0);
+                       rc = mnt_buffer_append_option(&buf, ent->name, sz, NULL, 0, 0);
                        if (rc)
                                break;
                }