core/path: implement transient path unit
This commit is contained in:
@@ -18,9 +18,12 @@
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "dbus-path.h"
|
||||
#include "list.h"
|
||||
#include "path.h"
|
||||
#include "path-util.h"
|
||||
#include "string-util.h"
|
||||
#include "unit.h"
|
||||
|
||||
@@ -85,3 +88,119 @@ const sd_bus_vtable bus_path_vtable[] = {
|
||||
SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Path, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
|
||||
SD_BUS_VTABLE_END
|
||||
};
|
||||
|
||||
static int bus_path_set_transient_property(
|
||||
Path *p,
|
||||
const char *name,
|
||||
sd_bus_message *message,
|
||||
UnitWriteFlags flags,
|
||||
sd_bus_error *error) {
|
||||
|
||||
Unit *u = UNIT(p);
|
||||
int r;
|
||||
|
||||
assert(p);
|
||||
assert(name);
|
||||
assert(message);
|
||||
|
||||
flags |= UNIT_PRIVATE;
|
||||
|
||||
if (STR_IN_SET(name, "PathExists", "PathExistsGlob", "PathChanged", "PathModified", "DirectoryNotEmpty")) {
|
||||
const char *str;
|
||||
PathType b;
|
||||
|
||||
b = path_type_from_string(name);
|
||||
if (b < 0)
|
||||
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type");
|
||||
|
||||
r = sd_bus_message_read(message, "s", &str);
|
||||
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");
|
||||
|
||||
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
|
||||
if (isempty(str)) {
|
||||
path_free_specs(p);
|
||||
unit_write_settingf(u, flags, name, "%s=", name);
|
||||
} else {
|
||||
_cleanup_free_ char *k;
|
||||
PathSpec *s;
|
||||
|
||||
k = strdup(str);
|
||||
if (!k)
|
||||
return -ENOMEM;
|
||||
|
||||
s = new0(PathSpec, 1);
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
s->unit = u;
|
||||
s->path = path_kill_slashes(k);
|
||||
k = NULL;
|
||||
s->type = b;
|
||||
s->inotify_fd = -1;
|
||||
|
||||
LIST_PREPEND(spec, p->specs, s);
|
||||
|
||||
unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, str);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
} else if (streq(name, "Unit")) {
|
||||
/* not implemented yet */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bus_path_set_property(
|
||||
Unit *u,
|
||||
const char *name,
|
||||
sd_bus_message *message,
|
||||
UnitWriteFlags mode,
|
||||
sd_bus_error *error) {
|
||||
|
||||
Path *p = PATH(u);
|
||||
|
||||
assert(p);
|
||||
assert(name);
|
||||
assert(message);
|
||||
|
||||
if (u->transient && u->load_state == UNIT_STUB)
|
||||
return bus_path_set_transient_property(p, name, message, mode, error);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include "sd-bus.h"
|
||||
|
||||
#include "unit.h"
|
||||
|
||||
extern const sd_bus_vtable bus_path_vtable[];
|
||||
|
||||
int bus_path_set_property(Unit *u, const char *name, sd_bus_message *i, UnitWriteFlags flags, sd_bus_error *error);
|
||||
|
||||
+5
-1
@@ -772,6 +772,9 @@ const UnitVTable path_vtable = {
|
||||
"Unit\0"
|
||||
"Path\0"
|
||||
"Install\0",
|
||||
.private_section = "Path",
|
||||
|
||||
.can_transient = true,
|
||||
|
||||
.init = path_init,
|
||||
.done = path_done,
|
||||
@@ -794,5 +797,6 @@ const UnitVTable path_vtable = {
|
||||
|
||||
.reset_failed = path_reset_failed,
|
||||
|
||||
.bus_vtable = bus_path_vtable
|
||||
.bus_vtable = bus_path_vtable,
|
||||
.bus_set_property = bus_path_set_property,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user