From f76e92af1c0d29124c7f219f142bed2860edaac8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 2 Jan 2018 02:25:57 +0900 Subject: [PATCH] dbus-path: add Paths= option to set path specs in transient path unit --- src/core/dbus-path.c | 74 +++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/src/core/dbus-path.c b/src/core/dbus-path.c index d9cf212eb7a..b3f502f4c98 100644 --- a/src/core/dbus-path.c +++ b/src/core/dbus-path.c @@ -21,6 +21,7 @@ #include "alloc-util.h" #include "bus-util.h" #include "dbus-path.h" +#include "dbus-util.h" #include "list.h" #include "path.h" #include "path-util.h" @@ -105,30 +106,38 @@ static int bus_path_set_transient_property( flags |= UNIT_PRIVATE; - if (STR_IN_SET(name, "PathExists", "PathExistsGlob", "PathChanged", "PathModified", "DirectoryNotEmpty")) { - const char *str; - PathType b; + if (streq(name, "MakeDirectory")) + return bus_set_transient_bool(u, name, &p->make_directory, message, flags, error); - b = path_type_from_string(name); - if (b < 0) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type"); + if (streq(name, "DirectoryMode")) + return bus_set_transient_mode_t(u, name, &p->directory_mode, message, flags, error); - r = sd_bus_message_read(message, "s", &str); + if (streq(name, "Paths")) { + const char *type_name, *path; + bool empty = true; + + r = sd_bus_message_enter_container(message, 'a', "(ss)"); if (r < 0) return r; - if (!isempty(str) && !path_is_absolute(str)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path is not absolute"); + while ((r = sd_bus_message_read(message, "(ss)", &type_name, &path)) > 0) { + PathType t; - if (!UNIT_WRITE_FLAGS_NOOP(flags)) { - if (isempty(str)) { - path_free_specs(p); - unit_write_settingf(u, flags, name, "%s=", name); - } else { + t = path_type_from_string(type_name); + if (t < 0) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type: %s", type_name); + + if (isempty(path)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in %s is empty", type_name); + + if (!path_is_absolute(path)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in %s is not absolute: %s", type_name, path); + + if (!UNIT_WRITE_FLAGS_NOOP(flags)) { _cleanup_free_ char *k; PathSpec *s; - k = strdup(str); + k = strdup(path); if (!k) return -ENOMEM; @@ -139,48 +148,29 @@ static int bus_path_set_transient_property( s->unit = u; s->path = path_kill_slashes(k); k = NULL; - s->type = b; + s->type = t; s->inotify_fd = -1; LIST_PREPEND(spec, p->specs, s); - unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, str); + unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", type_name, path); } + + empty = false; } - - return 1; - - } else if (streq(name, "MakeDirectory")) { - int b; - - r = sd_bus_message_read(message, "b", &b); if (r < 0) return r; - if (!UNIT_WRITE_FLAGS_NOOP(flags)) { - p->make_directory = b; - unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(b)); - } - - return 1; - - } else if (streq(name, "DirectoryMode")) { - mode_t m; - - r = sd_bus_message_read(message, "u", &m); + r = sd_bus_message_exit_container(message); if (r < 0) return r; - if (!UNIT_WRITE_FLAGS_NOOP(flags)) { - p->directory_mode = m; - unit_write_settingf(u, flags, name, "%s=%040o", name, m); + if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) { + path_free_specs(p); + unit_write_settingf(u, flags, name, "PathExists="); } return 1; - - } else if (streq(name, "Unit")) { - /* not implemented yet */ - return 0; } return 0;