diff --git a/weed/sftpd/sftp_copy_existing_test.go b/weed/sftpd/sftp_copy_existing_test.go index 85c45b8466..d7258c61e3 100644 --- a/weed/sftpd/sftp_copy_existing_test.go +++ b/weed/sftpd/sftp_copy_existing_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "os" + "path" "testing" "github.com/seaweedfs/seaweedfs/weed/pb" @@ -31,6 +32,17 @@ func (f *copyTestFiler) LookupDirectoryEntry(_ context.Context, req *filer_pb.Lo return nil, status.Error(codes.NotFound, "no entry is found") } +func (f *copyTestFiler) ListEntries(req *filer_pb.ListEntriesRequest, stream grpc.ServerStreamingServer[filer_pb.ListEntriesResponse]) error { + for p, entry := range f.entries { + if path.Dir(p) == req.Directory { + if err := stream.Send(&filer_pb.ListEntriesResponse{Entry: entry}); err != nil { + return err + } + } + } + return nil +} + func (f *copyTestFiler) LookupVolume(_ context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) { resp := &filer_pb.LookupVolumeResponse{LocationsMap: make(map[string]*filer_pb.Locations)} for _, vid := range req.VolumeIds { diff --git a/weed/sftpd/sftp_filer.go b/weed/sftpd/sftp_filer.go index 30025f542e..582f4d2a91 100644 --- a/weed/sftpd/sftp_filer.go +++ b/weed/sftpd/sftp_filer.go @@ -509,6 +509,13 @@ func (fs *SftpServer) listAllPages(dirPath string) (sftp.ListerAt, error) { if err != nil { return nil, err } + // The filer lists a missing directory, or a file, as empty; look it up + // only then so a listing doesn't pay for an extra lookup. + if received == 0 && last == "" { + if err := fs.requireEntry(dirPath, true); err != nil { + return nil, err + } + } all = append(all, page...) if received < defaultListLimit { break diff --git a/weed/sftpd/sftp_filer_test.go b/weed/sftpd/sftp_filer_test.go index ead1884622..00dd5cd806 100644 --- a/weed/sftpd/sftp_filer_test.go +++ b/weed/sftpd/sftp_filer_test.go @@ -240,3 +240,35 @@ func TestWithFilerClientSendsAdminToken(t *testing.T) { t.Fatal("filer did not receive the request") } } + +// The filer lists a missing directory, or a file, as empty; sftp must report it as missing. +func TestListAllPagesMissingDirectory(t *testing.T) { + fs := newCopyTestServer(t, "", map[string]*filer_pb.Entry{ + "/home/d": {Name: "d", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{}}, + "/home/d/a.txt": {Name: "a.txt", Attributes: &filer_pb.FuseAttributes{}}, + "/home/empty": {Name: "empty", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{}}, + }) + fs.user.Permissions = map[string][]string{"/home": {"list"}} + + if _, err := fs.listAllPages("/home/missing"); err != os.ErrNotExist { + t.Fatalf("missing directory: got %v, want os.ErrNotExist", err) + } + if _, err := fs.listAllPages("/home/d/a.txt"); err != os.ErrNotExist { + t.Fatalf("file: got %v, want os.ErrNotExist", err) + } + lister, err := fs.listAllPages("/home/empty") + if err != nil { + t.Fatalf("empty directory: %v", err) + } + if n, _ := lister.ListAt(make([]os.FileInfo, 1), 0); n != 0 { + t.Fatalf("empty directory: got %d entries, want 0", n) + } + lister, err = fs.listAllPages("/home/d") + if err != nil { + t.Fatalf("directory: %v", err) + } + infos := make([]os.FileInfo, 2) + if n, _ := lister.ListAt(infos, 0); n != 1 || infos[0].Name() != "a.txt" { + t.Fatalf("directory: got %d entries, want a.txt", n) + } +}