Skip to content

Commit

Permalink
ignore excluded paths when transferring files with stfp
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Mar 21, 2023
1 parent be46f1a commit e252305
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions sdk-internals/communicator/ssh/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ func (c *comm) sftpUploadDirSession(dst string, src string, excl []string) error
if err != nil {
return err
}

if excluded := isExcluded(relSrc, excl); excluded {
log.Printf("[DEBUG] SCP: skipping excluded file: %s", relSrc)
return nil
}

finalDst := filepath.Join(rootDst, relSrc)

// In Windows, Join uses backslashes which we don't want to get
Expand Down Expand Up @@ -964,16 +970,7 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader, e
for _, fi := range fs {
realPath := filepath.Join(root, fi.Name())

// Check if this file is excluded
excluded := false
for _, e := range excl {
if e == realPath {
excluded = true
break
}
}

if excluded {
if excluded := isExcluded(realPath, excl); excluded {
log.Printf("[DEBUG] SCP: skipping excluded file: %s", realPath)
continue
}
Expand Down Expand Up @@ -1038,3 +1035,25 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader, e

return nil
}

// isExcluded checks if the given file path is excluded by the given list of
// excludes.
// It does shell style matching, so the excludes can contain wildcards.
func isExcluded(filePath string, excludes []string) bool {
// Check if this file is excluded
excluded := false
log.Printf("[DEBUG] SCP: checking if %v is excluded", excludes)
for _, excl := range excludes {
log.Printf("[DEBUG] SCP: checking if %s is excluded by %s", filePath, excl)
match, err := filepath.Match(excl, filePath)
if err != nil {
log.Printf("[DEBUG] SCP: error matching %s to %s: %s", filePath, excl, err)
continue
}
if match {
excluded = true
break
}
}
return excluded
}

0 comments on commit e252305

Please sign in to comment.