[lxc-devel] [lxd/master] Clean up path handling for push

techtonik on Github lxc-bot at linuxcontainers.org
Thu Oct 27 16:29:47 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 433 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161027/52fb453d/attachment.bin>
-------------- next part --------------
From bf28ebf9616ccf775b8cddb135eb415019de23f9 Mon Sep 17 00:00:00 2001
From: anatoly techtonik <techtonik at gmail.com>
Date: Wed, 26 Oct 2016 22:00:02 +0300
Subject: [PATCH 1/5] Parse remote and container first

Signed-off-by: anatoly techtonik <techtonik at gmail.com>
---
 lxc/file.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lxc/file.go b/lxc/file.go
index 82050a6..302b04d 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -74,10 +74,10 @@ func (c *fileCmd) push(config *lxd.Config, send_file_perms bool, args []string)
 		return fmt.Errorf(i18n.G("Invalid target %s"), target)
 	}
 
-	pathSpec[1] = c.normalize(pathSpec[1], target)
+	remote, container := config.ParseRemoteAndContainer(pathSpec[0])
 
+	pathSpec[1] = c.normalize(pathSpec[1], target)
 	targetPath := pathSpec[1]
-	remote, container := config.ParseRemoteAndContainer(pathSpec[0])
 
 	d, err := lxd.NewClient(config, remote)
 	if err != nil {

From 655cf3cbeab68c053a92931f39595a4ebb26ab35 Mon Sep 17 00:00:00 2001
From: anatoly techtonik <techtonik at gmail.com>
Date: Wed, 26 Oct 2016 22:42:53 +0300
Subject: [PATCH 2/5] Explicitly detect if push destination is a directory

Add --debug logging to troubleshoot path conversions

Signed-off-by: anatoly techtonik <techtonik at gmail.com>
---
 lxc/file.go      | 10 ++++++----
 lxc/file_unix.go |  5 -----
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/lxc/file.go b/lxc/file.go
index 302b04d..82676f8 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -76,9 +76,13 @@ func (c *fileCmd) push(config *lxd.Config, send_file_perms bool, args []string)
 
 	remote, container := config.ParseRemoteAndContainer(pathSpec[0])
 
+	targetIsDir := strings.HasSuffix(target, "/")
+
 	pathSpec[1] = c.normalize(pathSpec[1], target)
 	targetPath := pathSpec[1]
 
+	shared.LogDebugf("Pushing to: %s  (isdir: %t)", targetPath, targetIsDir)
+
 	d, err := lxd.NewClient(config, remote)
 	if err != nil {
 		return err
@@ -134,9 +138,7 @@ func (c *fileCmd) push(config *lxd.Config, send_file_perms bool, args []string)
 		gid = c.gid
 	}
 
-	_, targetfilename := filepath.Split(targetPath)
-
-	if (targetfilename != "") && (len(sourcefilenames) > 1) {
+	if (len(sourcefilenames) > 1) && !targetIsDir {
 		return errArgs
 	}
 
@@ -160,7 +162,7 @@ func (c *fileCmd) push(config *lxd.Config, send_file_perms bool, args []string)
 
 	for _, f := range files {
 		fpath := targetPath
-		if targetfilename == "" {
+		if targetIsDir {
 			fpath = path.Join(fpath, path.Base(f.Name()))
 		}
 
diff --git a/lxc/file_unix.go b/lxc/file_unix.go
index ba70c45..2969355 100644
--- a/lxc/file_unix.go
+++ b/lxc/file_unix.go
@@ -25,13 +25,8 @@ func (c *fileCmd) normalize(path string, target string) string {
 	/* Fix up the path. Let's:
 	 * 1. re-add the leading / that got stripped from the SplitN
 	 * 2. clean it and remove any /./, /../, /////, etc.
-	 * 3. keep the trailing slash if it had one, since we use it via
-	 *    filepath.Split below
 	 */
 	path = filepath.Clean("/" + path)
-	if target[len(target)-1] == '/' {
-		path = path + "/"
-	}
 
 	return path
 }

From 0e318af8cae405e7987c31e08785b6a190659302 Mon Sep 17 00:00:00 2001
From: anatoly techtonik <techtonik at gmail.com>
Date: Wed, 26 Oct 2016 23:13:00 +0300
Subject: [PATCH 3/5] Cleanup

Signed-off-by: anatoly techtonik <techtonik at gmail.com>
---
 lxc/file.go         |  7 ++++---
 lxc/file_unix.go    | 10 ----------
 lxc/file_windows.go |  4 ----
 3 files changed, 4 insertions(+), 17 deletions(-)

diff --git a/lxc/file.go b/lxc/file.go
index 82676f8..ee43ebf 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -77,9 +77,10 @@ func (c *fileCmd) push(config *lxd.Config, send_file_perms bool, args []string)
 	remote, container := config.ParseRemoteAndContainer(pathSpec[0])
 
 	targetIsDir := strings.HasSuffix(target, "/")
-
-	pathSpec[1] = c.normalize(pathSpec[1], target)
-	targetPath := pathSpec[1]
+	// re-add leading / that got stripped by the SplitN
+	targetPath := "/" + pathSpec[1]
+	// clean various /./, /../, /////, etc. that users add (#2557)
+	targetPath = path.Clean(targetPath)
 
 	shared.LogDebugf("Pushing to: %s  (isdir: %t)", targetPath, targetIsDir)
 
diff --git a/lxc/file_unix.go b/lxc/file_unix.go
index 2969355..5908a59 100644
--- a/lxc/file_unix.go
+++ b/lxc/file_unix.go
@@ -20,13 +20,3 @@ func (c *fileCmd) getOwner(f *os.File) (os.FileMode, int, int, error) {
 
 	return mode, uid, gid, nil
 }
-
-func (c *fileCmd) normalize(path string, target string) string {
-	/* Fix up the path. Let's:
-	 * 1. re-add the leading / that got stripped from the SplitN
-	 * 2. clean it and remove any /./, /../, /////, etc.
-	 */
-	path = filepath.Clean("/" + path)
-
-	return path
-}
diff --git a/lxc/file_windows.go b/lxc/file_windows.go
index 583a518..c3bf927 100644
--- a/lxc/file_windows.go
+++ b/lxc/file_windows.go
@@ -9,7 +9,3 @@ import (
 func (c *fileCmd) getOwner(f *os.File) (os.FileMode, int, int, error) {
 	return os.FileMode(0), -1, -1, nil
 }
-
-func (c *fileCmd) normalize(path string, target string) string {
-	return path
-}

From 95b3a56c7e7b10b84c94a112d91b3ad6c259b783 Mon Sep 17 00:00:00 2001
From: anatoly techtonik <techtonik at gmail.com>
Date: Wed, 26 Oct 2016 23:29:14 +0300
Subject: [PATCH 4/5] Use targetPath everywhere

Signed-off-by: anatoly techtonik <techtonik at gmail.com>
---
 lxc/file.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lxc/file.go b/lxc/file.go
index ee43ebf..69f3db7 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -115,13 +115,13 @@ func (c *fileCmd) push(config *lxd.Config, send_file_perms bool, args []string)
 		}
 
 		if c.mkdirs {
-			if err := d.MkdirP(container, pathSpec[1], mode); err != nil {
+			if err := d.MkdirP(container, targetPath, mode); err != nil {
 				return err
 			}
 		}
 
 		for _, fname := range sourcefilenames {
-			if err := d.RecursivePushFile(container, fname, pathSpec[1]); err != nil {
+			if err := d.RecursivePushFile(container, fname, targetPath); err != nil {
 				return err
 			}
 		}

From 1a4dca5a92f43da2d99e420c162c45e8654fc72b Mon Sep 17 00:00:00 2001
From: anatoly techtonik <techtonik at gmail.com>
Date: Wed, 26 Oct 2016 23:40:03 +0300
Subject: [PATCH 5/5] Avoid slash convertion in target path on Windows

Signed-off-by: anatoly techtonik <techtonik at gmail.com>
---
 lxc/file.go | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/lxc/file.go b/lxc/file.go
index 69f3db7..1d85d76 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -6,7 +6,6 @@ import (
 	"io/ioutil"
 	"os"
 	"path"
-	"path/filepath"
 	"strconv"
 	"strings"
 	"syscall"
@@ -168,7 +167,7 @@ func (c *fileCmd) push(config *lxd.Config, send_file_perms bool, args []string)
 		}
 
 		if c.mkdirs {
-			if err := d.MkdirP(container, filepath.Dir(fpath), mode); err != nil {
+			if err := d.MkdirP(container, path.Dir(fpath), mode); err != nil {
 				return err
 			}
 		}


More information about the lxc-devel mailing list