summaryrefslogtreecommitdiff
path: root/src/machine
diff options
context:
space:
mode:
authorAlexander Wilson <rdtscp@fb.com>2022-07-22 04:08:11 -0700
committerAlexander Wilson <rdtscp@fb.com>2022-07-27 08:41:03 -0700
commitae03e1a97229b192e50439091db92a1b11a8df93 (patch)
tree5c45a048534c10b6c64a97dbe534d0f13f30bdd8 /src/machine
parent922409558e46f855a8fba2c666c056fd95dc2428 (diff)
downloadsystemd-ae03e1a97229b192e50439091db92a1b11a8df93.tar.gz
machinectl: Add plumbing for a `--force` flag for file copy
machine: Add APIs CopyTo[Machine]WithFlags + CopyFrom[Machine]WithFlags - Same API to those without `WithFlags` (except this can take flags) - Initially, only a flag to allow replacing a file if it already exists
Diffstat (limited to 'src/machine')
-rw-r--r--src/machine/machine-dbus.c24
-rw-r--r--src/machine/machine-dbus.h5
-rw-r--r--src/machine/machinectl.c16
-rw-r--r--src/machine/machined-dbus.c10
4 files changed, 54 insertions, 1 deletions
diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c
index 293be0bf8b..7a07d20e0b 100644
--- a/src/machine/machine-dbus.c
+++ b/src/machine/machine-dbus.c
@@ -928,6 +928,20 @@ int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_erro
if (r < 0)
return r;
+ if (endswith(sd_bus_message_get_member(message), "WithFlags")) {
+ uint64_t raw_flags;
+
+ r = sd_bus_message_read(message, "t", &raw_flags);
+ if (r < 0)
+ return r;
+
+ if ((raw_flags & ~_MACHINE_COPY_FLAGS_MASK_PUBLIC) != 0)
+ return -EINVAL;
+
+ if (raw_flags & MACHINE_COPY_REPLACE)
+ copy_flags |= COPY_REPLACE;
+ }
+
if (!path_is_absolute(src))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Source path must be absolute.");
@@ -1328,6 +1342,16 @@ static const sd_bus_vtable machine_vtable[] = {
SD_BUS_NO_RESULT,
bus_machine_method_copy,
SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD_WITH_ARGS("CopyFromWithFlags",
+ SD_BUS_ARGS("s", source, "s", destination, "t", flags),
+ SD_BUS_NO_RESULT,
+ bus_machine_method_copy,
+ SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD_WITH_ARGS("CopyToWithFlags",
+ SD_BUS_ARGS("s", source, "s", destination, "t", flags),
+ SD_BUS_NO_RESULT,
+ bus_machine_method_copy,
+ SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("OpenRootDirectory",
SD_BUS_NO_ARGS,
SD_BUS_RESULT("h", fd),
diff --git a/src/machine/machine-dbus.h b/src/machine/machine-dbus.h
index 1c114f47c3..a0133451ab 100644
--- a/src/machine/machine-dbus.h
+++ b/src/machine/machine-dbus.h
@@ -6,6 +6,11 @@
#include "bus-util.h"
#include "machine.h"
+typedef enum {
+ MACHINE_COPY_REPLACE = 1 << 0, /* Public API via DBUS, do not change */
+ _MACHINE_COPY_FLAGS_MASK_PUBLIC = MACHINE_COPY_REPLACE,
+} MachineCopyFlags;
+
extern const BusObjectImplementation machine_object;
char *machine_bus_path(Machine *s);
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 213879c8ed..ef600e8ee4 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -34,6 +34,7 @@
#include "locale-util.h"
#include "log.h"
#include "logs-show.h"
+#include "machine-dbus.h"
#include "macro.h"
#include "main-func.h"
#include "mkdir.h"
@@ -1093,6 +1094,13 @@ static int terminate_machine(int argc, char *argv[], void *userdata) {
return 0;
}
+static const char *select_copy_method(bool copy_from, bool force) {
+ if (force)
+ return copy_from ? "CopyFromMachineWithFlags" : "CopyToMachineWithFlags";
+ else
+ return copy_from ? "CopyFromMachine" : "CopyToMachine";
+}
+
static int copy_files(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
@@ -1123,7 +1131,7 @@ static int copy_files(int argc, char *argv[], void *userdata) {
bus,
&m,
bus_machine_mgr,
- copy_from ? "CopyFromMachine" : "CopyToMachine");
+ select_copy_method(copy_from, arg_force));
if (r < 0)
return bus_log_create_error(r);
@@ -1136,6 +1144,12 @@ static int copy_files(int argc, char *argv[], void *userdata) {
if (r < 0)
return bus_log_create_error(r);
+ if (arg_force) {
+ r = sd_bus_message_append(m, "t", MACHINE_COPY_REPLACE);
+ if (r < 0)
+ return bus_log_create_error(r);
+ }
+
/* This is a slow operation, hence turn off any method call timeouts */
r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
if (r < 0)
diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c
index 865d8400e8..794e353caa 100644
--- a/src/machine/machined-dbus.c
+++ b/src/machine/machined-dbus.c
@@ -1110,6 +1110,16 @@ const sd_bus_vtable manager_vtable[] = {
SD_BUS_NO_RESULT,
method_copy_machine,
SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD_WITH_ARGS("CopyFromMachineWithFlags",
+ SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
+ SD_BUS_NO_RESULT,
+ method_copy_machine,
+ SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD_WITH_ARGS("CopyToMachineWithFlags",
+ SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
+ SD_BUS_NO_RESULT,
+ method_copy_machine,
+ SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("OpenMachineRootDirectory",
SD_BUS_ARGS("s", name),
SD_BUS_RESULT("h", fd),