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