From 931fae1fd43ec88504ee67b75799e7606df4dc24 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Fri, 2 Jun 2023 00:06:49 +0200 Subject: [PATCH] add check for isdir in readdir --- libs/filer/dbfs_client.go | 4 ++++ libs/filer/filer.go | 15 +++++++++++++++ libs/filer/workspace_files_client.go | 6 ++++++ 3 files changed, 25 insertions(+) diff --git a/libs/filer/dbfs_client.go b/libs/filer/dbfs_client.go index 7a3084ac..0a9d5b08 100644 --- a/libs/filer/dbfs_client.go +++ b/libs/filer/dbfs_client.go @@ -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}} diff --git a/libs/filer/filer.go b/libs/filer/filer.go index e54efb96..9d505399 100644 --- a/libs/filer/filer.go +++ b/libs/filer/filer.go @@ -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 { diff --git a/libs/filer/workspace_files_client.go b/libs/filer/workspace_files_client.go index b9f0f3db..594e1dbc 100644 --- a/libs/filer/workspace_files_client.go +++ b/libs/filer/workspace_files_client.go @@ -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