Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UnionFile.Readdir return when c <= 0 #199

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestCacheOnReadFsNotInLayer(t *testing.T) {
}

// #194
func TestUniontFileReaddirEmpty(t *testing.T) {
func TestUnionFileReaddirEmpty(t *testing.T) {
osFs := NewOsFs()

base := NewMemMapFs()
Expand Down Expand Up @@ -439,7 +439,41 @@ func TestUniontFileReaddirEmpty(t *testing.T) {
}
}

func TestUniontFileReaddirAskForTooMany(t *testing.T) {
// #197
func TestUnionFileReaddirDuplicateEmpty(t *testing.T) {
base := NewMemMapFs()
dir, err := TempDir(base, "", "empty-dir")
if err != nil {
t.Fatal(err)
}

// Overlay shares same empty directory as base
overlay := NewMemMapFs()
err = overlay.Mkdir(dir, 0700)
if err != nil {
t.Fatal(err)
}

ufs := &CopyOnWriteFs{base: base, layer: overlay}

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

names, err := f.Readdirnames(0)

if err == io.EOF {
t.Errorf("unexpected io.EOF error")
}

if len(names) != 0 {
t.Fatal("should be empty")
}
}

func TestUnionFileReaddirAskForTooMany(t *testing.T) {
base := &MemMapFs{}
overlay := &MemMapFs{}

Expand Down
10 changes: 7 additions & 3 deletions unionFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ var defaultUnionMergeDirsFn = func(lofi, bofi []os.FileInfo) ([]os.FileInfo, err
}

// Readdir will weave the two directories together and
// return a single view of the overlayed directories
// At the end of the directory view, the error is io.EOF.
// return a single view of the overlayed directories.
// At the end of the directory view, the error is io.EOF if c > 0.
func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) {
var merge DirsMerger = f.Merger
if merge == nil {
Expand Down Expand Up @@ -187,11 +187,15 @@ func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) {
f.files = append(f.files, merged...)
}

if c <= 0 && len(f.files) == 0 {
return f.files, nil
}

if f.off >= len(f.files) {
return nil, io.EOF
}

if c == -1 {
if c <= 0 {
return f.files[f.off:], nil
}

Expand Down