From 93bddb3e65101d553e596d975098949129dfba36 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 4 Oct 2017 13:35:15 -0700 Subject: [PATCH] implement directory fix for lxc file uploads --- builder/lxc/communicator.go | 18 ++++++++++++++++++ communicator/ssh/communicator.go | 2 -- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/builder/lxc/communicator.go b/builder/lxc/communicator.go index 8d9765979..77ea3c052 100644 --- a/builder/lxc/communicator.go +++ b/builder/lxc/communicator.go @@ -71,6 +71,24 @@ func (c *LxcAttachCommunicator) Upload(dst string, r io.Reader, fi *os.FileInfo) return err } + if fi != nil { + tfDir := filepath.Dir(tf.Name()) + // rename tempfile to match original file name. This makes sure that if file is being + // moved into a directory, the filename is preserved instead of a temp name. + adjustedTempName := filepath.Join(tfDir, (*fi).Name()) + mvCmd, err := c.CmdWrapper(fmt.Sprintf("sudo mv %s %s", tf.Name(), adjustedTempName)) + if err != nil { + return err + } + defer os.Remove(adjustedTempName) + exitStatus := ShellCommand(mvCmd).Run() + // change cpCmd to use new file name as source + cpCmd, err = c.CmdWrapper(fmt.Sprintf("sudo cp %s %s", adjustedTempName, dst)) + if err != nil { + return err + } + } + log.Printf("Running copy command: %s", dst) return ShellCommand(cpCmd).Run() diff --git a/communicator/ssh/communicator.go b/communicator/ssh/communicator.go index 3ec2d1aba..8d5a388d5 100644 --- a/communicator/ssh/communicator.go +++ b/communicator/ssh/communicator.go @@ -550,8 +550,6 @@ func (c *comm) scpUploadSession(path string, input io.Reader, fi *os.FileInfo) e target_file = filepath.Base((*fi).Name()) } - log.Printf("target_file was %s", target_file) - // On windows, filepath.Dir uses backslash seperators (ie. "\tmp"). // This does not work when the target host is unix. Switch to forward slash // which works for unix and windows