From 317e9f5490b279a5edad99fa530ac68b956779ed Mon Sep 17 00:00:00 2001 From: aviau Date: Fri, 29 Jan 2021 00:14:12 -0500 Subject: [PATCH] fs: don't cause io errors on missing blobrefs - Looking up missing blobrefs would cause fs errors. Instead, simply return fuse.ENOENT. - Add tests for: * validating that the mountpoint/ lookups work * validating that the mountpount/ lookups don't fail when the blob does not exist. --- pkg/fs/fs_test.go | 29 +++++++++++++++++++++++++++++ pkg/fs/root.go | 6 +++++- pkg/test/world.go | 9 +++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/fs/fs_test.go b/pkg/fs/fs_test.go index 40f1755ed..9164ee394 100644 --- a/pkg/fs/fs_test.go +++ b/pkg/fs/fs_test.go @@ -90,6 +90,7 @@ type mountEnv struct { t *testing.T mountPoint string process *os.Process + world *test.World } func (e *mountEnv) Stat(s *stat) int64 { @@ -200,6 +201,7 @@ func pkmountTest(t *testing.T, fn func(env *mountEnv)) { t: t, mountPoint: mountPoint, process: mount.Process, + world: w, }) } @@ -224,6 +226,33 @@ func TestRoot(t *testing.T) { }) } +func TestReadFileFromRoot(t *testing.T) { + condSkip(t) + pkmountTest(t, func(env *mountEnv) { + // pk-put a file + tmpFile, err := ioutil.TempFile(t.TempDir(), "camlitest") + if err != nil { + t.Fatal(err) + } + testContent := "some test content" + tmpFile.WriteString(testContent) + blobRef := env.world.PutFile(t, tmpFile.Name()) + + // Read it using the file's blobref + if contents, err := ioutil.ReadFile(filepath.Join(env.mountPoint, blobRef.String())); err != nil { + t.Fatal(err) + } else if got := string(contents); got != testContent { + t.Fatalf("Expected test content, got %q", got) + } + + // Read a non-existing blobref, should return NotExist. + badRefPath := filepath.Join(env.mountPoint, "sha224-1853501438ffe541dd1e48b9efc4a230f67f7b98afe83df24bfbfa25") + if _, err := os.Stat(badRefPath); !os.IsNotExist(err) { + t.Fatalf("expected NotExist; got stat err = %v instead", err) + } + }) +} + type testLog struct { t *testing.T } diff --git a/pkg/fs/root.go b/pkg/fs/root.go index 1e78d5abc..c27177f93 100644 --- a/pkg/fs/root.go +++ b/pkg/fs/root.go @@ -135,7 +135,11 @@ func (n *root) Lookup(ctx context.Context, name string) (fs.Node, error) { if br, ok := blob.Parse(name); ok { Logger.Printf("Root lookup of blobref. %q => %v", name, br) - return &node{fs: n.fs, blobref: br}, nil + node := &node{fs: n.fs, blobref: br} + if _, err := node.schema(); os.IsNotExist(err) { + return nil, fuse.ENOENT + } + return node, nil } Logger.Printf("Bogus root lookup of %q", name) return nil, fuse.ENOENT diff --git a/pkg/test/world.go b/pkg/test/world.go index edf1b712e..22bd69c67 100644 --- a/pkg/test/world.go +++ b/pkg/test/world.go @@ -311,6 +311,15 @@ func (w *World) NewPermanode(t *testing.T) blob.Ref { return br } +func (w *World) PutFile(t *testing.T, name string) blob.Ref { + out := MustRunCmd(t, w.Cmd("pk-put", "file", name)) + br, ok := blob.Parse(strings.TrimSpace(out)) + if !ok { + t.Fatalf("Expected blobref in pk-put stdout; got %q", out) + } + return br +} + func (w *World) Cmd(binary string, args ...string) *exec.Cmd { return w.CmdWithEnv(binary, os.Environ(), args...) }