]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fstab-util.c
1a3bc46d2a2d253a2f11205ec0d55b55e86a231a
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Zbigniew Jędrzejewski-Szmek
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "alloc-util.h"
28 #include "device-nodes.h"
29 #include "fstab-util.h"
31 #include "mount-util.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "string-util.h"
38 int fstab_has_fstype(const char *fstype
) {
39 _cleanup_endmntent_
FILE *f
= NULL
;
42 f
= setmntent("/etc/fstab", "re");
44 return errno
== ENOENT
? false : -errno
;
50 return errno
!= 0 ? -errno
: false;
52 if (streq(m
->mnt_type
, fstype
))
58 int fstab_is_mount_point(const char *mount
) {
59 _cleanup_endmntent_
FILE *f
= NULL
;
62 f
= setmntent("/etc/fstab", "re");
64 return errno
== ENOENT
? false : -errno
;
70 return errno
!= 0 ? -errno
: false;
72 if (path_equal(m
->mnt_dir
, mount
))
78 int fstab_filter_options(const char *opts
, const char *names
,
79 const char **namefound
, char **value
, char **filtered
) {
80 const char *name
, *n
= NULL
, *x
;
81 _cleanup_strv_free_
char **stor
= NULL
;
82 _cleanup_free_
char *v
= NULL
, **strv
= NULL
;
84 assert(names
&& *names
);
89 /* If !value and !filtered, this function is not allowed to fail. */
92 const char *word
, *state
;
95 FOREACH_WORD_SEPARATOR(word
, l
, opts
, ",", state
)
96 NULSTR_FOREACH(name
, names
) {
99 if (!strneq(word
, name
, strlen(name
)))
102 /* we know that the string is NUL
103 * terminated, so *x is valid */
104 x
= word
+ strlen(name
);
105 if (IN_SET(*x
, '\0', '=', ',')) {
109 if (IN_SET(*x
, '\0', ','))
114 v
= strndup(x
, l
- strlen(name
) - 1);
124 stor
= strv_split(opts
, ",");
127 strv
= memdup(stor
, sizeof(char*) * (strv_length(stor
) + 1));
131 for (s
= t
= strv
; *s
; s
++) {
132 NULSTR_FOREACH(name
, names
) {
133 x
= startswith(*s
, name
);
134 if (x
&& IN_SET(*x
, '\0', '='))
142 /* Keep the last occurence found */
166 f
= strv_join(strv
, ",");
173 *value
= TAKE_PTR(v
);
178 int fstab_extract_values(const char *opts
, const char *name
, char ***values
) {
179 _cleanup_strv_free_
char **optsv
= NULL
, **res
= NULL
;
186 optsv
= strv_split(opts
, ",");
190 STRV_FOREACH(s
, optsv
) {
194 arg
= startswith(*s
, name
);
195 if (!arg
|| *arg
!= '=')
197 r
= strv_extend(&res
, arg
+ 1);
202 *values
= TAKE_PTR(res
);
207 int fstab_find_pri(const char *options
, int *ret
) {
208 _cleanup_free_
char *opt
= NULL
;
214 r
= fstab_filter_options(options
, "pri\0", NULL
, &opt
, NULL
);
220 r
= safe_atou(opt
, &pri
);
231 static char *unquote(const char *s
, const char* quotes
) {
235 /* This is rather stupid, simply removes the heading and
236 * trailing quotes if there is one. Doesn't care about
237 * escaping or anything.
239 * DON'T USE THIS FOR NEW CODE ANYMORE! */
245 if (strchr(quotes
, s
[0]) && s
[l
-1] == s
[0])
246 return strndup(s
+1, l
-2);
251 static char *tag_to_udev_node(const char *tagvalue
, const char *by
) {
252 _cleanup_free_
char *t
= NULL
, *u
= NULL
;
255 u
= unquote(tagvalue
, QUOTES
);
259 enc_len
= strlen(u
) * 4 + 1;
260 t
= new(char, enc_len
);
264 if (encode_devnode_name(u
, t
, enc_len
) < 0)
267 return strjoin("/dev/disk/by-", by
, "/", t
);
270 char *fstab_node_to_udev_node(const char *p
) {
273 if (startswith(p
, "LABEL="))
274 return tag_to_udev_node(p
+6, "label");
276 if (startswith(p
, "UUID="))
277 return tag_to_udev_node(p
+5, "uuid");
279 if (startswith(p
, "PARTUUID="))
280 return tag_to_udev_node(p
+9, "partuuid");
282 if (startswith(p
, "PARTLABEL="))
283 return tag_to_udev_node(p
+10, "partlabel");