From 7fa00dc90e986ec51179292074287725f9b88541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Tue, 18 Mar 2025 14:12:09 -0400 Subject: [PATCH] incus/file: Properly handle relative source paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When going through either .Split() or .Base(), a source path of ".." is incorrectly treated as an empty source directory and so processed the same as ".". This is obviously incorect behavior which then leads to files landing in the wrong directory on the server side. I couldn't find a good way with pure filepath calls to handle this, so instead we've got to hardcode the logic for the parent paths. Any path containing a separator (/) are handled correctly though. Closes #1808 Signed-off-by: Stéphane Graber --- cmd/incus/file.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/incus/file.go b/cmd/incus/file.go index a199e301506..8aaf3dd9926 100644 --- a/cmd/incus/file.go +++ b/cmd/incus/file.go @@ -1180,9 +1180,15 @@ func (c *cmdFile) recursivePullFile(sftpConn *sftp.Client, p string, targetDir s func (c *cmdFile) recursivePushFile(sftpConn *sftp.Client, source string, target string) error { source = filepath.Clean(source) + sourceDir, _ := filepath.Split(source) sourceLen := len(sourceDir) + // Special handling for relative paths. + if source == ".." { + sourceLen = 1 + } + sendFile := func(p string, fInfo os.FileInfo, err error) error { if err != nil { return fmt.Errorf(i18n.G("Failed to walk path for %s: %s"), p, err)