add check for isdir in readdir

This commit is contained in:
Shreyas Goenka 2023-06-02 00:06:49 +02:00
parent fb5ea166ca
commit 931fae1fd4
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 25 additions and 0 deletions

View File

@ -222,6 +222,10 @@ func (w *DbfsClient) ReadDir(ctx context.Context, name string) ([]fs.DirEntry, e
return nil, err
}
if len(res.Files) == 1 && !res.Files[0].IsDir && res.Files[0].Path == absPath {
return nil, NotADirectory{absPath}
}
info := make([]fs.DirEntry, len(res.Files))
for i, v := range res.Files {
info[i] = dbfsDirEntry{dbfsFileInfo: dbfsFileInfo{fi: v}}

View File

@ -2,6 +2,7 @@ package filer
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
@ -50,6 +51,20 @@ func (err NoSuchDirectoryError) Is(other error) bool {
return other == fs.ErrNotExist
}
var ErrNotADirectory = errors.New("not a directory")
type NotADirectory struct {
path string
}
func (err NotADirectory) Error() string {
return fmt.Sprintf("%s is not a directory", err.path)
}
func (err NotADirectory) Is(other error) bool {
return other == ErrNotADirectory
}
// Filer is used to access files in a workspace.
// It has implementations for accessing files in WSFS and in DBFS.
type Filer interface {

View File

@ -222,6 +222,12 @@ func (w *WorkspaceFilesClient) ReadDir(ctx context.Context, name string) ([]fs.D
objects, err := w.workspaceClient.Workspace.ListAll(ctx, workspace.ListWorkspaceRequest{
Path: absPath,
})
// TODO: add integration test for this
if len(objects) == 1 && objects[0].Path == absPath {
return nil, NotADirectory{absPath}
}
if err != nil {
// If we got an API error we deal with it below.
var aerr *apierr.APIError