[lxc-devel] [lxd/master] lxc/file: Allow using -r to follow symlinks

stgraber on Github lxc-bot at linuxcontainers.org
Thu Apr 5 23:29:07 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180405/c71fe1ce/attachment.bin>
-------------- next part --------------
From 8d0222dcb1fb4f73be8d3141a078903258daa317 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 5 Apr 2018 17:51:53 -0400
Subject: [PATCH] lxc/file: Allow using -r to follow symlinks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #4411

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxc/file.go | 58 +++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 39 insertions(+), 19 deletions(-)

diff --git a/lxc/file.go b/lxc/file.go
index b8f0f4ed44..adde732c0f 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -243,22 +243,23 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error {
 			return fmt.Errorf(i18n.G("Invalid source %s"), resource.name)
 		}
 
-		if c.file.flagRecursive {
-			err := c.file.recursivePullFile(resource.server, pathSpec[0], pathSpec[1], target)
-			if err != nil {
-				return err
-			}
-
-			continue
-		}
-
 		buf, resp, err := resource.server.GetContainerFile(pathSpec[0], pathSpec[1])
 		if err != nil {
 			return err
 		}
 
+		// Deal with recursion
 		if resp.Type == "directory" {
-			return fmt.Errorf(i18n.G("Can't pull a directory without --recursive"))
+			if c.file.flagRecursive {
+				err := c.file.recursivePullFile(resource.server, pathSpec[0], pathSpec[1], target)
+				if err != nil {
+					return err
+				}
+
+				continue
+			} else {
+				return fmt.Errorf(i18n.G("Can't pull a directory without --recursive"))
+			}
 		}
 
 		var targetPath string
@@ -270,16 +271,30 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error {
 
 		logger.Infof("Pulling %s from %s (%s)", targetPath, pathSpec[1], resp.Type)
 
-		var f *os.File
-		if targetPath == "-" {
-			f = os.Stdout
-		} else {
-			if resp.Type == "symlink" {
-				linkTarget, err := ioutil.ReadAll(buf)
-				if err != nil {
-					return err
-				}
+		if resp.Type == "symlink" {
+			linkTarget, err := ioutil.ReadAll(buf)
+			if err != nil {
+				return err
+			}
 
+			// Follow the symlink
+			if targetPath == "-" || c.file.flagRecursive {
+				for {
+					newPath := strings.TrimSuffix(string(linkTarget), "\n")
+					if !strings.HasPrefix(newPath, "/") {
+						newPath = filepath.Clean(filepath.Join(filepath.Dir(pathSpec[1]), newPath))
+					}
+
+					buf, resp, err = resource.server.GetContainerFile(pathSpec[0], newPath)
+					if err != nil {
+						return err
+					}
+
+					if resp.Type != "symlink" {
+						break
+					}
+				}
+			} else {
 				err = os.Symlink(strings.TrimSpace(string(linkTarget)), targetPath)
 				if err != nil {
 					return err
@@ -287,7 +302,12 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error {
 
 				continue
 			}
+		}
 
+		var f *os.File
+		if targetPath == "-" {
+			f = os.Stdout
+		} else {
 			f, err = os.Create(targetPath)
 			if err != nil {
 				return err


More information about the lxc-devel mailing list