mirror of https://github.com/spf13/afero.git
Merge pull request #260 from std0/union-readdir-fix
Fix amount of files read in UnionFile.Readdir
This commit is contained in:
commit
27c9ee0498
|
@ -477,7 +477,8 @@ func TestUnionFileReaddirAskForTooMany(t *testing.T) {
|
||||||
base := &MemMapFs{}
|
base := &MemMapFs{}
|
||||||
overlay := &MemMapFs{}
|
overlay := &MemMapFs{}
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
const testFiles = 5
|
||||||
|
for i := 0; i < testFiles; i++ {
|
||||||
WriteFile(base, fmt.Sprintf("file%d.txt", i), []byte("afero"), 0777)
|
WriteFile(base, fmt.Sprintf("file%d.txt", i), []byte("afero"), 0777)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -490,13 +491,24 @@ func TestUnionFileReaddirAskForTooMany(t *testing.T) {
|
||||||
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
names, err := f.Readdirnames(6)
|
// Read part of all files
|
||||||
|
wantNames := 3
|
||||||
|
names, err := f.Readdirnames(wantNames)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if len(names) != wantNames {
|
||||||
|
t.Fatalf("got %d names %v, want %d", len(names), names, wantNames)
|
||||||
|
}
|
||||||
|
|
||||||
if len(names) != 5 {
|
// Try to read more files than remaining
|
||||||
t.Fatal(names)
|
wantNames = testFiles - len(names)
|
||||||
|
names, err = f.Readdirnames(wantNames + 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(names) != wantNames {
|
||||||
|
t.Fatalf("got %d names %v, want %d", len(names), names, wantNames)
|
||||||
}
|
}
|
||||||
|
|
||||||
// End of directory
|
// End of directory
|
||||||
|
|
17
unionFile.go
17
unionFile.go
|
@ -186,25 +186,22 @@ func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) {
|
||||||
}
|
}
|
||||||
f.files = append(f.files, merged...)
|
f.files = append(f.files, merged...)
|
||||||
}
|
}
|
||||||
|
files := f.files[f.off:]
|
||||||
|
|
||||||
if c <= 0 && len(f.files) == 0 {
|
if c <= 0 {
|
||||||
return f.files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.off >= len(f.files) {
|
if len(files) == 0 {
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
if c <= 0 {
|
if c > len(files) {
|
||||||
return f.files[f.off:], nil
|
c = len(files)
|
||||||
}
|
|
||||||
|
|
||||||
if c > len(f.files) {
|
|
||||||
c = len(f.files)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() { f.off += c }()
|
defer func() { f.off += c }()
|
||||||
return f.files[f.off:c], nil
|
return files[:c], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *UnionFile) Readdirnames(c int) ([]string, error) {
|
func (f *UnionFile) Readdirnames(c int) ([]string, error) {
|
||||||
|
|
Loading…
Reference in New Issue