]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3d7415f4 | 2 | |
a8fbdf54 TA |
3 | #include <errno.h> |
4 | #include <string.h> | |
5 | ||
b5efdb8a | 6 | #include "alloc-util.h" |
8c9cfc28 | 7 | #include "btrfs-util.h" |
bb15fafe | 8 | #include "import-util.h" |
a8fbdf54 TA |
9 | #include "log.h" |
10 | #include "macro.h" | |
d8b4d14d | 11 | #include "nulstr-util.h" |
bb15fafe | 12 | #include "path-util.h" |
8b43440b | 13 | #include "string-table.h" |
07630cea | 14 | #include "string-util.h" |
3d7415f4 LP |
15 | |
16 | int import_url_last_component(const char *url, char **ret) { | |
17 | const char *e, *p; | |
18 | char *s; | |
19 | ||
20 | e = strchrnul(url, '?'); | |
21 | ||
22 | while (e > url && e[-1] == '/') | |
23 | e--; | |
24 | ||
25 | p = e; | |
26 | while (p > url && p[-1] != '/') | |
27 | p--; | |
28 | ||
29 | if (e <= p) | |
30 | return -EINVAL; | |
31 | ||
32 | s = strndup(p, e - p); | |
33 | if (!s) | |
34 | return -ENOMEM; | |
35 | ||
36 | *ret = s; | |
37 | return 0; | |
38 | } | |
39 | ||
3d7415f4 LP |
40 | int import_url_change_last_component(const char *url, const char *suffix, char **ret) { |
41 | const char *e; | |
42 | char *s; | |
43 | ||
44 | assert(url); | |
45 | assert(ret); | |
46 | ||
47 | e = strchrnul(url, '?'); | |
48 | ||
49 | while (e > url && e[-1] == '/') | |
50 | e--; | |
51 | ||
52 | while (e > url && e[-1] != '/') | |
53 | e--; | |
54 | ||
55 | if (e <= url) | |
56 | return -EINVAL; | |
57 | ||
58 | s = new(char, (e - url) + strlen(suffix) + 1); | |
59 | if (!s) | |
60 | return -ENOMEM; | |
61 | ||
62 | strcpy(mempcpy(s, url, e - url), suffix); | |
63 | *ret = s; | |
64 | return 0; | |
65 | } | |
66 | ||
67 | static const char* const import_verify_table[_IMPORT_VERIFY_MAX] = { | |
68 | [IMPORT_VERIFY_NO] = "no", | |
7f444afa | 69 | [IMPORT_VERIFY_CHECKSUM] = "checksum", |
3d7415f4 LP |
70 | [IMPORT_VERIFY_SIGNATURE] = "signature", |
71 | }; | |
72 | ||
73 | DEFINE_STRING_TABLE_LOOKUP(import_verify, ImportVerify); | |
74 | ||
75 | int tar_strip_suffixes(const char *name, char **ret) { | |
76 | const char *e; | |
77 | char *s; | |
78 | ||
79 | e = endswith(name, ".tar"); | |
80 | if (!e) | |
81 | e = endswith(name, ".tar.xz"); | |
82 | if (!e) | |
83 | e = endswith(name, ".tar.gz"); | |
84 | if (!e) | |
85 | e = endswith(name, ".tar.bz2"); | |
86 | if (!e) | |
87 | e = endswith(name, ".tgz"); | |
88 | if (!e) | |
89 | e = strchr(name, 0); | |
90 | ||
91 | if (e <= name) | |
92 | return -EINVAL; | |
93 | ||
94 | s = strndup(name, e - name); | |
95 | if (!s) | |
96 | return -ENOMEM; | |
97 | ||
98 | *ret = s; | |
99 | return 0; | |
100 | } | |
101 | ||
102 | int raw_strip_suffixes(const char *p, char **ret) { | |
103 | ||
104 | static const char suffixes[] = | |
105 | ".xz\0" | |
106 | ".gz\0" | |
107 | ".bz2\0" | |
108 | ".raw\0" | |
109 | ".qcow2\0" | |
110 | ".img\0" | |
111 | ".bin\0"; | |
112 | ||
113 | _cleanup_free_ char *q = NULL; | |
114 | ||
115 | q = strdup(p); | |
116 | if (!q) | |
117 | return -ENOMEM; | |
118 | ||
119 | for (;;) { | |
120 | const char *sfx; | |
121 | bool changed = false; | |
122 | ||
123 | NULSTR_FOREACH(sfx, suffixes) { | |
124 | char *e; | |
125 | ||
126 | e = endswith(q, sfx); | |
127 | if (e) { | |
128 | *e = 0; | |
129 | changed = true; | |
130 | } | |
131 | } | |
132 | ||
133 | if (!changed) | |
134 | break; | |
135 | } | |
136 | ||
ae2a15bc | 137 | *ret = TAKE_PTR(q); |
3d7415f4 LP |
138 | |
139 | return 0; | |
140 | } | |
141 | ||
8c9cfc28 LP |
142 | int import_assign_pool_quota_and_warn(const char *path) { |
143 | int r; | |
144 | ||
145 | r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true); | |
146 | if (r == -ENOTTY) { | |
147 | log_debug_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines, as directory is not on btrfs or not a subvolume. Ignoring."); | |
148 | return 0; | |
149 | } | |
150 | if (r < 0) | |
151 | return log_error_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines: %m"); | |
152 | if (r > 0) | |
153 | log_info("Set up default quota hierarchy for /var/lib/machines."); | |
154 | ||
155 | r = btrfs_subvol_auto_qgroup(path, 0, true); | |
156 | if (r == -ENOTTY) { | |
157 | log_debug_errno(r, "Failed to set up quota hierarchy for %s, as directory is not on btrfs or not a subvolume. Ignoring.", path); | |
158 | return 0; | |
159 | } | |
160 | if (r < 0) | |
161 | return log_error_errno(r, "Failed to set up default quota hierarchy for %s: %m", path); | |
162 | if (r > 0) | |
b11591af | 163 | log_debug("Set up default quota hierarchy for %s.", path); |
8c9cfc28 LP |
164 | |
165 | return 0; | |
166 | } |