1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "bus-label.h"
23 #include "bus-common-errors.h"
27 static int image_find_by_bus_path(const char *path, Image **ret) {
28 _cleanup_free_ char *e = NULL;
33 p = startswith(path, "/org/freedesktop/machine1/image/");
37 e = bus_label_unescape(p);
41 return image_find(e, ret);
44 static int image_find_by_bus_path_with_error(const char *path, Image **ret, sd_bus_error *error) {
49 r = image_find_by_bus_path(path, ret);
51 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "Image doesn't exist.");
56 static int property_get_name(
59 const char *interface,
61 sd_bus_message *reply,
63 sd_bus_error *error) {
65 _cleanup_(image_unrefp) Image *image = NULL;
71 r = image_find_by_bus_path_with_error(path, &image, error);
75 r = sd_bus_message_append(reply, "s", image->name);
82 static int property_get_path(
85 const char *interface,
87 sd_bus_message *reply,
89 sd_bus_error *error) {
91 _cleanup_(image_unrefp) Image *image = NULL;
97 r = image_find_by_bus_path_with_error(path, &image, error);
101 r = sd_bus_message_append(reply, "s", image->path);
108 static int property_get_type(
111 const char *interface,
112 const char *property,
113 sd_bus_message *reply,
115 sd_bus_error *error) {
118 _cleanup_(image_unrefp) Image *image = NULL;
124 r = image_find_by_bus_path_with_error(path, &image, error);
128 r = sd_bus_message_append(reply, "s", image_type_to_string(image->type));
135 static int property_get_read_only(
138 const char *interface,
139 const char *property,
140 sd_bus_message *reply,
142 sd_bus_error *error) {
145 _cleanup_(image_unrefp) Image *image = NULL;
151 r = image_find_by_bus_path_with_error(path, &image, error);
155 r = sd_bus_message_append(reply, "b", image->read_only);
162 const sd_bus_vtable image_vtable[] = {
163 SD_BUS_VTABLE_START(0),
164 SD_BUS_PROPERTY("Name", "s", property_get_name, 0, 0),
165 SD_BUS_PROPERTY("Path", "s", property_get_path, 0, 0),
166 SD_BUS_PROPERTY("Type", "s", property_get_type, 0, 0),
167 SD_BUS_PROPERTY("ReadOnly", "b", property_get_read_only, 0, 0),
171 int image_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
179 r = image_find_by_bus_path(path, NULL);
187 char *image_bus_path(const char *name) {
188 _cleanup_free_ char *e = NULL;
192 e = bus_label_escape(name);
196 return strappend("/org/freedesktop/machine1/image/", e);
199 int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
200 _cleanup_(image_hashmap_freep) Hashmap *images = NULL;
201 _cleanup_strv_free_ char **l = NULL;
210 images = hashmap_new(&string_hash_ops);
214 r = image_discover(images);
218 HASHMAP_FOREACH(image, images, i) {
221 p = image_bus_path(image->name);
225 r = strv_consume(&l, p);