Skip to content

Commit

Permalink
bug: error with cloudstorage.ErrObjectNotFound for dirs (#98)
Browse files Browse the repository at this point in the history
this matches the behavior of the google provider
  • Loading branch information
humanchimp authored Mar 5, 2022
1 parent adf3d2f commit d105f8f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
27 changes: 21 additions & 6 deletions localfs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,25 @@ func (l *LocalStore) Folders(ctx context.Context, csq cloudstorage.Query) ([]str
func (l *LocalStore) NewReader(o string) (io.ReadCloser, error) {
return l.NewReaderWithContext(context.Background(), o)
}
func (l *LocalStore) NewReaderWithContext(ctx context.Context, o string) (io.ReadCloser, error) {
func (l *LocalStore) pathForObject(o string) (string, error) {
fo := path.Join(l.storepath, o)
if !cloudstorage.Exists(fo) {
return nil, cloudstorage.ErrObjectNotFound
return "", cloudstorage.ErrObjectNotFound
}
stat, err := os.Stat(fo)
if err != nil {
return "", err
}
if stat.IsDir() {
return "", cloudstorage.ErrObjectNotFound
}
return fo, nil
}

func (l *LocalStore) NewReaderWithContext(ctx context.Context, o string) (io.ReadCloser, error) {
fo, err := l.pathForObject(o)
if err != nil {
return nil, err
}
return csbufio.OpenReader(fo)
}
Expand Down Expand Up @@ -260,11 +275,11 @@ func (l *LocalStore) NewWriterWithContext(ctx context.Context, o string, metadat
}

func (l *LocalStore) Get(ctx context.Context, o string) (cloudstorage.Object, error) {
fo := path.Join(l.storepath, o)

if !cloudstorage.Exists(fo) {
return nil, cloudstorage.ErrObjectNotFound
fo, err := l.pathForObject(o)
if err != nil {
return nil, err
}

var updated time.Time
if stat, err := os.Stat(fo); err == nil {
updated = stat.ModTime()
Expand Down
37 changes: 37 additions & 0 deletions localfs/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package localfs_test

import (
"context"
"os"
"testing"

Expand Down Expand Up @@ -51,3 +52,39 @@ func TestAll(t *testing.T) {
assert.NotEqual(t, nil, err)
assert.Equal(t, nil, store)
}

func TestNewReaderDir(t *testing.T) {
// When a dir is requested, serve the index.html file instead
localFsConf := &cloudstorage.Config{
Type: localfs.StoreType,
AuthMethod: localfs.AuthFileSystem,
LocalFS: "/tmp/mockcloud",
TmpDir: "/tmp/localcache",
}
store, err := cloudstorage.NewStore(localFsConf)
testutils.MockFile(store, "test/index.html", "test")
assert.Equal(t, nil, err)
assert.Equal(t, nil, err)
_, err = store.NewReader("test")
assert.Equal(t, err, cloudstorage.ErrObjectNotFound)
err = store.Delete(context.Background(), "test/index.html")
assert.Equal(t, nil, err)
}

func TestGetDir(t *testing.T) {
// When a dir is requested, serve the index.html file instead
localFsConf := &cloudstorage.Config{
Type: localfs.StoreType,
AuthMethod: localfs.AuthFileSystem,
LocalFS: "/tmp/mockcloud",
TmpDir: "/tmp/localcache",
}
store, err := cloudstorage.NewStore(localFsConf)
testutils.MockFile(store, "test/index.html", "test")
assert.Equal(t, nil, err)
assert.Equal(t, nil, err)
_, err = store.Get(context.Background(), "test")
assert.Equal(t, err, cloudstorage.ErrObjectNotFound)
err = store.Delete(context.Background(), "test/index.html")
assert.Equal(t, nil, err)
}
19 changes: 19 additions & 0 deletions testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,22 @@ func MultipleRW(t TestingT, store cloudstorage.Store, conf *cloudstorage.Config)
assert.Equal(t, nil, err)
}
}

func MockFile(store cloudstorage.Store, path string, body string) error {
obj, err := store.NewObject(path)
if err != nil {
return err
}
f, err := obj.Open(cloudstorage.ReadWrite)
if err != nil {
return err
}
w := bufio.NewWriter(f)

if _, err := w.WriteString(body); err != nil {
return err
}
w.Flush()
obj.Close()
return nil
}

0 comments on commit d105f8f

Please sign in to comment.