Skip to content

Commit

Permalink
Support new VFS interface for pebble v1.0.0 (lni#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerwilliams authored Dec 21, 2023
1 parent d1f9159 commit d43d3c6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dir_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ import (

func (defaultFS) OpenDir(name string) (File, error) {
f, err := os.OpenFile(name, syscall.O_CLOEXEC, 0)
return f, errors.WithStack(err)
return &fileCompat{f}, errors.WithStack(err)
}
26 changes: 26 additions & 0 deletions error_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,29 @@ func (f *errorFile) Sync() error {
}
return f.file.Sync()
}

func (f *errorFile) Preallocate(offset, length int64) error {
return nil
}

func (f *errorFile) SyncTo(length int64) (fullSync bool, err error) {
return true, f.Sync()
}

func (f *errorFile) SyncData() error {
return f.Sync()
}

func (f *errorFile) Prefetch(offset int64, length int64) error {
return nil
}

func (f *errorFile) Fd() uintptr {
type fd interface {
Fd() uintptr
}
if d, ok := f.file.(fd); ok {
return d.Fd()
}
return InvalidFd
}
20 changes: 20 additions & 0 deletions mem_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,23 @@ func (f *memFile) Sync() error {
}
return nil
}

func (f *memFile) Preallocate(offset, length int64) error {
return nil
}

func (f *memFile) SyncTo(length int64) (fullSync bool, err error) {
return true, f.Sync()
}

func (f *memFile) SyncData() error {
return f.Sync()
}

func (f *memFile) Prefetch(offset int64, length int64) error {
return nil
}

func (f *memFile) Fd() uintptr {
return InvalidFd
}
23 changes: 23 additions & 0 deletions syncing_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,26 @@ func (f *syncingFile) Close() error {
}
return f.File.Close()
}

func (f *syncingFile) Preallocate(offset, length int64) error {
return nil
}

func (f *syncingFile) SyncTo(length int64) (fullSync bool, err error) {
return true, f.Sync()
}

func (f *syncingFile) SyncData() error {
return f.Sync()
}

func (f *syncingFile) Prefetch(offset int64, length int64) error {
return nil
}

func (f *syncingFile) Fd() uintptr {
if f.fd != 0 {
return f.fd
}
return InvalidFd
}
42 changes: 37 additions & 5 deletions vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type File interface {
io.WriterAt
Stat() (os.FileInfo, error)
Sync() error
Preallocate(offset, length int64) error
SyncTo(length int64) (fullSync bool, err error)
SyncData() error
Prefetch(offset int64, length int64) error
Fd() uintptr
}

// OpenOption provide an interface to do work on file handles in the Open()
Expand Down Expand Up @@ -137,6 +142,32 @@ type DiskUsage struct {
UsedBytes uint64
}

const InvalidFd uintptr = ^(uintptr(0))

type fileCompat struct {
*os.File
}

func (f *fileCompat) Preallocate(offset, length int64) error {
return nil
}

func (f *fileCompat) SyncTo(length int64) (fullSync bool, err error) {
return true, f.Sync()
}

func (f *fileCompat) SyncData() error {
return f.Sync()
}

func (f *fileCompat) Prefetch(offset int64, length int64) error {
return nil
}

func (f *fileCompat) Fd() uintptr {
return f.File.Fd()
}

// Default is a FS implementation backed by the underlying operating system's
// file system.
var Default FS = defaultFS{}
Expand All @@ -145,7 +176,7 @@ type defaultFS struct{}

func (defaultFS) Create(name string) (File, error) {
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC|syscall.O_CLOEXEC, 0666)
return f, errors.WithStack(err)
return &fileCompat{f}, errors.WithStack(err)
}

func (defaultFS) Link(oldname, newname string) error {
Expand All @@ -154,18 +185,19 @@ func (defaultFS) Link(oldname, newname string) error {

func (defaultFS) Open(name string, opts ...OpenOption) (File, error) {
file, err := os.OpenFile(name, os.O_RDONLY|syscall.O_CLOEXEC, 0)
f := &fileCompat{file}
if err != nil {
return nil, errors.WithStack(err)
}
for _, opt := range opts {
opt.Apply(file)
opt.Apply(f)
}
return file, nil
return f, nil
}

func (defaultFS) OpenForAppend(name string) (File, error) {
f, err := os.OpenFile(name, os.O_RDWR|os.O_APPEND|syscall.O_CLOEXEC, 0)
return f, errors.WithStack(err)
return &fileCompat{f}, errors.WithStack(err)
}

func (defaultFS) Remove(name string) error {
Expand All @@ -185,7 +217,7 @@ func (fs defaultFS) ReuseForWrite(oldname, newname string) (File, error) {
return nil, errors.WithStack(err)
}
f, err := os.OpenFile(newname, os.O_RDWR|os.O_CREATE|syscall.O_CLOEXEC, 0666)
return f, errors.WithStack(err)
return &fileCompat{f}, errors.WithStack(err)
}

func (defaultFS) MkdirAll(dir string, perm os.FileMode) error {
Expand Down

0 comments on commit d43d3c6

Please sign in to comment.