mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
sftp: fail listing a missing directory (#13280)
The filer streams nothing for ListEntries on a directory that does not exist, or on a file, so OPENDIR succeeded and READDIR returned an empty listing. Clients could not tell a missing directory from an empty one: `rclone lsf remote:does-not-exist` exited 0, and rclone's FsListDirNotFound test got nil instead of "directory not found". When the first page comes back empty, look the path up and return os.ErrNotExist unless it is a directory, which the request server sends as SSH_FX_NO_SUCH_FILE. Non-empty listings pay no extra lookup. Co-authored-by: Denis Maier <denis@dAir.lab> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
3 files changed
+51
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user