static int action_discover(void) {
_cleanup_hashmap_free_ Hashmap *images = NULL;
- _cleanup_(table_unrefp) Table *t = NULL;
- Image *img;
int r;
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return log_oom();
-
for (ImageClass cl = 0; cl < _IMAGE_CLASS_MAX; cl++) {
- r = image_discover(arg_runtime_scope, cl, NULL, images);
+ r = image_discover(arg_runtime_scope, cl, NULL, &images);
if (r < 0)
return log_error_errno(r, "Failed to discover images: %m");
}
return 0;
}
- t = table_new("name", "type", "class", "ro", "path", "time", "usage");
+ _cleanup_(table_unrefp) Table *t = table_new("name", "type", "class", "ro", "path", "time", "usage");
if (!t)
return log_oom();
table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
+ Image *img;
HASHMAP_FOREACH(img, images) {
if (!arg_all && startswith(img->name, "."))
class < 0 ? (c < _IMAGE_CLASS_MAX) : (c == class);
c++) {
- _cleanup_hashmap_free_ Hashmap *h = NULL;
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
- h = hashmap_new(&image_hash_ops);
- if (!h)
- return -ENOMEM;
-
- r = image_discover(m->runtime_scope, c, /* root= */ NULL, h);
+ r = image_discover(m->runtime_scope, c, /* root= */ NULL, &images);
if (r < 0) {
if (class >= 0)
return r;
}
Image *i;
- HASHMAP_FOREACH(i, h) {
+ HASHMAP_FOREACH(i, images) {
r = sd_bus_message_append(
reply,
"(ssssbtttttt)",
}
static int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
- _cleanup_hashmap_free_ Hashmap *images = NULL;
- _cleanup_strv_free_ char **l = NULL;
Manager *m = ASSERT_PTR(userdata);
- Image *image;
int r;
assert(bus);
assert(path);
assert(nodes);
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return -ENOMEM;
-
- r = image_discover(m->runtime_scope, IMAGE_MACHINE, NULL, images);
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
+ r = image_discover(m->runtime_scope, IMAGE_MACHINE, NULL, &images);
if (r < 0)
return r;
+ _cleanup_strv_free_ char **l = NULL;
+ Image *image;
HASHMAP_FOREACH(image, images) {
char *p;
if (r < 0)
return log_debug_errno(r, "Failed to fork(): %m");
if (r == 0) {
- _cleanup_hashmap_free_ Hashmap *images = NULL;
- bool success = true;
- Image *image;
-
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
- images = hashmap_new(&image_hash_ops);
- if (!images)
- report_errno_and_exit(errno_pipe_fd[1], ENOMEM);
-
- r = image_discover(manager->runtime_scope, IMAGE_MACHINE, /* root = */ NULL, images);
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
+ r = image_discover(manager->runtime_scope, IMAGE_MACHINE, /* root = */ NULL, &images);
if (r < 0) {
log_debug_errno(r, "Failed to discover images: %m");
report_errno_and_exit(errno_pipe_fd[1], r);
}
+ bool success = true;
ssize_t n = loop_write(result_fd, &success, sizeof(success));
if (n < 0) {
log_debug_errno(n, "Failed to write to tmp file: %m");
report_errno_and_exit(errno_pipe_fd[1], n);
}
+ Image *image;
HASHMAP_FOREACH(image, images) {
/* We can't remove vendor images (i.e. those in /usr) */
if (image_is_vendor(image))
static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
- _cleanup_hashmap_free_ Hashmap *images = NULL;
- _unused_ Manager *m = ASSERT_PTR(userdata);
- Image *image;
+ Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return -ENOMEM;
-
- r = image_discover(m->runtime_scope, IMAGE_MACHINE, NULL, images);
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
+ r = image_discover(m->runtime_scope, IMAGE_MACHINE, NULL, &images);
if (r < 0)
return r;
if (r < 0)
return r;
+ Image *image;
HASHMAP_FOREACH(image, images) {
_cleanup_free_ char *p = NULL;
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE))
return sd_varlink_error(link, SD_VARLINK_ERROR_EXPECTED_MORE, NULL);
- _cleanup_hashmap_free_ Hashmap *images = hashmap_new(&image_hash_ops);
- if (!images)
- return -ENOMEM;
-
- r = image_discover(m->runtime_scope, IMAGE_MACHINE, /* root = */ NULL, images);
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
+ r = image_discover(m->runtime_scope, IMAGE_MACHINE, /* root = */ NULL, &images);
if (r < 0)
return log_debug_errno(r, "Failed to discover images: %m");
assert(message);
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return -ENOMEM;
-
- r = manager_image_cache_discover(m, images, error);
+ r = manager_image_cache_discover(m, &images, error);
if (r < 0)
return r;
assert(path);
assert(nodes);
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return -ENOMEM;
-
- r = manager_image_cache_discover(m, images, error);
+ r = manager_image_cache_discover(m, &images, error);
if (r < 0)
return r;
return 0;
}
-int manager_image_cache_discover(Manager *m, Hashmap *images, sd_bus_error *error) {
- Image *image;
+int manager_image_cache_discover(Manager *m, Hashmap **ret_images, sd_bus_error *error) {
int r;
assert(m);
/* A wrapper around image_discover() (for finding images in search path) and portable_discover_attached() (for
* finding attached images). */
- r = image_discover(m->runtime_scope, IMAGE_PORTABLE, NULL, images);
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
+ r = image_discover(m->runtime_scope, IMAGE_PORTABLE, NULL, &images);
if (r < 0)
return r;
+ Image *image;
HASHMAP_FOREACH(image, images)
(void) manager_image_cache_add(m, image);
+ if (ret_images)
+ *ret_images = TAKE_PTR(images);
+
return 0;
}
int manager_image_cache_add(Manager *m, Image *image);
-int manager_image_cache_discover(Manager *m, Hashmap *images, sd_bus_error *error);
+int manager_image_cache_discover(Manager *m, Hashmap **ret_images, sd_bus_error *error);
RuntimeScope scope,
ImageClass class,
const char *root,
- Hashmap *h) {
+ Hashmap **images) {
/* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
* symlink block devices into the search path. (For now, we disable that when operating relative to
assert(scope < _RUNTIME_SCOPE_MAX && scope != RUNTIME_SCOPE_GLOBAL);
assert(class >= 0);
assert(class < _IMAGE_CLASS_MAX);
- assert(h);
+ assert(images);
_cleanup_strv_free_ char **search = NULL;
r = pick_image_search_path(scope, class, &search);
continue;
}
- if (hashmap_contains(h, pretty))
+ if (hashmap_contains(*images, pretty))
continue;
r = image_make(class, pretty, dirfd(d), resolved, fname, fd, &st, &image);
image->discoverable = true;
- r = hashmap_put(h, image->name, image);
+ r = hashmap_ensure_put(images, &image_hash_ops, image->name, image);
if (r < 0)
return r;
}
}
- if (scope == RUNTIME_SCOPE_SYSTEM && class == IMAGE_MACHINE && !hashmap_contains(h, ".host")) {
+ if (scope == RUNTIME_SCOPE_SYSTEM && class == IMAGE_MACHINE && !hashmap_contains(*images, ".host")) {
_cleanup_(image_unrefp) Image *image = NULL;
r = image_make(IMAGE_MACHINE,
image->discoverable = true;
- r = hashmap_put(h, image->name, image);
+ r = hashmap_ensure_put(images, &image_hash_ops, image->name, image);
if (r < 0)
return r;
int image_find(RuntimeScope scope, ImageClass class, const char *name, const char *root, Image **ret);
int image_from_path(const char *path, Image **ret);
int image_find_harder(RuntimeScope scope, ImageClass class, const char *name_or_path, const char *root, Image **ret);
-int image_discover(RuntimeScope scope, ImageClass class, const char *root, Hashmap *map);
+int image_discover(RuntimeScope scope, ImageClass class, const char *root, Hashmap **images);
int image_remove(Image *i);
int image_rename(Image *i, const char *new_name, RuntimeScope scope);
return 1;
}
-static int image_discover_and_read_metadata(
- ImageClass image_class,
- Hashmap **ret_images) {
+static int image_discover_and_read_metadata(ImageClass image_class, Hashmap **ret_images) {
_cleanup_hashmap_free_ Hashmap *images = NULL;
Image *img;
int r;
assert(ret_images);
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return log_oom();
-
- r = image_discover(RUNTIME_SCOPE_SYSTEM, image_class, arg_root, images);
+ r = image_discover(RUNTIME_SCOPE_SYSTEM, image_class, arg_root, &images);
if (r < 0)
return log_error_errno(r, "Failed to discover images: %m");
return log_error_errno(r, "Failed to read metadata for image %s: %m", img->name);
}
- *ret_images = TAKE_PTR(images);
+ if (ret_images)
+ *ret_images = TAKE_PTR(images);
return 0;
}
Image *img;
int r;
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return log_oom();
-
- r = image_discover(RUNTIME_SCOPE_SYSTEM, arg_image_class, arg_root, images);
+ r = image_discover(RUNTIME_SCOPE_SYSTEM, arg_image_class, arg_root, &images);
if (r < 0)
return log_error_errno(r, "Failed to discover images: %m");
MethodListParameters p = {
};
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
- _cleanup_hashmap_free_ Hashmap *images = NULL;
- ImageClass image_class = arg_image_class;
- Image *img;
int r;
assert(link);
if (r != 0)
return r;
+ ImageClass image_class = arg_image_class;
r = parse_image_class_parameter(link, p.class, &image_class, NULL);
if (r < 0)
return r;
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return -ENOMEM;
-
- r = image_discover(RUNTIME_SCOPE_SYSTEM, image_class, arg_root, images);
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
+ r = image_discover(RUNTIME_SCOPE_SYSTEM, image_class, arg_root, &images);
if (r < 0)
return r;
+ Image *img;
HASHMAP_FOREACH(img, images) {
if (v) {
/* Send previous item with more=true */
Image *image;
int r;
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return -ENOMEM;
-
- r = image_discover(m->runtime_scope, (ImageClass) class, NULL, images);
+ r = image_discover(m->runtime_scope, (ImageClass) class, NULL, &images);
if (r < 0)
return r;