2021-02-18 04:39:52 +00:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2021-05-09 03:54:36 +00:00
|
|
|
// Interface guard
|
|
|
|
// fs.ReadFileFS also fulfills fs.FS
|
|
|
|
var _ fs.ReadFileFS = (*FS)(nil)
|
|
|
|
|
2021-02-18 04:39:52 +00:00
|
|
|
// FS is an overlay File System
|
|
|
|
type FS struct {
|
|
|
|
fs fs.FS
|
|
|
|
root string
|
|
|
|
doCache bool
|
|
|
|
cache map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FS) apn(name string) string {
|
|
|
|
return path.Join(f.root, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FS) exists(name string) bool {
|
|
|
|
if has, ok := f.cache[name]; ok && f.doCache {
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
_, err := os.Stat(f.apn(name))
|
|
|
|
if err != nil {
|
|
|
|
f.cache[name] = false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
f.cache[name] = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens an fs.File, preferring disk
|
|
|
|
func (f *FS) Open(name string) (fs.File, error) {
|
|
|
|
if f.exists(name) {
|
|
|
|
return os.Open(f.apn(name))
|
|
|
|
}
|
|
|
|
return f.fs.Open(name)
|
|
|
|
}
|
|
|
|
|
2021-05-09 03:54:36 +00:00
|
|
|
// ReadFile reads a file, preferring disk
|
|
|
|
func (f *FS) ReadFile(name string) ([]byte, error) {
|
|
|
|
if f.exists(name) {
|
|
|
|
return os.ReadFile(f.apn(name))
|
|
|
|
}
|
|
|
|
return fs.ReadFile(f.fs, name)
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:41:44 +00:00
|
|
|
// ReadDir reads []fs.DirEntry
|
|
|
|
// This method will prefer EMBEDDED, because that is the "real" FS for overlay
|
|
|
|
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
|
|
|
return fs.ReadDir(f.fs, name)
|
|
|
|
}
|
|
|
|
|
2021-02-18 04:39:52 +00:00
|
|
|
// Option is a functional option for an FS
|
|
|
|
type Option func(*FS) error
|
|
|
|
|
|
|
|
// New returns a new FS
|
|
|
|
func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
|
|
|
|
x := &FS{
|
|
|
|
fs: fs,
|
|
|
|
root: root,
|
|
|
|
doCache: true,
|
|
|
|
cache: make(map[string]bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(x); err != nil {
|
|
|
|
return x, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
|
2021-05-09 03:54:36 +00:00
|
|
|
// MustNew returns New and panics on error
|
|
|
|
func MustNew(root string, fs fs.FS, opts ...Option) *FS {
|
|
|
|
f, err := New(root, fs, opts...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2021-02-18 04:39:52 +00:00
|
|
|
// WithSub sets a fs.Sub for an FS
|
|
|
|
func WithSub(sub string) Option {
|
|
|
|
return func(x *FS) (err error) {
|
|
|
|
x.fs, err = fs.Sub(x.fs, sub)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCaching sets a caching mode for an FS
|
|
|
|
// Caching avoids subsequent os.Stat to determine if a file exists on disk
|
|
|
|
// See bench.txt for differences in usage
|
|
|
|
func WithCaching(doCache bool) Option {
|
|
|
|
return func(x *FS) error {
|
|
|
|
x.doCache = doCache
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|