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

Interface->fs.FS translation logic #8

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Interface->fs.FS translation logic #8

wants to merge 9 commits into from

Conversation

ahouene
Copy link
Contributor

@ahouene ahouene commented Jul 13, 2022

Implement a wrapper for Interface and Blob. Wrapped filesystems implements fs.ReadFileFS, fs.ReadDirFS, fs.SubFS, fs.GlobFS and fs.StatFS.

Wrapping function func AsFS(Interface) fs.FS has been implemented as described in #2.

Filesystem actions related to subdirectories will only work if the subdirectory's path is ".".

b := New()
tester.DoBackendTests(t, b)
tester.DoBackendTests(t, New())
tester.DoFSWrapperTests(t, New())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would fold this into DoBackendTests, so that that one always tests everything we would want to test about a backend. It could skip certain tests if the backend does not implement an optional backend.

fs.go Outdated
}
n, err := bw.r.Read(p)
if err == io.EOF {
bw.r = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not reset. if someone calls read twice on an EOF object, it will read the whole file all over again. Only reset it when Close is close is called, after which the File object should probably be made invalid.

return nil
}

// ReadDir satisfies fs.ReadDirFS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be best if we do not implement extra FS interface and instead stick to the basic fs.FS, but ReadDirFS could be a useful addition and corresponds to the List call.

I'm not sure if the single root directory restriction is problematic or not, I can imagine that we may want to store blocs with / in the future. Currently we do not clearly describe what kind of names are allowed and this could vary by backend, which is probably not a good thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the name checking. I feel like fs.ReadFileFS should stay too because it matches what Load does. What do you think?

fs.go Outdated
return ret, nil
}

// Glob satisfies fs.GlobFS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically reimplements the default fs.Glob fallback implementation. I would leave this one out.

fs.go Outdated

// AsFS casts the provided interface to a fs.FS interface if supported,
// else it wraps it to replicate its functionalities.
func AsFS(st Interface) fs.FS {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all storage methods require a context and the FS interface itself does have have a way to provide one, I suggest passing it here and holding on to it in the wrapper. Using context.Background somewhere other than main is a bad idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I'll push a fix. Thank you!

Comment on lines +75 to +90
func (bw *fsBlobWrapper) Read(p []byte) (int, error) {
if bw.r == nil {
b, err := bw.parent.Interface.Load(bw.parent.ctx, bw.b.Name)
if err != nil {
return 0, err
}
bw.r = bytes.NewReader(b)
}
return bw.r.Read(p)
}
func (bw *fsBlobWrapper) Close() error {
if bw.r != nil {
bw.r = nil
}
return nil
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use simpleblob.NewReader once #11 is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants