Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore excluded paths when transferring files #163

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 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 @@ -685,7 +691,7 @@ func (c *comm) scpUploadDirSession(dst string, src string, excl []string) error
return err
}

return scpUploadDir(src, entries, w, r)
return scpUploadDir(src, entries, w, r, excl)
}

if src[len(src)-1] != '/' {
Expand Down Expand Up @@ -960,10 +966,15 @@ func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() er
return err
}

func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) error {
func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader, excl []string) error {
for _, fi := range fs {
realPath := filepath.Join(root, fi.Name())

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

// Track if this is actually a symlink to a directory. If it is
// a symlink to a file we don't do any special behavior because uploading
// a file just works. If it is a directory, we need to know so we
Expand Down Expand Up @@ -1015,7 +1026,7 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) e
return err
}

return scpUploadDir(realPath, entries, w, r)
return scpUploadDir(realPath, entries, w, r, excl)
}, fi)
if err != nil {
return err
Expand All @@ -1024,3 +1035,24 @@ 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 {
Glyphack marked this conversation as resolved.
Show resolved Hide resolved
// Check if this file is excluded
excluded := false
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
}
31 changes: 31 additions & 0 deletions sdk-internals/communicator/ssh/communicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,34 @@ func TestHandshakeTimeout(t *testing.T) {
t.Fatalf("Expected handshake timeout, got: %s", err)
}
}

func TestIsExclude(t *testing.T) {
testCases := []struct {
Exclude []string
Path string
Is bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of clarity, this bool seems to be used to check that we did exclude something, could you rename it to make it clearer what it's used for?
That's personal preference, but I found that constructions like ExpectExclude works well at communicating the intent, but feel free to give it your own take on that!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion, applied!

}{
{
Exclude: []string{"foo"},
Path: "foo",
Is: true,
},
{
Exclude: []string{"foo/*"},
Path: "foo/bar",
Is: true,
},
{
Exclude: []string{"foo"},
Path: "bar",
Is: false,
},
}

for _, tc := range testCases {
if isExcluded(tc.Path, tc.Exclude) != tc.Is {
t.Fatalf("Expected %s to be excluded: %t", tc.Path, tc.Is)
}
}

}