Skip to content

Commit

Permalink
Merge pull request #4210 from yoctocloud/fixes
Browse files Browse the repository at this point in the history
various fixes
  • Loading branch information
mwhooker authored Dec 12, 2016
2 parents 3895ec4 + 00ebe9e commit b49be38
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
12 changes: 6 additions & 6 deletions communicator/ssh/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (c *comm) DownloadDir(src string, dst string, excl []string) error {
break
case 'E':
dirStack = dirStack[:len(dirStack)-1]
if len(dirStack) == 1 {
if len(dirStack) == 0 {
fmt.Fprint(w, "\x00")
return nil
}
Expand All @@ -178,32 +178,32 @@ func (c *comm) DownloadDir(src string, dst string, excl []string) error {
return fmt.Errorf("unexpected server response (%x)", fi[0])
}

var mode string
var mode int64
var size int64
var name string
log.Printf("Download dir str:%s", fi)
n, err := fmt.Sscanf(fi, "%6s %d %s", &mode, &size, &name)
n, err := fmt.Sscanf(fi[1:], "%o %d %s", &mode, &size, &name)
if err != nil || n != 3 {
return fmt.Errorf("can't parse server response (%s)", fi)
}
if size < 0 {
return fmt.Errorf("negative file size")
}

log.Printf("Download dir mode:%s size:%d name:%s", mode, size, name)
log.Printf("Download dir mode:%0o size:%d name:%s", mode, size, name)

dst = filepath.Join(dirStack...)
switch fi[0] {
case 'D':
err = os.MkdirAll(filepath.Join(dst, name), os.FileMode(0755))
err = os.MkdirAll(filepath.Join(dst, name), os.FileMode(mode))
if err != nil {
return err
}
dirStack = append(dirStack, name)
continue
case 'C':
fmt.Fprint(w, "\x00")
err = scpDownloadFile(filepath.Join(dst, name), stdoutR, size, os.FileMode(0644))
err = scpDownloadFile(filepath.Join(dst, name), stdoutR, size, os.FileMode(mode))
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions post-processor/checksum/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
h = getHash(ct)

for _, art := range files {
if len(artifact.Files()) > 1 {
checksumFile = filepath.Join(filepath.Dir(art), ct+"sums")
} else if p.config.OutputPath != "" {
checksumFile = p.config.OutputPath
} else {
checksumFile = fmt.Sprintf("%s.%s", art, ct+"sum")
}
checksumFile = p.config.OutputPath

if _, err := os.Stat(checksumFile); err != nil {
newartifact.files = append(newartifact.files, checksumFile)
}
Expand All @@ -124,6 +119,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
fr.Close()
fw.WriteString(fmt.Sprintf("%x\t%s\n", h.Sum(nil), filepath.Base(art)))
fw.Close()
h.Reset()
}
}

Expand Down
27 changes: 18 additions & 9 deletions provisioner/file/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,28 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {

func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator) error {
for _, src := range p.config.Sources {
ui.Say(fmt.Sprintf("Downloading %s => %s", src, p.config.Destination))
dst := p.config.Destination
ui.Say(fmt.Sprintf("Downloading %s => %s", src, dst))
// ensure destination dir exists. p.config.Destination may either be a file or a dir.
dir := p.config.Destination
dir := dst
// if it doesn't end with a /, set dir as the parent dir
if !strings.HasSuffix(p.config.Destination, "/") {
if !strings.HasSuffix(dst, "/") {
dir = filepath.Dir(dir)
} else if !strings.HasSuffix(src, "/") && !strings.HasSuffix(src, "*") {
dst = filepath.Join(dst, filepath.Base(src))
}
if dir != "" {
err := os.MkdirAll(dir, os.FileMode(0755))
if err != nil {
return err
}
}
// if the config.Destination was a dir, download the dir
if strings.HasSuffix(p.config.Destination, "/") {
return comm.DownloadDir(src, p.config.Destination, nil)
// if the src was a dir, download the dir
if strings.HasSuffix(src, "/") || strings.IndexAny(src, "*?[") >= 0 {
return comm.DownloadDir(src, dst, nil)
}

f, err := os.OpenFile(p.config.Destination, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
Expand All @@ -130,7 +133,9 @@ func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator)

func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) error {
for _, src := range p.config.Sources {
ui.Say(fmt.Sprintf("Uploading %s => %s", src, p.config.Destination))
dst := p.config.Destination

ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst))

info, err := os.Stat(src)
if err != nil {
Expand All @@ -154,7 +159,11 @@ func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) er
return err
}

err = comm.Upload(p.config.Destination, f, &fi)
if strings.HasSuffix(dst, "/") {
dst = filepath.Join(dst, filepath.Base(src))
}

err = comm.Upload(dst, f, &fi)
if err != nil {
ui.Error(fmt.Sprintf("Upload failed: %s", err))
return err
Expand Down

0 comments on commit b49be38

Please sign in to comment.