1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
6 #include "alloc-util.h"
12 #include "path-util.h"
13 #include "string-util.h"
16 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result
, path_result
, PathResult
);
18 static int property_get_paths(
21 const char *interface
,
23 sd_bus_message
*reply
,
25 sd_bus_error
*error
) {
35 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
39 LIST_FOREACH(spec
, k
, p
->specs
) {
40 r
= sd_bus_message_append(reply
, "(ss)", path_type_to_string(k
->type
), k
->path
);
45 return sd_bus_message_close_container(reply
);
48 const sd_bus_vtable bus_path_vtable
[] = {
49 SD_BUS_VTABLE_START(0),
50 SD_BUS_PROPERTY("Unit", "s", bus_property_get_triggered_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
51 SD_BUS_PROPERTY("Paths", "a(ss)", property_get_paths
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
52 SD_BUS_PROPERTY("MakeDirectory", "b", bus_property_get_bool
, offsetof(Path
, make_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
53 SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode
, offsetof(Path
, directory_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
54 SD_BUS_PROPERTY("Result", "s", property_get_result
, offsetof(Path
, result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
58 static int bus_path_set_transient_property(
61 sd_bus_message
*message
,
63 sd_bus_error
*error
) {
72 flags
|= UNIT_PRIVATE
;
74 if (streq(name
, "MakeDirectory"))
75 return bus_set_transient_bool(u
, name
, &p
->make_directory
, message
, flags
, error
);
77 if (streq(name
, "DirectoryMode"))
78 return bus_set_transient_mode_t(u
, name
, &p
->directory_mode
, message
, flags
, error
);
80 if (streq(name
, "Paths")) {
81 const char *type_name
, *path
;
84 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
88 while ((r
= sd_bus_message_read(message
, "(ss)", &type_name
, &path
)) > 0) {
91 t
= path_type_from_string(type_name
);
93 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown path type: %s", type_name
);
96 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in %s is empty", type_name
);
98 if (!path_is_absolute(path
))
99 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in %s is not absolute: %s", type_name
, path
);
101 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
102 _cleanup_free_
char *k
;
109 path_simplify(k
, false);
111 s
= new0(PathSpec
, 1);
116 s
->path
= TAKE_PTR(k
);
120 LIST_PREPEND(spec
, p
->specs
, s
);
122 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "%s=%s", type_name
, path
);
130 r
= sd_bus_message_exit_container(message
);
134 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
136 unit_write_settingf(u
, flags
, name
, "PathExists=");
145 int bus_path_set_property(
148 sd_bus_message
*message
,
150 sd_bus_error
*error
) {
158 if (u
->transient
&& u
->load_state
== UNIT_STUB
)
159 return bus_path_set_transient_property(p
, name
, message
, mode
, error
);