summaryrefslogtreecommitdiff
path: root/libmount
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2023-01-09 13:58:45 +0100
committerKarel Zak <kzak@redhat.com>2023-01-09 13:58:45 +0100
commit10dd0712fa6d0e016fb5caff0719cbf72a98dfa0 (patch)
tree4bcb39bad0f22d7db8206991c110d1e92b202ebc /libmount
parent1a938fce5961446897bbb7bdfe025220880bf951 (diff)
downloadutil-linux-10dd0712fa6d0e016fb5caff0719cbf72a98dfa0.tar.gz
libmount: (optlist) keep parsed options without quotes
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>
Diffstat (limited to 'libmount')
-rw-r--r--libmount/src/mountP.h2
-rw-r--r--libmount/src/optlist.c36
-rw-r--r--libmount/src/optstr.c23
3 files changed, 29 insertions, 32 deletions
diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h
index a34004836..964b89673 100644
--- a/libmount/src/mountP.h
+++ b/libmount/src/mountP.h
@@ -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;
diff --git a/libmount/src/optlist.c b/libmount/src/optlist.c
index 2561f1913..a226028ee 100644
--- a/libmount/src/optlist.c
+++ b/libmount/src/optlist.c
@@ -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)
diff --git a/libmount/src/optstr.c b/libmount/src/optstr.c
index cfac346fe..a8b56e212 100644
--- a/libmount/src/optstr.c
+++ b/libmount/src/optstr.c
@@ -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;
}