[lxc-devel] [lxd/master] lxc/file: Expand complex symlink chains

stgraber on Github lxc-bot at linuxcontainers.org
Thu Feb 13 21:14:25 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200213/d2d56302/attachment.bin>
-------------- next part --------------
From 17356bee9218a311688299cac52bd8f6adb4fc77 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 13 Feb 2020 16:13:37 -0500
Subject: [PATCH 1/2] shared/util: Tweak HostPathFollow to use readlink
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 shared/util.go | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index 0db130129f..aae5defc6f 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -109,20 +109,22 @@ func IsUnixSocket(path string) bool {
 // HostPathFollow takes a valid path (from HostPath) and resolves it
 // all the way to its target or to the last which can be resolved.
 func HostPathFollow(path string) string {
-	// Check if the path is already snap-aware
-	for _, prefix := range []string{"/dev", "/snap", "/var/snap", "/var/lib/snapd"} {
-		if path == prefix || strings.HasPrefix(path, fmt.Sprintf("%s/", prefix)) {
-			return path
-		}
+	// Check if we're running in a snap package.
+	_, inSnap := os.LookupEnv("SNAP")
+	snapName := os.Getenv("SNAP_NAME")
+	if !inSnap || snapName != "lxd" {
+		return path
 	}
 
+	// Rely on "readlink -m" to do the right thing.
 	for {
-		target, err := os.Readlink(path)
+		target, err := RunCommand("readlink", "-m", path)
 		if err != nil {
 			return path
 		}
+		target = strings.TrimSpace(target)
 
-		if target == path {
+		if path == HostPath(target) {
 			return path
 		}
 

From 47ec76da72533100a019b0f14cf3dd4efe3433c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 13 Feb 2020 16:13:40 -0500
Subject: [PATCH 2/2] lxc/file: Expand complex symlink chains
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #6874

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 lxc/file.go | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/lxc/file.go b/lxc/file.go
index aa7167a84b..82c8fe26b3 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -235,7 +235,7 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error {
 	}
 
 	// Determine the target
-	target := shared.HostPath(filepath.Clean(args[len(args)-1]))
+	target := shared.HostPathFollow(filepath.Clean(args[len(args)-1]))
 	targetIsDir := false
 	sb, err := os.Stat(target)
 	if err != nil && !os.IsNotExist(err) {
@@ -457,7 +457,7 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
 	// Make a list of paths to transfer
 	sourcefilenames := []string{}
 	for _, fname := range args[:len(args)-1] {
-		sourcefilenames = append(sourcefilenames, shared.HostPath(filepath.Clean(fname)))
+		sourcefilenames = append(sourcefilenames, shared.HostPathFollow(filepath.Clean(fname)))
 	}
 
 	// Determine the target mode
@@ -483,7 +483,7 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
 
 		// Create needed paths if requested
 		if c.file.flagMkdir {
-			f, err := os.Open(args[0])
+			f, err := os.Open(sourcefilenames[0])
 			if err != nil {
 				return err
 			}
@@ -535,9 +535,6 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
 		if f == "-" {
 			file = os.Stdin
 		} else {
-			// Follow symlinks within the snap environment.
-			f = shared.HostPathFollow(f)
-
 			file, err = os.Open(f)
 			if err != nil {
 				return err


More information about the lxc-devel mailing list