]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine: generalize rename_image_and_update_cache() logic
authorIvan Kruglov <mail@ikruglov.com>
Tue, 8 Oct 2024 09:30:10 +0000 (11:30 +0200)
committerIvan Kruglov <mail@ikruglov.com>
Mon, 14 Oct 2024 08:42:47 +0000 (10:42 +0200)
This is a prep step to reuse the function in varlink interface

src/machine/image-dbus.c
src/machine/machined-core.c
src/machine/machined.h

index ff9d2f625812fb35bbe00a076525399b2c16be47..da71b9115f434882a2f5c5117470c095e48a13f4 100644 (file)
@@ -127,17 +127,9 @@ int bus_image_method_rename(
         if (r == 0)
                 return 1; /* Will call us back */
 
-        /* The image is cached with its name, hence it is necessary to remove from the cache before renaming. */
-        assert_se(hashmap_remove_value(m->image_cache, image->name, image));
-
-        r = image_rename(image, new_name);
-        if (r < 0) {
-                image_unref(image);
-                return r;
-        }
-
-        /* Then save the object again in the cache. */
-        assert_se(hashmap_put(m->image_cache, image->name, image) > 0);
+        r = rename_image_and_update_cache(m, image, new_name);
+        if (r < 0)
+                return sd_bus_error_set_errnof(error, r, "Failed to rename image: %m");
 
         return sd_bus_reply_method_return(message, NULL);
 }
index e0e4f684e25480eba13340d2e2fa0f37ac268c16..2836b90c6f83b974722ade671eef4baefd7af722 100644 (file)
@@ -456,3 +456,29 @@ int manager_acquire_image(Manager *m, const char *name, Image **ret) {
         TAKE_PTR(image);
         return 0;
 }
+
+int rename_image_and_update_cache(Manager *m, Image *image, const char* new_name) {
+        int r;
+
+        assert(m);
+        assert(image);
+        assert(new_name);
+
+        /* The image is cached with its name, hence it is necessary to remove from the cache before renaming. */
+        assert_se(hashmap_remove_value(m->image_cache, image->name, image));
+
+        r = image_rename(image, new_name);
+        if (r < 0) {
+                image = image_unref(image);
+                return r;
+        }
+
+        /* Then save the object again in the cache. */
+        r = hashmap_put(m->image_cache, image->name, image);
+        if (r < 0) {
+                image = image_unref(image);
+                log_debug_errno(r, "Failed to put renamed image into cache, ignoring: %m");
+        }
+
+        return 0;
+}
index 9cc902f05f383368bab60982302aed35a20411c8..3d1f50269974c84cb1a84e126831931e6cb59d09 100644 (file)
@@ -69,3 +69,4 @@ void manager_enqueue_gc(Manager *m);
 int machine_get_addresses(Machine* machine, struct local_address **ret_addresses);
 int machine_get_os_release(Machine *machine, char ***ret_os_release);
 int manager_acquire_image(Manager *m, const char *name, Image **ret);
+int rename_image_and_update_cache(Manager *m, Image *image, const char* new_name);