implemented os file interface acstraction
This commit is contained in:
parent
23528fe28e
commit
ad41ddbe54
11 changed files with 223 additions and 92 deletions
45
pkg/provider/interface.go
Normal file
45
pkg/provider/interface.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright (C) 2022 Marius Schellenberger
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
type FS interface {
|
||||
Stat(string) (fs.FileInfo, error)
|
||||
Remove(string) error
|
||||
Open(string) (File, error)
|
||||
OpenFile(string, int, os.FileMode) (File, error)
|
||||
MkdirAll(string, os.FileMode) error
|
||||
Root() string
|
||||
Fstat(uintptr) (int64, error)
|
||||
}
|
||||
|
||||
type walk struct {
|
||||
fs FS
|
||||
}
|
||||
|
||||
func (w *walk) Open(p string) (fs.File, error) {
|
||||
return w.fs.Open(p)
|
||||
}
|
||||
|
||||
func WalkFS(fs FS) fs.FS {
|
||||
return &walk{fs}
|
||||
}
|
||||
|
||||
type File interface {
|
||||
Name() string
|
||||
Stat() (fs.FileInfo, error)
|
||||
Truncate(int64) error
|
||||
Sync() error
|
||||
Fd() uintptr
|
||||
ReadDir(int) ([]fs.DirEntry, error)
|
||||
Read([]byte) (int, error)
|
||||
ReadAt([]byte, int64) (int, error)
|
||||
WriteAt([]byte, int64) (int, error)
|
||||
Seek(int64, int) (int64, error)
|
||||
Readdir(int) ([]fs.FileInfo, error)
|
||||
Close() error
|
||||
}
|
||||
57
pkg/provider/mount/mount.go
Normal file
57
pkg/provider/mount/mount.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright (C) 2022 Marius Schellenberger
|
||||
|
||||
package mount
|
||||
|
||||
import (
|
||||
"cachefs/pkg/provider"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ provider.FS = (*FS)(nil)
|
||||
|
||||
type FS struct {
|
||||
root string
|
||||
}
|
||||
|
||||
func NewFS(root string) *FS {
|
||||
return &FS{root}
|
||||
}
|
||||
|
||||
func (fs *FS) path(p string) string {
|
||||
return filepath.Join(fs.root, filepath.FromSlash(path.Clean("/"+p)))
|
||||
}
|
||||
|
||||
func (fs *FS) Stat(p string) (fs.FileInfo, error) {
|
||||
return os.Stat(fs.path(p))
|
||||
}
|
||||
|
||||
func (fs *FS) Remove(p string) error {
|
||||
return os.Remove(fs.path(p))
|
||||
}
|
||||
|
||||
func (fs *FS) Open(p string) (provider.File, error) {
|
||||
return os.Open(fs.path(p))
|
||||
}
|
||||
|
||||
func (fs *FS) OpenFile(p string, flags int, mode os.FileMode) (provider.File, error) {
|
||||
return os.OpenFile(fs.path(p), flags, mode)
|
||||
}
|
||||
|
||||
func (fs *FS) MkdirAll(p string, mode os.FileMode) error {
|
||||
return os.MkdirAll(fs.path(p), mode)
|
||||
}
|
||||
|
||||
func (fs *FS) Root() string {
|
||||
return fs.root
|
||||
}
|
||||
|
||||
func (fs *FS) Fstat(fd uintptr) (int64, error) {
|
||||
var stat unix.Stat_t
|
||||
err := unix.Fstat(int(fd), &stat)
|
||||
return stat.Blocks, err
|
||||
}
|
||||
34
pkg/provider/parse/parse.go
Normal file
34
pkg/provider/parse/parse.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright (C) 2022 Marius Schellenberger
|
||||
|
||||
package parse
|
||||
|
||||
import (
|
||||
"cachefs/pkg/provider"
|
||||
"cachefs/pkg/provider/mount"
|
||||
"errors"
|
||||
"fmt"
|
||||
neturl "net/url"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPathNotAbsolute = errors.New("path is not absolute")
|
||||
ErrUnsupportedScheme = errors.New("unsupported scheme")
|
||||
)
|
||||
|
||||
func FS(url string) (provider.FS, error) {
|
||||
u, err := neturl.Parse(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !filepath.IsAbs(u.Path) {
|
||||
return nil, ErrPathNotAbsolute
|
||||
}
|
||||
switch u.Scheme {
|
||||
case "file":
|
||||
return mount.NewFS(u.Path), nil
|
||||
//case "sftp":
|
||||
// return sftp.FS(u.Path), nil
|
||||
}
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnsupportedScheme, u.Scheme)
|
||||
}
|
||||
5
pkg/provider/sftp/sftp.go
Normal file
5
pkg/provider/sftp/sftp.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// Copyright (C) 2022 Marius Schellenberger
|
||||
|
||||
package sftp
|
||||
|
||||
// github.com/pkg/sftp
|
||||
Loading…
Add table
Add a link
Reference in a new issue