#include "bus-label.h"
#include "bus-common-errors.h"
#include "strv.h"
+#include "bus-util.h"
#include "machine-image.h"
#include "image-dbus.h"
-static int image_find_by_bus_path(const char *path, Image **ret) {
- _cleanup_free_ char *e = NULL;
- const char *p;
-
- assert(path);
-
- p = startswith(path, "/org/freedesktop/machine1/image/");
- if (!p)
- return 0;
-
- e = bus_label_unescape(p);
- if (!e)
- return -ENOMEM;
+static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, image_type, ImageType);
- return image_find(e, ret);
-}
-
-static int image_find_by_bus_path_with_error(const char *path, Image **ret, sd_bus_error *error) {
- int r;
-
- assert(path);
-
- r = image_find_by_bus_path(path, ret);
- if (r == 0)
- return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "Image doesn't exist.");
-
- return r;
-}
-
-static int property_get_name(
+int bus_image_method_remove(
sd_bus *bus,
- const char *path,
- const char *interface,
- const char *property,
- sd_bus_message *reply,
+ sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
- _cleanup_(image_unrefp) Image *image = NULL;
+ Image *image = userdata;
int r;
assert(bus);
- assert(reply);
-
- r = image_find_by_bus_path_with_error(path, &image, error);
- if (r < 0)
- return r;
+ assert(message);
+ assert(image);
- r = sd_bus_message_append(reply, "s", image->name);
+ r = image_remove(image);
if (r < 0)
return r;
- return 1;
+ return sd_bus_reply_method_return(message, NULL);
}
-static int property_get_path(
+int bus_image_method_rename(
sd_bus *bus,
- const char *path,
- const char *interface,
- const char *property,
- sd_bus_message *reply,
+ sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
- _cleanup_(image_unrefp) Image *image = NULL;
+ Image *image = userdata;
+ const char *new_name;
int r;
assert(bus);
- assert(reply);
+ assert(message);
+ assert(image);
- r = image_find_by_bus_path_with_error(path, &image, error);
+ r = sd_bus_message_read(message, "s", &new_name);
if (r < 0)
return r;
- r = sd_bus_message_append(reply, "s", image->path);
+ if (!image_name_is_valid(new_name))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", new_name);
+
+ r = image_rename(image, new_name);
if (r < 0)
return r;
- return 1;
+ return sd_bus_reply_method_return(message, NULL);
}
-static int property_get_type(
+int bus_image_method_clone(
sd_bus *bus,
- const char *path,
- const char *interface,
- const char *property,
- sd_bus_message *reply,
+ sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
-
- _cleanup_(image_unrefp) Image *image = NULL;
- int r;
+ Image *image = userdata;
+ const char *new_name;
+ int r, read_only;
assert(bus);
- assert(reply);
+ assert(message);
+ assert(image);
- r = image_find_by_bus_path_with_error(path, &image, error);
+ r = sd_bus_message_read(message, "sb", &new_name, &read_only);
if (r < 0)
return r;
- r = sd_bus_message_append(reply, "s", image_type_to_string(image->type));
+ if (!image_name_is_valid(new_name))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", new_name);
+
+ r = image_clone(image, new_name, read_only);
if (r < 0)
return r;
- return 1;
+ return sd_bus_reply_method_return(message, NULL);
}
-static int property_get_read_only(
+int bus_image_method_mark_read_only(
sd_bus *bus,
- const char *path,
- const char *interface,
- const char *property,
- sd_bus_message *reply,
+ sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
-
- _cleanup_(image_unrefp) Image *image = NULL;
- int r;
+ Image *image = userdata;
+ int r, read_only;
assert(bus);
- assert(reply);
+ assert(message);
- r = image_find_by_bus_path_with_error(path, &image, error);
+ r = sd_bus_message_read(message, "b", &read_only);
if (r < 0)
return r;
- r = sd_bus_message_append(reply, "b", image->read_only);
+ r = image_read_only(image, read_only);
if (r < 0)
return r;
- return 1;
+ return sd_bus_reply_method_return(message, NULL);
}
-static int property_get_crtime(
- sd_bus *bus,
- const char *path,
- const char *interface,
- const char *property,
- sd_bus_message *reply,
- void *userdata,
- sd_bus_error *error) {
-
+const sd_bus_vtable image_vtable[] = {
+ SD_BUS_VTABLE_START(0),
+ SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Image, name), 0),
+ SD_BUS_PROPERTY("Path", "s", NULL, offsetof(Image, path), 0),
+ SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Image, type), 0),
+ SD_BUS_PROPERTY("ReadOnly", "b", bus_property_get_bool, offsetof(Image, read_only), 0),
+ SD_BUS_PROPERTY("CreationTimestamp", "t", NULL, offsetof(Image, crtime), 0),
+ SD_BUS_PROPERTY("ModificationTimestamp", "t", NULL, offsetof(Image, mtime), 0),
+ SD_BUS_PROPERTY("Usage", "t", NULL, offsetof(Image, usage), 0),
+ SD_BUS_PROPERTY("Limit", "t", NULL, offsetof(Image, limit), 0),
+ SD_BUS_PROPERTY("UsageExclusive", "t", NULL, offsetof(Image, usage_exclusive), 0),
+ SD_BUS_PROPERTY("LimitExclusive", "t", NULL, offsetof(Image, limit_exclusive), 0),
+ SD_BUS_METHOD("Remove", NULL, NULL, bus_image_method_remove, 0),
+ SD_BUS_METHOD("Rename", "s", NULL, bus_image_method_rename, 0),
+ SD_BUS_METHOD("Clone", "sb", NULL, bus_image_method_clone, 0),
+ SD_BUS_METHOD("MarkReadOnly", "b", NULL, bus_image_method_mark_read_only, 0),
+ SD_BUS_VTABLE_END
+};
- _cleanup_(image_unrefp) Image *image = NULL;
- int r;
+static int image_flush_cache(sd_event_source *s, void *userdata) {
+ Manager *m = userdata;
+ Image *i;
- assert(bus);
- assert(reply);
+ assert(s);
+ assert(m);
- r = image_find_by_bus_path_with_error(path, &image, error);
- if (r < 0)
- return r;
+ while ((i = hashmap_steal_first(m->image_cache)))
+ image_unref(i);
- r = sd_bus_message_append(reply, "t", image->crtime);
- if (r < 0)
- return r;
-
- return 1;
+ return 0;
}
-static int property_get_mtime(
- sd_bus *bus,
- const char *path,
- const char *interface,
- const char *property,
- sd_bus_message *reply,
- void *userdata,
- sd_bus_error *error) {
-
-
- _cleanup_(image_unrefp) Image *image = NULL;
+int image_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
+ _cleanup_free_ char *e = NULL;
+ Manager *m = userdata;
+ Image *image = NULL;
+ const char *p;
int r;
assert(bus);
- assert(reply);
+ assert(path);
+ assert(interface);
+ assert(found);
- r = image_find_by_bus_path_with_error(path, &image, error);
- if (r < 0)
- return r;
+ p = startswith(path, "/org/freedesktop/machine1/image/");
+ if (!p)
+ return 0;
+
+ e = bus_label_unescape(p);
+ if (!e)
+ return -ENOMEM;
+
+ image = hashmap_get(m->image_cache, e);
+ if (image) {
+ *found = image;
+ return 1;
+ }
- r = sd_bus_message_append(reply, "t", image->mtime);
+ r = hashmap_ensure_allocated(&m->image_cache, &string_hash_ops);
if (r < 0)
return r;
- return 1;
-}
-
-const sd_bus_vtable image_vtable[] = {
- SD_BUS_VTABLE_START(0),
- SD_BUS_PROPERTY("Name", "s", property_get_name, 0, 0),
- SD_BUS_PROPERTY("Path", "s", property_get_path, 0, 0),
- SD_BUS_PROPERTY("Type", "s", property_get_type, 0, 0),
- SD_BUS_PROPERTY("ReadOnly", "b", property_get_read_only, 0, 0),
- SD_BUS_PROPERTY("CreationTimestamp", "t", property_get_crtime, 0, 0),
- SD_BUS_PROPERTY("ModificationTimestamp", "t", property_get_mtime, 0, 0),
- SD_BUS_VTABLE_END
-};
+ if (!m->image_cache_defer_event) {
+ r = sd_event_add_defer(m->event, &m->image_cache_defer_event, image_flush_cache, m);
+ if (r < 0)
+ return r;
-int image_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
- int r;
+ r = sd_event_source_set_priority(m->image_cache_defer_event, SD_EVENT_PRIORITY_IDLE);
+ if (r < 0)
+ return r;
+ }
- assert(bus);
- assert(path);
- assert(interface);
- assert(found);
+ r = sd_event_source_set_enabled(m->image_cache_defer_event, SD_EVENT_ONESHOT);
+ if (r < 0)
+ return r;
- r = image_find_by_bus_path(path, NULL);
+ r = image_find(e, &image);
if (r <= 0)
return r;
- *found = NULL;
+ r = hashmap_put(m->image_cache, image->name, image);
+ if (r < 0) {
+ image_unref(image);
+ return r;
+ }
+
+ *found = image;
return 1;
}