fs refactor

This commit is contained in:
royalcat 2024-06-26 00:39:30 +03:00
parent 3dcf27d900
commit 0fa3a91447
13 changed files with 80 additions and 71 deletions
pkg

View file

@ -13,10 +13,6 @@ type Filesystem interface {
// it if it already exists. If successful, methods on the returned File can
// be used for I/O; the associated file descriptor has mode O_RDWR.
Create(ctx context.Context, filename string) (File, error)
// Open opens the named file for reading. If successful, methods on the
// returned file can be used for reading; the associated file descriptor has
// mode O_RDONLY.
Open(ctx context.Context, filename string) (File, error)
// OpenFile is the generalized open call; most users will use Open or Create
// instead. It opens the named file with specified flag (O_RDONLY etc.) and
// perm, (0666 etc.) if applicable. If successful, methods on the returned

View file

@ -117,10 +117,6 @@ func (fs *UringFS) MkdirAll(ctx context.Context, path string, perm os.FileMode)
return os.MkdirAll(dir, perm)
}
func (fs *UringFS) Open(ctx context.Context, filename string) (File, error) {
return fs.OpenFile(ctx, filename, os.O_RDONLY, 0)
}
func (fs *UringFS) Stat(ctx context.Context, filename string) (os.FileInfo, error) {
filename, err := fs.abs(filename)
if err != nil {

View file

@ -6,6 +6,7 @@ import (
"errors"
"io"
"os"
"sync"
"github.com/royalcat/ctxio"
)
@ -19,6 +20,7 @@ type FileBuffer struct {
// index indicates where in the buffer we are at
index int64
isClosed bool
mu sync.RWMutex
}
var _ FileReader = (*FileBuffer)(nil)
@ -53,14 +55,20 @@ func NewFileBufferFromIoReader(reader io.Reader) (*FileBuffer, error) {
// Bytes returns the bytes available until the end of the buffer.
func (f *FileBuffer) Bytes() []byte {
f.mu.RLock()
defer f.mu.RUnlock()
if f.isClosed || f.index >= int64(f.buff.Len()) {
return []byte{}
}
return f.buff.Bytes()[f.index:]
return bytes.Clone(f.buff.Bytes()[f.index:])
}
// String implements the Stringer interface
func (f *FileBuffer) String() string {
f.mu.RLock()
defer f.mu.RUnlock()
return string(f.buff.Bytes()[f.index:])
}
@ -76,6 +84,9 @@ func (f *FileBuffer) String() string {
// that a Reader returning a non-zero number of bytes at the end of the input stream may return
// either err == EOF or err == nil. The next Read should return 0, EOF.
func (f *FileBuffer) Read(ctx context.Context, b []byte) (n int, err error) {
f.mu.RLock()
defer f.mu.RUnlock()
if f.isClosed {
return 0, os.ErrClosed
}
@ -109,6 +120,9 @@ func (f *FileBuffer) Read(ctx context.Context, b []byte) (n int, err error) {
// ReadAt should not affect nor be affected by the underlying seek offset.
// Clients of ReadAt can execute parallel ReadAt calls on the same input source.
func (f *FileBuffer) ReadAt(ctx context.Context, p []byte, off int64) (n int, err error) {
f.mu.RLock()
defer f.mu.RUnlock()
if f.isClosed {
return 0, os.ErrClosed
}
@ -131,6 +145,9 @@ func (f *FileBuffer) ReadAt(ctx context.Context, p []byte, off int64) (n int, er
// Write implements io.Writer https://golang.org/pkg/io/#Writer
// by appending the passed bytes to the buffer unless the buffer is closed or index negative.
func (f *FileBuffer) Write(ctx context.Context, p []byte) (n int, err error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.isClosed {
return 0, os.ErrClosed
}
@ -151,6 +168,9 @@ func (f *FileBuffer) Write(ctx context.Context, p []byte) (n int, err error) {
// Seek implements io.Seeker https://golang.org/pkg/io/#Seeker
func (f *FileBuffer) Seek(offset int64, whence int) (idx int64, err error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.isClosed {
return 0, os.ErrClosed
}
@ -176,6 +196,9 @@ func (f *FileBuffer) Seek(offset int64, whence int) (idx int64, err error) {
// Close implements io.Closer https://golang.org/pkg/io/#Closer
// It closes the buffer, rendering it unusable for I/O. It returns an error, if any.
func (f *FileBuffer) Close(ctx context.Context) error {
f.mu.Lock()
defer f.mu.Unlock()
f.isClosed = true
f.buff = nil
return nil