fstab-util.c revision c5e04d51277994cca29234b33a6b8fc90a183cf3
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/***
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright 2015 Zbigniew Jędrzejewski-Szmek
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering (at your option) any later version.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is distributed in the hope that it will be useful, but
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Lesser General Public License for more details.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering***/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "fstab-util.h"
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "strv.h"
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "util.h"
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther
6482f6269c87d2249e52e889a63adbdd50f2d691Ronny Chevalierint fstab_filter_options(const char *opts, const char *names,
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier const char **namefound, char **value, char **filtered) {
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek const char *name, *n = NULL, *x;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering _cleanup_strv_free_ char **stor = NULL;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *v = NULL, **strv = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek assert(names && *names);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek if (!opts)
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek goto answer;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering /* If !value and !filtered, this function is not allowed to fail. */
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering if (!filtered) {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering const char *word, *state;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering size_t l;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering FOREACH_WORD_SEPARATOR(word, l, opts, ",", state)
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering NULSTR_FOREACH(name, names) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering if (l < strlen(name))
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering continue;
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering if (!strneq(word, name, strlen(name)))
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering continue;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering /* we know that the string is NUL
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering * terminated, so *x is valid */
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering x = word + strlen(name);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek if (IN_SET(*x, '\0', '=', ',')) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering n = name;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek if (value) {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek free(v);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek if (IN_SET(*x, '\0', ','))
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering v = NULL;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering else {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert(*x == '=');
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering x++;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering v = strndup(x, l - strlen(name) - 1);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering if (!v)
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering return -ENOMEM;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering }
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering }
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering }
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering }
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering } else {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering char **t, **s;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering stor = strv_split(opts, ",");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering if (!stor)
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering return -ENOMEM;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering strv = memdup(stor, sizeof(char*) * (strv_length(stor) + 1));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering if (!strv)
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek return -ENOMEM;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek for (s = t = strv; *s; s++) {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek NULSTR_FOREACH(name, names) {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek x = startswith(*s, name);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering if (x && IN_SET(*x, '\0', '='))
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering goto found;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering }
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering *t = *s;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering t++;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering continue;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering found:
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering /* Keep the last occurence found */
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering n = name;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering if (value) {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering free(v);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering if (*x == '\0')
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering v = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek else {
320814811417146cfa1e416f69f1101eed630c36Luke Shumaker assert(*x == '=');
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering x++;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering v = strdup(x);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering if (!v)
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering return -ENOMEM;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering }
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering }
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering }
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering *t = NULL;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek }
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringanswer:
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering if (namefound)
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering *namefound = n;
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering if (filtered) {
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering char *f;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering f = strv_join(strv, ",");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering if (!f)
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering return -ENOMEM;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering *filtered = f;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering }
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering if (value) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering *value = v;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering v = NULL;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering }
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering return !!n;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringint fstab_find_pri(const char *options, int *ret) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_free_ char *opt = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering int r;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering unsigned pri;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert(ret);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering r = fstab_filter_options(options, "pri\0", NULL, &opt, NULL);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering if (r <= 0)
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering return r;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering r = safe_atou(opt, &pri);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering if (r < 0)
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering return r;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering if ((int) pri < 0)
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering return -ERANGE;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
fed1e721fd0c81e60c77120539f34e16c2585634Lennart Poettering *ret = (int) r;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering return 1;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering}
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering