| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "btrfs-util.h" |
| 5 |
#include "chattr-util.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "import-util.h" |
| 8 |
#include "log.h" |
| 9 |
#include "nulstr-util.h" |
| 10 |
#include "string-table.h" |
| 11 |
#include "string-util.h" |
| 12 |
|
| 13 |
static const char *skip_protocol_and_hostname(const char *url) { |
| 14 |
const char *d; |
| 15 |
size_t n; |
| 16 |
|
| 17 |
/* A very very lenient implementation of RFC3986 Section 3.2 */ |
| 18 |
|
| 19 |
/* Find colon separating protocol and hostname */ |
| 20 |
d = strchr(url, ':'); |
| 21 |
if (!d || url == d) |
| 22 |
return NULL; |
| 23 |
d++; |
| 24 |
|
| 25 |
/* Skip slashes after colon */ |
| 26 |
d += strspn(d, "/"); |
| 27 |
|
| 28 |
/* Skip everything till next slash or end */ |
| 29 |
n = strcspn(d, "/?#"); |
| 30 |
if (n == 0) |
| 31 |
return NULL; |
| 32 |
|
| 33 |
return d + n; |
| 34 |
} |
| 35 |
|
| 36 |
int import_url_last_component( |
| 37 |
const char *url, |
| 38 |
char **ret) { |
| 39 |
|
| 40 |
const char *e, *p, *h; |
| 41 |
|
| 42 |
/* This extracts the last path component of the specified URI, i.e. the last non-empty substrings |
| 43 |
* between two "/" characters. This ignores "Query" and "Fragment" suffixes (as per RFC3986). */ |
| 44 |
|
| 45 |
h = skip_protocol_and_hostname(url); |
| 46 |
if (!h) |
| 47 |
return -EINVAL; |
| 48 |
|
| 49 |
e = h + strcspn(h, "?#"); /* Cut off "Query" and "Fragment" */ |
| 50 |
|
| 51 |
while (e > h && e[-1] == '/') /* Eat trailing slashes */ |
| 52 |
e--; |
| 53 |
|
| 54 |
p = e; |
| 55 |
while (p > h && p[-1] != '/') /* Find component before that */ |
| 56 |
p--; |
| 57 |
|
| 58 |
if (e <= p) /* Empty component? */ |
| 59 |
return -EADDRNOTAVAIL; |
| 60 |
|
| 61 |
if (ret) { |
| 62 |
char *s; |
| 63 |
|
| 64 |
s = strndup(p, e - p); |
| 65 |
if (!s) |
| 66 |
return -ENOMEM; |
| 67 |
|
| 68 |
*ret = s; |
| 69 |
} |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
int import_url_change_suffix( |
| 75 |
const char *url, |
| 76 |
size_t n_drop_components, |
| 77 |
const char *suffix, |
| 78 |
char **ret) { |
| 79 |
|
| 80 |
const char *e, *h; |
| 81 |
char *s; |
| 82 |
|
| 83 |
assert(url); |
| 84 |
assert(ret); |
| 85 |
|
| 86 |
/* This drops the specified number of path components of the specified URI, i.e. the specified number |
| 87 |
* of non-empty substring between two "/" characters from the end of the string, and then append the |
| 88 |
* specified suffix instead. Before doing all this it chops off the "Query" and "Fragment" suffixes |
| 89 |
* (they are *not* re-added to the final URL). Note that n_drop_components may be 0 (in which case the |
| 90 |
* component are simply added to the end). The suffix may be specified as NULL or empty string in |
| 91 |
* which case nothing is appended, only the specified number of components chopped off. Note that the |
| 92 |
* function may be called with n_drop_components == 0 and suffix == NULL, in which case the "Query" |
| 93 |
* and "Fragment" is chopped off, and ensured the URL ends in a single "/", and that's it. */ |
| 94 |
|
| 95 |
h = skip_protocol_and_hostname(url); |
| 96 |
if (!h) |
| 97 |
return -EINVAL; |
| 98 |
|
| 99 |
e = h + strcspn(h, "?#"); /* Cut off "Query" and "Fragment" */ |
| 100 |
|
| 101 |
while (e > h && e[-1] == '/') /* Eat trailing slashes */ |
| 102 |
e--; |
| 103 |
|
| 104 |
/* Drop the specified number of components from the end. Note that this is pretty lenient: if there |
| 105 |
* are less component we silently drop those and then append the suffix to the top. */ |
| 106 |
while (n_drop_components > 0) { |
| 107 |
while (e > h && e[-1] != '/') /* Eat last word (we don't mind if empty) */ |
| 108 |
e--; |
| 109 |
|
| 110 |
while (e > h && e[-1] == '/') /* Eat slashes before the last word */ |
| 111 |
e--; |
| 112 |
|
| 113 |
n_drop_components--; |
| 114 |
} |
| 115 |
|
| 116 |
s = new(char, (e - url) + 1 + strlen_ptr(suffix) + 1); |
| 117 |
if (!s) |
| 118 |
return -ENOMEM; |
| 119 |
|
| 120 |
strcpy(stpcpy(mempcpy(s, url, e - url), "/"), strempty(suffix)); |
| 121 |
*ret = s; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static const char* const import_type_table[_IMPORT_TYPE_MAX] = { |
| 126 |
[IMPORT_RAW] = "raw", |
| 127 |
[IMPORT_TAR] = "tar", |
| 128 |
[IMPORT_OCI] = "oci", |
| 129 |
}; |
| 130 |
|
| 131 |
DEFINE_STRING_TABLE_LOOKUP(import_type, ImportType); |
| 132 |
|
| 133 |
static const char* const import_verify_table[_IMPORT_VERIFY_MAX] = { |
| 134 |
[IMPORT_VERIFY_NO] = "no", |
| 135 |
[IMPORT_VERIFY_CHECKSUM] = "checksum", |
| 136 |
[IMPORT_VERIFY_SIGNATURE] = "signature", |
| 137 |
}; |
| 138 |
|
| 139 |
DEFINE_STRING_TABLE_LOOKUP(import_verify, ImportVerify); |
| 140 |
|
| 141 |
int tar_strip_suffixes(const char *name, char **ret) { |
| 142 |
const char *e; |
| 143 |
char *s; |
| 144 |
|
| 145 |
assert(ret); |
| 146 |
|
| 147 |
e = endswith(name, ".tar"); |
| 148 |
if (!e) |
| 149 |
e = endswith(name, ".tar.xz"); |
| 150 |
if (!e) |
| 151 |
e = endswith(name, ".tar.gz"); |
| 152 |
if (!e) |
| 153 |
e = endswith(name, ".tar.bz2"); |
| 154 |
if (!e) |
| 155 |
e = endswith(name, ".tar.zst"); |
| 156 |
if (!e) |
| 157 |
e = endswith(name, ".tgz"); |
| 158 |
if (!e) |
| 159 |
e = strchr(name, 0); |
| 160 |
|
| 161 |
if (e <= name) |
| 162 |
return -EINVAL; |
| 163 |
|
| 164 |
s = strndup(name, e - name); |
| 165 |
if (!s) |
| 166 |
return -ENOMEM; |
| 167 |
|
| 168 |
*ret = s; |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
int raw_strip_suffixes(const char *name, char **ret) { |
| 173 |
|
| 174 |
static const char suffixes[] = |
| 175 |
".xz\0" |
| 176 |
".gz\0" |
| 177 |
".bz2\0" |
| 178 |
".zst\0" |
| 179 |
".sysext.raw\0" |
| 180 |
".confext.raw\0" |
| 181 |
".raw\0" |
| 182 |
".qcow2\0" |
| 183 |
".img\0" |
| 184 |
".bin\0"; |
| 185 |
|
| 186 |
_cleanup_free_ char *q = NULL; |
| 187 |
|
| 188 |
assert(ret); |
| 189 |
|
| 190 |
q = strdup(name); |
| 191 |
if (!q) |
| 192 |
return -ENOMEM; |
| 193 |
|
| 194 |
for (;;) { |
| 195 |
bool changed = false; |
| 196 |
|
| 197 |
NULSTR_FOREACH(sfx, suffixes) { |
| 198 |
char *e; |
| 199 |
|
| 200 |
e = endswith(q, sfx); |
| 201 |
if (e) { |
| 202 |
*e = 0; |
| 203 |
changed = true; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if (!changed) |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
*ret = TAKE_PTR(q); |
| 212 |
|
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
int import_assign_pool_quota_and_warn(const char *path) { |
| 217 |
int r; |
| 218 |
|
| 219 |
assert(path); |
| 220 |
|
| 221 |
r = btrfs_subvol_auto_qgroup(path, 0, true); |
| 222 |
if (r == -ENOTTY) { |
| 223 |
log_debug_errno(r, "Failed to set up quota hierarchy for %s, as directory is not on btrfs or not a subvolume. Ignoring.", path); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
if (r < 0) |
| 227 |
return log_error_errno(r, "Failed to set up default quota hierarchy for %s: %m", path); |
| 228 |
if (r > 0) |
| 229 |
log_debug("Set up default quota hierarchy for %s.", path); |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
int import_set_nocow_and_log(int fd, const char *path) { |
| 235 |
int r; |
| 236 |
|
| 237 |
r = chattr_fd(fd, FS_NOCOW_FL, FS_NOCOW_FL); |
| 238 |
if (r < 0) |
| 239 |
return log_full_errno( |
| 240 |
ERRNO_IS_IOCTL_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, |
| 241 |
r, "Failed to set file attributes on %s: %m", path); |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |