Skip to content

Commit

Permalink
Merge pull request #135 from rodaine/cacheOnRead-NotExistsError
Browse files Browse the repository at this point in the history
CacheOnReadFS: erroneous NotExists error from MemMapFS layer
  • Loading branch information
mbertschler authored Oct 3, 2017
2 parents 44971ef + 838d6de commit 8a6ade7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
9 changes: 2 additions & 7 deletions cacheOnReadFs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,10 @@ func (u *CacheOnReadFs) cacheStatus(name string) (state cacheState, fi os.FileIn
return cacheHit, lfi, nil
}

if err == syscall.ENOENT {
if err == syscall.ENOENT || os.IsNotExist(err) {
return cacheMiss, nil, nil
}
var ok bool
if err, ok = err.(*os.PathError); ok {
if err == os.ErrNotExist {
return cacheMiss, nil, nil
}
}

return cacheMiss, nil, err
}

Expand Down
36 changes: 36 additions & 0 deletions composite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package afero

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -366,3 +367,38 @@ func TestUnionCacheExpire(t *testing.T) {
t.Errorf("cache time failed: <%s>", data)
}
}

func TestCacheOnReadFs_Open_NotInLayer(t *testing.T) {
base := NewMemMapFs()
layer := NewMemMapFs()
fs := NewCacheOnReadFs(base, layer, 0)

fh, err := base.Create("/file.txt")
if err != nil {
t.Fatal("unable to create file: ", err)
}

txt := []byte("This is a test")
fh.Write(txt)
fh.Close()

fh, err = fs.Open("/file.txt")
if err != nil {
t.Fatal("could not open file: ", err)
}

b, err := ReadAll(fh)
fh.Close()

if err != nil {
t.Fatal("could not read file: ", err)
} else if !bytes.Equal(txt, b) {
t.Fatalf("wanted file text %q, got %q", txt, b)
}

fh, err = layer.Open("/file.txt")
if err != nil {
t.Fatal("could not open file from layer: ", err)
}
fh.Close()
}

0 comments on commit 8a6ade7

Please sign in to comment.