Compare commits
No commits in common. "main" and "v0.0.2" have entirely different histories.
|
@ -1,8 +0,0 @@
|
||||||
pipeline:
|
|
||||||
compliance:
|
|
||||||
image: golang:1.17
|
|
||||||
commands:
|
|
||||||
- go test -race ./...
|
|
||||||
- go vet ./...
|
|
||||||
when:
|
|
||||||
event: [ pull_request ]
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
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,7 +1,4 @@
|
||||||
# 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
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
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
|
29
overlay.go
29
overlay.go
|
@ -6,10 +6,6 @@ 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
|
||||||
|
@ -18,17 +14,12 @@ 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 {
|
if has, ok := f.cache[name]; ok && f.doCache {
|
||||||
return has
|
return has
|
||||||
}
|
}
|
||||||
_, err := os.Stat(f.apn(name))
|
_, err := os.Stat(f.apn(name))
|
||||||
|
@ -48,14 +39,6 @@ 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) {
|
||||||
|
@ -70,6 +53,7 @@ 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),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,15 +66,6 @@ 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,6 +2,7 @@ package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -48,7 +49,7 @@ func TestOverlay(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer fi.Close()
|
defer fi.Close()
|
||||||
|
|
||||||
contents, err := x.ReadFile(tc.File)
|
contents, err := io.ReadAll(fi)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
|
Loading…
Reference in New Issue