Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
jolheiser | eefdce7b03 | |
jolheiser | 99bf189237 | |
jolheiser | 7017051b60 | |
jolheiser | 94a9e1f48c |
|
@ -0,0 +1,8 @@
|
||||||
|
pipeline:
|
||||||
|
compliance:
|
||||||
|
image: golang:1.17
|
||||||
|
commands:
|
||||||
|
- go test -race ./...
|
||||||
|
- go vet ./...
|
||||||
|
when:
|
||||||
|
event: [ pull_request ]
|
17
Makefile
17
Makefile
|
@ -1,17 +0,0 @@
|
||||||
GO ?= go
|
|
||||||
|
|
||||||
.PHONY: vet
|
|
||||||
vet:
|
|
||||||
$(GO) vet ./...
|
|
||||||
|
|
||||||
.PHONY: fmt
|
|
||||||
fmt:
|
|
||||||
$(GO) fmt ./...
|
|
||||||
|
|
||||||
.PHONY: test
|
|
||||||
test:
|
|
||||||
$(GO) test -race ./...
|
|
||||||
|
|
||||||
.PHONY: bench
|
|
||||||
bench:
|
|
||||||
$(GO) test -benchmem -bench=.
|
|
|
@ -1,4 +1,7 @@
|
||||||
# Overlay
|
# Overlay
|
||||||
|
|
||||||
|
[![Go Reference](https://pkg.go.dev/badge/go.jolheiser.com/overlay.svg)](https://pkg.go.dev/go.jolheiser.com/overlay)
|
||||||
|
|
||||||
Overlay File System
|
Overlay File System
|
||||||
|
|
||||||
Overlay is an easy way to implement a file system in such a way that
|
Overlay is an easy way to implement a file system in such a way that
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
go test -benchmem -bench=.
|
|
||||||
goos: linux
|
|
||||||
goarch: amd64
|
|
||||||
pkg: go.jolheiser.com/overlay
|
|
||||||
cpu: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
|
|
||||||
BenchmarkCache-8 134959974 9.003 ns/op 0 B/op 0 allocs/op
|
|
||||||
BenchmarkNoCache-8 897212 1369 ns/op 280 B/op 4 allocs/op
|
|
||||||
PASS
|
|
||||||
ok go.jolheiser.com/overlay 3.360s
|
|
35
overlay.go
35
overlay.go
|
@ -6,6 +6,10 @@ import (
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Interface guard
|
||||||
|
// fs.ReadFileFS also fulfills fs.FS
|
||||||
|
var _ fs.ReadFileFS = (*FS)(nil)
|
||||||
|
|
||||||
// FS is an overlay File System
|
// FS is an overlay File System
|
||||||
type FS struct {
|
type FS struct {
|
||||||
fs fs.FS
|
fs fs.FS
|
||||||
|
@ -14,12 +18,17 @@ type FS struct {
|
||||||
cache map[string]bool
|
cache map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PurgeCache purges the cache
|
||||||
|
func (f *FS) PurgeCache() {
|
||||||
|
f.cache = make(map[string]bool)
|
||||||
|
}
|
||||||
|
|
||||||
func (f *FS) apn(name string) string {
|
func (f *FS) apn(name string) string {
|
||||||
return path.Join(f.root, name)
|
return path.Join(f.root, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FS) exists(name string) bool {
|
func (f *FS) exists(name string) bool {
|
||||||
if has, ok := f.cache[name]; ok && f.doCache {
|
if has, ok := f.cache[name]; ok {
|
||||||
return has
|
return has
|
||||||
}
|
}
|
||||||
_, err := os.Stat(f.apn(name))
|
_, err := os.Stat(f.apn(name))
|
||||||
|
@ -39,6 +48,14 @@ func (f *FS) Open(name string) (fs.File, error) {
|
||||||
return f.fs.Open(name)
|
return f.fs.Open(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
// ReadDir reads []fs.DirEntry
|
// ReadDir reads []fs.DirEntry
|
||||||
// This method will prefer EMBEDDED, because that is the "real" FS for overlay
|
// This method will prefer EMBEDDED, because that is the "real" FS for overlay
|
||||||
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
||||||
|
@ -51,10 +68,9 @@ type Option func(*FS) error
|
||||||
// New returns a new FS
|
// New returns a new FS
|
||||||
func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
|
func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
|
||||||
x := &FS{
|
x := &FS{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
root: root,
|
root: root,
|
||||||
doCache: true,
|
cache: make(map[string]bool),
|
||||||
cache: make(map[string]bool),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
@ -66,6 +82,15 @@ func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
|
||||||
return x, nil
|
return x, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// WithSub sets a fs.Sub for an FS
|
// WithSub sets a fs.Sub for an FS
|
||||||
func WithSub(sub string) Option {
|
func WithSub(sub string) Option {
|
||||||
return func(x *FS) (err error) {
|
return func(x *FS) (err error) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"io"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -49,7 +48,7 @@ func TestOverlay(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer fi.Close()
|
defer fi.Close()
|
||||||
|
|
||||||
contents, err := io.ReadAll(fi)
|
contents, err := x.ReadFile(tc.File)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
|
Loading…
Reference in New Issue