summaryrefslogtreecommitdiffstats
path: root/src/vmspawn/vmspawn-mount.c
blob: ee63bda96c77a12119de7001902ecf152d455eac (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "extract-word.h"
#include "macro.h"
#include "parse-argument.h"
#include "path-util.h"
#include "string-util.h"
#include "vmspawn-mount.h"

static void runtime_mount_done(RuntimeMount *mount) {
        assert(mount);

        mount->source = mfree(mount->source);
        mount->target = mfree(mount->target);
}

void runtime_mount_context_done(RuntimeMountContext *ctx) {
        assert(ctx);

        FOREACH_ARRAY(mount, ctx->mounts, ctx->n_mounts)
                runtime_mount_done(mount);

        free(ctx->mounts);
}

int runtime_mount_parse(RuntimeMountContext *ctx, const char *s, bool read_only) {
        _cleanup_(runtime_mount_done) RuntimeMount mount = { .read_only = read_only };
        _cleanup_free_ char *source_rel = NULL;
        int r;

        assert(ctx);

        r = extract_first_word(&s, &source_rel, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
        if (r < 0)
                return r;
        if (r == 0)
                return -EINVAL;

        if (isempty(source_rel))
                return -EINVAL;

        r = path_make_absolute_cwd(source_rel, &mount.source);
        if (r < 0)
                return r;

        /* virtiofsd only supports directories */
        r = is_dir(mount.source, /* follow= */ true);
        if (r < 0)
                return r;
        if (!r)
                return -ENOTDIR;

        mount.target = s ? strdup(s) : TAKE_PTR(source_rel);
        if (!mount.target)
                return -ENOMEM;

        if (!path_is_absolute(mount.target))
                return -EINVAL;

        if (!GREEDY_REALLOC(ctx->mounts, ctx->n_mounts + 1))
                return log_oom();

        ctx->mounts[ctx->n_mounts++] = TAKE_STRUCT(mount);

        return 0;
}