]>
git.proxmox.com Git - systemd.git/blob - src/shared/fstab-util.c
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
, ",");
180 int fstab_extract_values(const char *opts
, const char *name
, char ***values
) {
181 _cleanup_strv_free_
char **optsv
= NULL
, **res
= NULL
;
188 optsv
= strv_split(opts
, ",");
192 STRV_FOREACH(s
, optsv
) {
196 arg
= startswith(*s
, name
);
197 if (!arg
|| *arg
!= '=')
199 r
= strv_extend(&res
, arg
+ 1);
210 int fstab_find_pri(const char *options
, int *ret
) {
211 _cleanup_free_
char *opt
= NULL
;
217 r
= fstab_filter_options(options
, "pri\0", NULL
, &opt
, NULL
);
223 r
= safe_atou(opt
, &pri
);
234 static char *unquote(const char *s
, const char* quotes
) {
238 /* This is rather stupid, simply removes the heading and
239 * trailing quotes if there is one. Doesn't care about
240 * escaping or anything.
242 * DON'T USE THIS FOR NEW CODE ANYMORE! */
248 if (strchr(quotes
, s
[0]) && s
[l
-1] == s
[0])
249 return strndup(s
+1, l
-2);
254 static char *tag_to_udev_node(const char *tagvalue
, const char *by
) {
255 _cleanup_free_
char *t
= NULL
, *u
= NULL
;
258 u
= unquote(tagvalue
, QUOTES
);
262 enc_len
= strlen(u
) * 4 + 1;
263 t
= new(char, enc_len
);
267 if (encode_devnode_name(u
, t
, enc_len
) < 0)
270 return strjoin("/dev/disk/by-", by
, "/", t
);
273 char *fstab_node_to_udev_node(const char *p
) {
276 if (startswith(p
, "LABEL="))
277 return tag_to_udev_node(p
+6, "label");
279 if (startswith(p
, "UUID="))
280 return tag_to_udev_node(p
+5, "uuid");
282 if (startswith(p
, "PARTUUID="))
283 return tag_to_udev_node(p
+9, "partuuid");
285 if (startswith(p
, "PARTLABEL="))
286 return tag_to_udev_node(p
+10, "partlabel");