importd: clean up how we determine image root in importd backends

Let's introduce a single helper that determines where to download images
to, taking all three primary parameters into account: the image class,
the runtime scope and whether to do runtime or persistency.

Then port everything over to this.

This not only cleans things up, but makes sure the importd backends
actually properly can deal with per-user downloads, as before we never
took the runtime scope into account for determining download location.
This commit is contained in:
Lennart Poettering
2025-10-21 23:09:50 +02:00
parent 0960b905bc
commit d9c10bf1d2
7 changed files with 96 additions and 20 deletions
+44 -2
View File
@@ -110,7 +110,7 @@ static const char *const image_root_table[_IMAGE_CLASS_MAX] = {
[IMAGE_CONFEXT] = "/var/lib/confexts",
};
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(image_root, ImageClass);
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(image_root, ImageClass);
static const char *const image_root_runtime_table[_IMAGE_CLASS_MAX] = {
[IMAGE_MACHINE] = "/run/machines",
@@ -119,7 +119,7 @@ static const char *const image_root_runtime_table[_IMAGE_CLASS_MAX] = {
[IMAGE_CONFEXT] = "/run/confexts",
};
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(image_root_runtime, ImageClass);
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(image_root_runtime, ImageClass);
static const char *const image_dirname_table[_IMAGE_CLASS_MAX] = {
[IMAGE_MACHINE] = "machines",
@@ -2022,3 +2022,45 @@ static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
};
DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);
int image_root_pick(
RuntimeScope scope,
ImageClass c,
bool runtime,
char **ret) {
int r;
assert(scope >= 0);
assert(scope < _RUNTIME_SCOPE_MAX);
assert(c >= 0);
assert(c < _IMAGE_CLASS_MAX);
assert(ret);
/* Picks the primary target directory for downloads, depending on invocation contexts */
_cleanup_free_ char *s = NULL;
switch (scope) {
case RUNTIME_SCOPE_SYSTEM: {
s = strdup(runtime ? image_root_runtime_to_string(c) : image_root_to_string(c));
if (!s)
return -ENOMEM;
break;
}
case RUNTIME_SCOPE_USER:
r = sd_path_lookup(runtime ? SD_PATH_USER_RUNTIME : SD_PATH_USER_STATE_PRIVATE, "machines", &s);
if (r < 0)
return r;
break;
default:
return -EOPNOTSUPP;
}
*ret = TAKE_PTR(s);
return 0;
}
+1 -2
View File
@@ -95,8 +95,7 @@ bool image_is_host(const struct Image *i);
int image_to_json(const struct Image *i, sd_json_variant **ret);
const char* image_root_to_string(ImageClass c) _const_;
const char* image_root_runtime_to_string(ImageClass c) _const_;
int image_root_pick(RuntimeScope scope, ImageClass c, bool runtime, char **ret);
extern const struct hash_ops image_hash_ops;