Debian Codegraph @

src/portable/portabled-image.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-event.h"
4
5 #include "discover-image.h"
6 #include "hashmap.h"
7 #include "portabled.h"
8 #include "portabled-image.h"
9
10 Image *manager_image_cache_get(Manager *m, const char *name_or_path) {
11 assert(m);
12
13 return hashmap_get(m->image_cache, name_or_path);
14 }
15
16 static int image_cache_flush(sd_event_source *s, void *userdata) {
17 Manager *m = ASSERT_PTR(userdata);
18
19 assert(s);
20
21 hashmap_clear(m->image_cache);
22 return 0;
23 }
24
25 static int manager_image_cache_initialize(Manager *m) {
26 int r;
27
28 assert(m);
29
30 r = hashmap_ensure_allocated(&m->image_cache, &image_hash_ops);
31 if (r < 0)
32 return r;
33
34 /* We flush the cache as soon as we are idle again */
35 if (!m->image_cache_defer_event) {
36 r = sd_event_add_defer(m->event, &m->image_cache_defer_event, image_cache_flush, m);
37 if (r < 0)
38 return r;
39
40 r = sd_event_source_set_priority(m->image_cache_defer_event, SD_EVENT_PRIORITY_IDLE);
41 if (r < 0)
42 return r;
43 }
44
45 r = sd_event_source_set_enabled(m->image_cache_defer_event, SD_EVENT_ONESHOT);
46 if (r < 0)
47 return r;
48
49 return 0;
50 }
51
52 int manager_image_cache_add(Manager *m, Image *image) {
53 int r;
54
55 assert(m);
56
57 /* We add the specified image to the cache under two keys.
58 *
59 * 1. Always under its path
60 *
61 * 2. If the image was discovered in the search path (i.e. its discoverable boolean set) we'll also add it
62 * under its short name.
63 */
64
65 r = manager_image_cache_initialize(m);
66 if (r < 0)
67 return r;
68
69 image->userdata = m;
70
71 r = hashmap_put(m->image_cache, image->path, image);
72 if (r < 0)
73 return r;
74
75 image_ref(image);
76
77 if (image->discoverable) {
78 r = hashmap_put(m->image_cache, image->name, image);
79 if (r < 0)
80 return r;
81
82 image_ref(image);
83 }
84
85 return 0;
86 }
87
88 int manager_image_cache_discover(Manager *m, Hashmap **ret_images, sd_bus_error *error) {
89 int r;
90
91 assert(m);
92
93 /* A wrapper around image_discover() (for finding images in search path) and portable_discover_attached() (for
94 * finding attached images). */
95
96 _cleanup_hashmap_free_ Hashmap *images = NULL;
97 r = image_discover(m->runtime_scope, IMAGE_PORTABLE, NULL, &images);
98 if (r < 0)
99 return r;
100
101 Image *image;
102 HASHMAP_FOREACH(image, images)
103 (void) manager_image_cache_add(m, image);
104
105 if (ret_images)
106 *ret_images = TAKE_PTR(images);
107
108 return 0;
109 }