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
|
||||
|
||||
[![Go Reference](https://pkg.go.dev/badge/go.jolheiser.com/overlay.svg)](https://pkg.go.dev/go.jolheiser.com/overlay)
|
||||
|
||||
Overlay File System
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// Interface guard
|
||||
// fs.ReadFileFS also fulfills fs.FS
|
||||
var _ fs.ReadFileFS = (*FS)(nil)
|
||||
|
||||
// FS is an overlay File System
|
||||
type FS struct {
|
||||
fs fs.FS
|
||||
|
@ -14,12 +18,17 @@ type FS struct {
|
|||
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 {
|
||||
return path.Join(f.root, name)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
_, err := os.Stat(f.apn(name))
|
||||
|
@ -39,6 +48,14 @@ func (f *FS) Open(name string) (fs.File, error) {
|
|||
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
|
||||
// This method will prefer EMBEDDED, because that is the "real" FS for overlay
|
||||
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
||||
|
@ -51,10 +68,9 @@ 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),
|
||||
fs: fs,
|
||||
root: root,
|
||||
cache: make(map[string]bool),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
|
@ -66,6 +82,15 @@ func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
|
|||
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
|
||||
func WithSub(sub string) Option {
|
||||
return func(x *FS) (err error) {
|
||||
|
|
|
@ -2,7 +2,6 @@ package overlay
|
|||
|
||||
import (
|
||||
"embed"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
|
@ -49,7 +48,7 @@ func TestOverlay(t *testing.T) {
|
|||
}
|
||||
defer fi.Close()
|
||||
|
||||
contents, err := io.ReadAll(fi)
|
||||
contents, err := x.ReadFile(tc.File)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
|
|
Loading…
Reference in New Issue