summaryrefslogtreecommitdiffstats
path: root/src/shared/portable-util.c
blob: 85c128a1b4497a4288ca5e5d0afc67bf02ec261d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "fs-util.h"
#include "nulstr-util.h"
#include "portable-util.h"
#include "string-util.h"

int find_portable_profile(const char *name, const char *unit, char **ret_path) {
        const char *dot;
        int r;

        assert(name);
        assert(ret_path);

        assert_se(dot = strrchr(unit, '.'));

        NULSTR_FOREACH(p, PORTABLE_PROFILE_DIRS) {
                _cleanup_free_ char *joined = NULL;

                joined = strjoin(p, "/", name, "/", dot + 1, ".conf");
                if (!joined)
                        return -ENOMEM;

                r = access_nofollow(joined, F_OK);
                if (r >= 0) {
                        *ret_path = TAKE_PTR(joined);
                        return 0;
                }
                if (r != -ENOENT)
                        return r;
        }

        return -ENOENT;
}