1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
10 #include "string-util.h"
13 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result
, path_result
, PathResult
);
15 static int property_get_paths(
18 const char *interface
,
20 sd_bus_message
*reply
,
22 sd_bus_error
*error
) {
32 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
36 LIST_FOREACH(spec
, k
, p
->specs
) {
37 r
= sd_bus_message_append(reply
, "(ss)", path_type_to_string(k
->type
), k
->path
);
42 return sd_bus_message_close_container(reply
);
45 const sd_bus_vtable bus_path_vtable
[] = {
46 SD_BUS_VTABLE_START(0),
47 SD_BUS_PROPERTY("Unit", "s", bus_property_get_triggered_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
48 SD_BUS_PROPERTY("Paths", "a(ss)", property_get_paths
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
49 SD_BUS_PROPERTY("MakeDirectory", "b", bus_property_get_bool
, offsetof(Path
, make_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
50 SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode
, offsetof(Path
, directory_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
51 SD_BUS_PROPERTY("Result", "s", property_get_result
, offsetof(Path
, result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
55 static int bus_path_set_transient_property(
58 sd_bus_message
*message
,
60 sd_bus_error
*error
) {
69 flags
|= UNIT_PRIVATE
;
71 if (streq(name
, "MakeDirectory"))
72 return bus_set_transient_bool(u
, name
, &p
->make_directory
, message
, flags
, error
);
74 if (streq(name
, "DirectoryMode"))
75 return bus_set_transient_mode_t(u
, name
, &p
->directory_mode
, message
, flags
, error
);
77 if (streq(name
, "Paths")) {
78 const char *type_name
, *path
;
81 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
85 while ((r
= sd_bus_message_read(message
, "(ss)", &type_name
, &path
)) > 0) {
88 t
= path_type_from_string(type_name
);
90 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown path type: %s", type_name
);
93 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in %s is empty", type_name
);
95 if (!path_is_absolute(path
))
96 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in %s is not absolute: %s", type_name
, path
);
98 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
99 _cleanup_free_
char *k
;
106 path_simplify(k
, false);
108 s
= new0(PathSpec
, 1);
113 s
->path
= TAKE_PTR(k
);
117 LIST_PREPEND(spec
, p
->specs
, s
);
119 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "%s=%s", type_name
, path
);
127 r
= sd_bus_message_exit_container(message
);
131 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
133 unit_write_settingf(u
, flags
, name
, "PathExists=");
142 int bus_path_set_property(
145 sd_bus_message
*message
,
147 sd_bus_error
*error
) {
155 if (u
->transient
&& u
->load_state
== UNIT_STUB
)
156 return bus_path_set_transient_property(p
, name
, message
, mode
, error
);