Skip to content

Commit

Permalink
fix sftp upload on windows by forcing the use of slashes in remote paths
Browse files Browse the repository at this point in the history
  • Loading branch information
orgrim committed Dec 27, 2021
1 parent e6e9333 commit f6bbb1f
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,16 @@ func (r *sftpRepo) Upload(path string, target string) error {
defer src.Close()

rpath := filepath.Join(r.baseDir, target)
targetDir := filepath.Dir(rpath)

// sftp requires slash as path separator
if os.PathSeparator != '/' {
rpath = strings.ReplaceAll(rpath, string(os.PathSeparator), "/")
targetDir = strings.ReplaceAll(targetDir, string(os.PathSeparator), "/")
}
l.Verboseln("sftp remote path is:", rpath)

// Target directory must be created first
targetDir := filepath.Dir(rpath)
if targetDir != "." && targetDir != "/" {
if err := r.client.MkdirAll(targetDir); err != nil {
return fmt.Errorf("sftp: could not create parent directory of %s: %w", rpath, err)
Expand All @@ -469,23 +476,35 @@ func (r *sftpRepo) Upload(path string, target string) error {
defer dst.Close()

if _, err := io.Copy(dst, src); err != nil {
return fmt.Errorf("sftp: could not open send data with sftp: %s", err)
return fmt.Errorf("sftp: could not send data with sftp: %s", err)
}

return nil
}

func (r *sftpRepo) List(prefix string) (items []Item, rerr error) {
items = make([]Item, 0)
w := r.client.Walk(r.baseDir)

// sftp requires slash as path separator
baseDir := r.baseDir
if os.PathSeparator != '/' {
baseDir = strings.ReplaceAll(baseDir, string(os.PathSeparator), "/")
}

w := r.client.Walk(baseDir)
for w.Step() {
if err := w.Err(); err != nil {
l.Warnln("could not list remote file:", err)
rerr = err
continue
}

path := relPath(r.baseDir, w.Path())
// relPath() makes use of functions of the filepath std module
// that take care of putting back the proper os.PathSeparator
// if it find some slashes, so we can compare paths without
// worrying about path separators
path := relPath(baseDir, w.Path())

if !strings.HasPrefix(path, prefix) {
continue
}
Expand All @@ -502,7 +521,14 @@ func (r *sftpRepo) List(prefix string) (items []Item, rerr error) {
}

func (r *sftpRepo) Remove(path string) error {
if err := r.client.Remove(filepath.Join(r.baseDir, path)); err != nil {
rpath := filepath.Join(r.baseDir, path)

// sftp requires slash as path separator
if os.PathSeparator != '/' {
rpath = strings.ReplaceAll(rpath, string(os.PathSeparator), "/")
}

if err := r.client.Remove(rpath); err != nil {
return err
}

Expand Down

0 comments on commit f6bbb1f

Please sign in to comment.