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

lock mutex while getting wd and dir during ListFiles #245

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
47 changes: 29 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,31 +418,42 @@ func (c *client) UploadFile(path string, contents io.ReadCloser) error {
func (c *client) ListFiles(dir string) ([]string, error) {
pattern := filepath.Clean(strings.TrimPrefix(dir, string(os.PathSeparator)))

conn, err := c.connection()
if err = c.clearConnectionOnError(err); err != nil {
return nil, err
}

wd := "."
switch {
case dir == "/":
pattern = "*"
case pattern == ".":
if dir == "" {
if err := func() error {
c.mu.Lock()
defer c.mu.Unlock()

conn, err := c.connection()
if err != nil {
return err
}

switch {
case dir == "/":
pattern = "*"
} else {
pattern = filepath.Join(dir, "*")
case pattern == ".":
if dir == "" {
pattern = "*"
} else {
pattern = filepath.Join(dir, "*")
}
case pattern != "":
pattern = "[/?]" + pattern + "/*"
wd, err = conn.Getwd()
if err != nil {
return err
}
}
case pattern != "":
pattern = "[/?]" + pattern + "/*"
wd, err = conn.Getwd()

return nil
}(); err != nil {
if err = c.clearConnectionOnError(err); err != nil {
return nil, err
}
}

var filenames []string
err = c.Walk(wd, func(path string, d fs.DirEntry, err error) error {
if err := c.Walk(wd, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -468,10 +479,10 @@ func (c *client) ListFiles(dir string) ([]string, error) {
}
}
return err
})
if err != nil {
}); err != nil {
return nil, fmt.Errorf("listing %s failed: %w", dir, err)
}

return filenames, nil
}

Expand Down
Loading