]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fstab-util.c
6b73e25f89aa635cf1ffb99ae60a638088845641
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "device-nodes.h"
10 #include "fstab-util.h"
12 #include "mount-util.h"
13 #include "nulstr-util.h"
14 #include "parse-util.h"
15 #include "path-util.h"
16 #include "string-util.h"
19 int fstab_has_fstype(const char *fstype
) {
20 _cleanup_endmntent_
FILE *f
= NULL
;
23 f
= setmntent("/etc/fstab", "re");
25 return errno
== ENOENT
? false : -errno
;
31 return errno
!= 0 ? -errno
: false;
33 if (streq(m
->mnt_type
, fstype
))
39 int fstab_is_mount_point(const char *mount
) {
40 _cleanup_endmntent_
FILE *f
= NULL
;
43 f
= setmntent("/etc/fstab", "re");
45 return errno
== ENOENT
? false : -errno
;
51 return errno
!= 0 ? -errno
: false;
53 if (path_equal(m
->mnt_dir
, mount
))
59 int fstab_filter_options(const char *opts
, const char *names
,
60 const char **namefound
, char **value
, char **filtered
) {
61 const char *name
, *n
= NULL
, *x
;
62 _cleanup_strv_free_
char **stor
= NULL
;
63 _cleanup_free_
char *v
= NULL
, **strv
= NULL
;
65 assert(names
&& *names
);
70 /* If !value and !filtered, this function is not allowed to fail. */
73 const char *word
, *state
;
76 FOREACH_WORD_SEPARATOR(word
, l
, opts
, ",", state
)
77 NULSTR_FOREACH(name
, names
) {
80 if (!strneq(word
, name
, strlen(name
)))
83 /* we know that the string is NUL
84 * terminated, so *x is valid */
85 x
= word
+ strlen(name
);
86 if (IN_SET(*x
, '\0', '=', ',')) {
90 if (IN_SET(*x
, '\0', ','))
95 v
= strndup(x
, l
- strlen(name
) - 1);
105 stor
= strv_split(opts
, ",");
108 strv
= memdup(stor
, sizeof(char*) * (strv_length(stor
) + 1));
112 for (s
= t
= strv
; *s
; s
++) {
113 NULSTR_FOREACH(name
, names
) {
114 x
= startswith(*s
, name
);
115 if (x
&& IN_SET(*x
, '\0', '='))
123 /* Keep the last occurrence found */
147 f
= strv_join(strv
, ",");
154 *value
= TAKE_PTR(v
);
159 int fstab_extract_values(const char *opts
, const char *name
, char ***values
) {
160 _cleanup_strv_free_
char **optsv
= NULL
, **res
= NULL
;
167 optsv
= strv_split(opts
, ",");
171 STRV_FOREACH(s
, optsv
) {
175 arg
= startswith(*s
, name
);
176 if (!arg
|| *arg
!= '=')
178 r
= strv_extend(&res
, arg
+ 1);
183 *values
= TAKE_PTR(res
);
188 int fstab_find_pri(const char *options
, int *ret
) {
189 _cleanup_free_
char *opt
= NULL
;
195 r
= fstab_filter_options(options
, "pri\0", NULL
, &opt
, NULL
);
201 r
= safe_atou(opt
, &pri
);
212 static char *unquote(const char *s
, const char* quotes
) {
216 /* This is rather stupid, simply removes the heading and
217 * trailing quotes if there is one. Doesn't care about
218 * escaping or anything.
220 * DON'T USE THIS FOR NEW CODE ANYMORE! */
226 if (strchr(quotes
, s
[0]) && s
[l
-1] == s
[0])
227 return strndup(s
+1, l
-2);
232 static char *tag_to_udev_node(const char *tagvalue
, const char *by
) {
233 _cleanup_free_
char *t
= NULL
, *u
= NULL
;
236 u
= unquote(tagvalue
, QUOTES
);
240 enc_len
= strlen(u
) * 4 + 1;
241 t
= new(char, enc_len
);
245 if (encode_devnode_name(u
, t
, enc_len
) < 0)
248 return strjoin("/dev/disk/by-", by
, "/", t
);
251 char *fstab_node_to_udev_node(const char *p
) {
254 if (startswith(p
, "LABEL="))
255 return tag_to_udev_node(p
+6, "label");
257 if (startswith(p
, "UUID="))
258 return tag_to_udev_node(p
+5, "uuid");
260 if (startswith(p
, "PARTUUID="))
261 return tag_to_udev_node(p
+9, "partuuid");
263 if (startswith(p
, "PARTLABEL="))
264 return tag_to_udev_node(p
+10, "partlabel");