Skip to content

Commit

Permalink
Merge pull request #9 from jarxorg/fix-readdirfile
Browse files Browse the repository at this point in the history
Fix readdirfile
  • Loading branch information
mojatter authored Nov 12, 2021
2 parents b09ff99 + edeafad commit 6017099
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
7 changes: 2 additions & 5 deletions memfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,12 @@ func (f *MemFile) ReadDir(n int) ([]fs.DirEntry, error) {
}
max := len(f.dirEntries)
if f.dirIndex >= max {
if n == -1 {
if n <= 0 {
return nil, nil
}
return nil, io.EOF
}
if n == 0 {
return nil, nil
}
if n == -1 {
if n <= 0 {
n = max - f.dirIndex
}
end := f.dirIndex + n
Expand Down
23 changes: 0 additions & 23 deletions memfs/memfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,26 +513,3 @@ func TestMemFile_ReadDir_Errors(t *testing.T) {
t.Fatalf(`Fatal ReadDir(1) returns no error`)
}
}

func TestMemFile_ReadDir0(t *testing.T) {
fsys := newMemFSTest(t)
dir := "dir0"

f, err := fsys.Open(dir)
if err != nil {
t.Fatal(err)
}

memf, ok := f.(*MemFile)
if !ok {
t.Fatalf(`Fatal not MemFile: %#v`, f)
}

entries, err := memf.ReadDir(0)
if err != nil {
t.Fatal(err)
}
if len(entries) != 0 {
t.Errorf(`Error ReadDir(0) returns unknown entries %v`, entries)
}
}
32 changes: 16 additions & 16 deletions memfs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,39 @@ var (
_ fs.FileInfo = (*value)(nil)
)

func (e *value) Name() string {
return filepath.Base(e.name)
func (v *value) Name() string {
return filepath.Base(v.name)
}

func (e *value) Size() int64 {
if e.isDir {
func (v *value) Size() int64 {
if v.isDir {
return 0
}
return int64(len(e.data))
return int64(len(v.data))
}

func (e *value) Mode() fs.FileMode {
return e.mode
func (v *value) Mode() fs.FileMode {
return v.mode
}

func (e *value) ModTime() time.Time {
return e.modTime
func (v *value) ModTime() time.Time {
return v.modTime
}

func (e *value) IsDir() bool {
return e.isDir
func (v *value) IsDir() bool {
return v.isDir
}

func (e *value) Sys() interface{} {
func (v *value) Sys() interface{} {
return nil
}

func (e *value) Type() fs.FileMode {
return e.mode
func (v *value) Type() fs.FileMode {
return v.mode
}

func (e *value) Info() (fs.FileInfo, error) {
return e, nil
func (v *value) Info() (fs.FileInfo, error) {
return v, nil
}

// Store represents an in-memory key value store.
Expand Down

0 comments on commit 6017099

Please sign in to comment.