From 6759e7a7638fc98877f98a7d45b265461ea78674 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 2 Jul 2010 03:51:05 +0200 Subject: [PATCH] systemctl: implement delete command --- man/systemctl.xml | 7 ++++ src/systemctl.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/man/systemctl.xml b/man/systemctl.xml index 80f35549..ad5ca468 100644 --- a/man/systemctl.xml +++ b/man/systemctl.xml @@ -331,6 +331,13 @@ state. Snapshots are dynamic and lost on reboot. + + delete [NAME...] + + Remove a snapshot + previously created with + snapshot. + daemon-reload diff --git a/src/systemctl.c b/src/systemctl.c index 390cec9f..84454305 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -1363,6 +1363,89 @@ finish: return r; } +static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) { + DBusMessage *m = NULL, *reply = NULL; + int r; + DBusError error; + unsigned i; + + assert(bus); + assert(args); + + dbus_error_init(&error); + + for (i = 1; i < n; i++) { + const char *path = NULL; + + if (!(m = dbus_message_new_method_call( + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + "GetUnit"))) { + log_error("Could not allocate message."); + r = -ENOMEM; + goto finish; + } + + if (!dbus_message_append_args(m, + DBUS_TYPE_STRING, &args[i], + DBUS_TYPE_INVALID)) { + log_error("Could not append arguments to message."); + r = -ENOMEM; + goto finish; + } + + if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { + log_error("Failed to issue method call: %s", error.message); + r = -EIO; + goto finish; + } + + if (!dbus_message_get_args(reply, &error, + DBUS_TYPE_OBJECT_PATH, &path, + DBUS_TYPE_INVALID)) { + log_error("Failed to parse reply: %s", error.message); + r = -EIO; + goto finish; + } + + dbus_message_unref(m); + if (!(m = dbus_message_new_method_call( + "org.freedesktop.systemd1", + path, + "org.freedesktop.systemd1.Snapshot", + "Remove"))) { + log_error("Could not allocate message."); + r = -ENOMEM; + goto finish; + } + + dbus_message_unref(reply); + if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { + log_error("Failed to issue method call: %s", error.message); + r = -EIO; + goto finish; + } + + dbus_message_unref(m); + dbus_message_unref(reply); + m = reply = NULL; + } + + r = 0; + +finish: + if (m) + dbus_message_unref(m); + + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return r; +} + static int clear_jobs(DBusConnection *bus, char **args, unsigned n) { DBusMessage *m = NULL, *reply = NULL; DBusError error; @@ -1597,6 +1680,7 @@ static int systemctl_help(void) { " monitor Monitor unit/job changes\n" " dump Dump server status\n" " snapshot [NAME] Create a snapshot\n" + " delete [NAME...] Remove one or more snapshots\n" " daemon-reload Reload systemd manager configuration\n" " daemon-reexec Reexecute systemd manager\n" " daemon-exit Ask the systemd manager to quit\n" @@ -2262,6 +2346,7 @@ static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) { { "monitor", EQUAL, 1, monitor }, { "dump", EQUAL, 1, dump }, { "snapshot", LESS, 2, snapshot }, + { "delete", MORE, 2, delete_snapshot }, { "daemon-reload", EQUAL, 1, clear_jobs }, { "daemon-reexec", EQUAL, 1, clear_jobs }, { "daemon-exit", EQUAL, 1, clear_jobs }, -- 2.39.5