]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/fstab-util.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / shared / fstab-util.c
CommitLineData
d15d0333
ZJS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2015 Zbigniew Jędrzejewski-Szmek
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
6bedfcbb
LP
22#include "fstab-util.h"
23#include "parse-util.h"
0b6b6787 24#include "path-util.h"
07630cea 25#include "string-util.h"
d15d0333
ZJS
26#include "strv.h"
27#include "util.h"
28
0b6b6787 29bool fstab_is_mount_point(const char *mount) {
0b6b6787
KS
30 _cleanup_endmntent_ FILE *f = NULL;
31 struct mntent *m;
32
33 f = setmntent("/etc/fstab", "r");
34 if (!f)
35 return false;
36
37 while ((m = getmntent(f)))
38 if (path_equal(m->mnt_dir, mount))
39 return true;
40
41 return false;
42}
43
d15d0333
ZJS
44int fstab_filter_options(const char *opts, const char *names,
45 const char **namefound, char **value, char **filtered) {
46 const char *name, *n = NULL, *x;
47 _cleanup_strv_free_ char **stor = NULL;
48 _cleanup_free_ char *v = NULL, **strv = NULL;
49
50 assert(names && *names);
51
52 if (!opts)
53 goto answer;
54
55 /* If !value and !filtered, this function is not allowed to fail. */
56
57 if (!filtered) {
58 const char *word, *state;
59 size_t l;
60
61 FOREACH_WORD_SEPARATOR(word, l, opts, ",", state)
62 NULSTR_FOREACH(name, names) {
63 if (l < strlen(name))
64 continue;
65 if (!strneq(word, name, strlen(name)))
66 continue;
67
68 /* we know that the string is NUL
69 * terminated, so *x is valid */
70 x = word + strlen(name);
71 if (IN_SET(*x, '\0', '=', ',')) {
72 n = name;
73 if (value) {
74 free(v);
75 if (IN_SET(*x, '\0', ','))
76 v = NULL;
77 else {
78 assert(*x == '=');
79 x++;
80 v = strndup(x, l - strlen(name) - 1);
81 if (!v)
82 return -ENOMEM;
83 }
84 }
85 }
86 }
87 } else {
88 char **t, **s;
89
90 stor = strv_split(opts, ",");
91 if (!stor)
92 return -ENOMEM;
93 strv = memdup(stor, sizeof(char*) * (strv_length(stor) + 1));
94 if (!strv)
95 return -ENOMEM;
96
97 for (s = t = strv; *s; s++) {
98 NULSTR_FOREACH(name, names) {
99 x = startswith(*s, name);
100 if (x && IN_SET(*x, '\0', '='))
101 goto found;
102 }
103
104 *t = *s;
105 t++;
106 continue;
107 found:
108 /* Keep the last occurence found */
109 n = name;
110 if (value) {
111 free(v);
112 if (*x == '\0')
113 v = NULL;
114 else {
115 assert(*x == '=');
116 x++;
117 v = strdup(x);
118 if (!v)
119 return -ENOMEM;
120 }
121 }
122 }
123 *t = NULL;
124 }
125
126answer:
127 if (namefound)
128 *namefound = n;
129 if (filtered) {
130 char *f;
131
132 f = strv_join(strv, ",");
133 if (!f)
134 return -ENOMEM;
135
136 *filtered = f;
137 }
138 if (value) {
139 *value = v;
140 v = NULL;
141 }
142
143 return !!n;
144}
145
3519d230
KZ
146int fstab_extract_values(const char *opts, const char *name, char ***values) {
147 _cleanup_strv_free_ char **optsv = NULL, **res = NULL;
148 char **s;
149
150 assert(opts);
151 assert(name);
152 assert(values);
153
154 optsv = strv_split(opts, ",");
155 if (!optsv)
156 return -ENOMEM;
157
158 STRV_FOREACH(s, optsv) {
159 char *arg;
160 int r;
161
162 arg = startswith(*s, name);
163 if (!arg || *arg != '=')
164 continue;
165 r = strv_extend(&res, arg + 1);
166 if (r < 0)
167 return r;
168 }
169
170 *values = res;
171 res = NULL;
172
173 return !!*values;
174}
175
d15d0333
ZJS
176int fstab_find_pri(const char *options, int *ret) {
177 _cleanup_free_ char *opt = NULL;
178 int r;
179 unsigned pri;
180
181 assert(ret);
182
183 r = fstab_filter_options(options, "pri\0", NULL, &opt, NULL);
a75f4e2a 184 if (r < 0)
d15d0333 185 return r;
a75f4e2a
ZJS
186 if (r == 0 || !opt)
187 return 0;
d15d0333
ZJS
188
189 r = safe_atou(opt, &pri);
190 if (r < 0)
191 return r;
192
c5e04d51
ZJS
193 if ((int) pri < 0)
194 return -ERANGE;
195
a75f4e2a 196 *ret = (int) pri;
d15d0333
ZJS
197 return 1;
198}