forked from checkpoint-restore/criu
/
external.c
94 lines (75 loc) · 1.59 KB
/
external.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "common/err.h"
#include "common/list.h"
#include "cr_options.h"
#include "xmalloc.h"
#include "mount.h"
#include "external.h"
#include "util.h"
#include "net.h"
int add_external(char *key)
{
struct external *ext;
ext = xmalloc(sizeof(*ext));
if (!ext)
return -1;
ext->id = key;
if (strstartswith(key, "macvlan") && macvlan_ext_add(ext) < 0) {
xfree(ext);
return -1;
}
if (strstartswith(key, "mnt[]")) {
xfree(ext);
return ext_mount_parse_auto(key + 5);
}
list_add(&ext->node, &opts.external);
return 0;
}
bool external_lookup_id(char *id)
{
struct external *ext;
list_for_each_entry(ext, &opts.external, node)
if (!strcmp(ext->id, id))
return true;
return false;
}
void *external_lookup_data(char *key)
{
struct external *ext;
int len = strlen(key);
list_for_each_entry(ext, &opts.external, node) {
if (strncmp(ext->id, key, len))
continue;
return ext->data;
}
return ERR_PTR(-ENOENT);
}
char *external_lookup_by_key(char *key)
{
struct external *ext;
int len = strlen(key);
list_for_each_entry(ext, &opts.external, node) {
if (strncmp(ext->id, key, len))
continue;
if (ext->id[len] == ':')
return ext->id + len + 1;
else if (ext->id[len] == '\0')
return NULL;
}
return ERR_PTR(-ENOENT);
}
int external_for_each_type(char *type, int (*cb)(struct external *, void *), void *arg)
{
struct external *ext;
int ln = strlen(type);
int ret = 0;
list_for_each_entry(ext, &opts.external, node) {
if (strncmp(ext->id, type, ln))
continue;
if (ext->id[ln] != '[')
continue;
ret = cb(ext, arg);
if (ret)
break;
}
return ret;
}