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

Fixed #185 - location.Exists was checking if a list entry was a direc… #186

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]

## [6.14.1] - 2024-05-28
### Fixed
- Fixed #185 - location.Exists was checking if a list entry was a directory but it was was only checking the first entry.

## [6.14.0] - 2024-05-15
### Security
- updated dependencies
Expand Down
2 changes: 1 addition & 1 deletion backend/ftp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (f *File) MoveToFile(t vfs.File) error {
// it doesn't matter which client we use since they are effectively the same
err = dc.MakeDir(t.Location().Path())
if err != nil {
return err
return fmt.Errorf("failed to create directory: %w", err)
}
}
return dc.Rename(f.Path(), t.Path())
Expand Down
7 changes: 4 additions & 3 deletions backend/ftp/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ func (l *Location) Exists() (bool, error) {
return false, err
}

locBasename := path.Base(l.Path())
// get parent directory by removing the last part of the path
parentDir := strings.TrimSuffix(l.Path(), path.Base(l.Path())+"/")
parentDir := strings.TrimSuffix(l.Path(), locBasename+"/")

entries, err := dc.List(parentDir)
if err != nil {
Expand All @@ -156,8 +157,8 @@ func (l *Location) Exists() (bool, error) {
return false, err
}

for _, entry := range entries {
if entry.Name == path.Base(l.Path()) && entries[0].Type == _ftp.EntryTypeFolder {
for i := range entries {
if entries[i].Name == locBasename && entries[i].Type == _ftp.EntryTypeFolder {
Copy link
Member Author

Choose a reason for hiding this comment

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

entries[0] was the culprit here... only checking the first entry's type, resulting Exists returning false, then MoveToFile tried to create the dir (which already existed).

return true, nil
}
}
Expand Down
35 changes: 27 additions & 8 deletions backend/ftp/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,21 @@ func (lt *locationTestSuite) TestExists() {

// location exists
locPath := "/"
dirs := []*_ftp.Entry{
entries := []*_ftp.Entry{
{
Name: "file.txt",
Target: "",
Type: _ftp.EntryTypeFile,
Time: time.Now().UTC(),
},
{
Name: locPath,
Target: "",
Type: _ftp.EntryTypeFolder,
Time: time.Now().UTC(),
},
}
lt.client.On("List", locPath).Return(dirs, nil).Once()
lt.client.On("List", locPath).Return(entries, nil).Once()
loc, err := lt.ftpfs.NewLocation(authority, locPath)
lt.NoError(err)
exists, err := loc.Exists()
Expand All @@ -405,32 +411,45 @@ func (lt *locationTestSuite) TestExists() {

// locations does not exist
locPath = "/my/dir/"
dirs = []*_ftp.Entry{}
lt.client.On("List", "/my/").Return(dirs, nil).Once()
entries = []*_ftp.Entry{
{
Name: "file.txt",
Target: "",
Type: _ftp.EntryTypeFile,
Time: time.Now().UTC(),
},
}
lt.client.On("List", "/my/").Return(entries, nil).Once()
loc, err = lt.ftpfs.NewLocation(authority, locPath)
lt.NoError(err)
exists, err = loc.Exists()
lt.Nil(err, "No error expected from Exists")
lt.True(!exists, "Call to Exists expected to return false.")

// some error calling list
lt.client.On("List", "/my/").Return(dirs, errors.New("some error")).Once()
lt.client.On("List", "/my/").Return(entries, errors.New("some error")).Once()
loc, err = lt.ftpfs.NewLocation(authority, locPath)
lt.NoError(err)
exists, err = loc.Exists()
lt.Error(err, "from Exists")
lt.True(!exists, "Call to Exists expected to return false.")

// check for not dir -- this shoudln't be possible since NewLocation won't accept non-absolute directories
dirs = []*_ftp.Entry{
// check for not dir -- this shouldn't be possible since NewLocation won't accept non-absolute directories
entries = []*_ftp.Entry{
{
Name: "file.txt",
Target: "",
Type: _ftp.EntryTypeFile,
Time: time.Now().UTC(),
},
{
Name: locPath,
Target: "",
Type: _ftp.EntryTypeFile,
Time: time.Now().UTC(),
},
}
lt.client.On("List", "/my/").Return(dirs, nil).Once()
lt.client.On("List", "/my/").Return(entries, nil).Once()
loc, err = lt.ftpfs.NewLocation(authority, locPath)
lt.NoError(err)
exists, err = loc.Exists()
Expand Down
Loading