[lxc-devel] [lxd/master] mkdir -p on file push

tych0 on Github lxc-bot at linuxcontainers.org
Wed Aug 17 15:41:49 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 377 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160817/1021c0b3/attachment.bin>
-------------- next part --------------
From fa2ecdf1c528f26809ba203cf4041c162ccba001 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Wed, 17 Aug 2016 09:11:48 -0600
Subject: [PATCH] mkdir -p on file push

Closes #2290

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 lxd/nsexec.go        | 12 ++++++++++++
 test/suites/basic.sh |  6 ++++++
 2 files changed, 18 insertions(+)

diff --git a/lxd/nsexec.go b/lxd/nsexec.go
index 30e2d1a..95bfe08 100644
--- a/lxd/nsexec.go
+++ b/lxd/nsexec.go
@@ -162,6 +162,18 @@ int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is
 	if (is_put && stat(container, &st) < 0)
 		exists = 0;
 
+	if (is_put) {
+		char *pos = strrchr(container, '/');
+		if (pos) {
+			*pos = 0;
+			if (mkdir_p(container, 0755) < 0) {
+				error("failed to mkdir -p");
+				goto close_host;
+			}
+			*pos = '/';
+		}
+	}
+
 	umask(0);
 	container_fd = open(container, container_open_flags, 0);
 	if (container_fd < 0) {
diff --git a/test/suites/basic.sh b/test/suites/basic.sh
index e2696f7..1a92050 100644
--- a/test/suites/basic.sh
+++ b/test/suites/basic.sh
@@ -232,6 +232,12 @@ test_basic_usage() {
   lxc list | grep foo | grep RUNNING
   lxc stop foo --force  # stop is hanging
 
+  # check that we can put files in nonexistent directories in stopped
+  # containers
+  lxc file push /etc/hosts foo/mkdir/p/this/dir/hosts
+  lxc file pull foo/mkdir/p/this/dir/hosts "$TEST_DIR"/hosts
+  diff "$TEST_DIR"/hosts /etc/hosts
+
   # cycle it a few times
   lxc start foo
   mac1=$(lxc exec foo cat /sys/class/net/eth0/address)


More information about the lxc-devel mailing list