Compare commits

...

5 Commits
v0.0.1 ... main

Author SHA1 Message Date
jolheiser eefdce7b03 Add cache purge (#7)
continuous-integration/woodpecker the build was successful Details
Reviewed-on: #7
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-10-08 21:26:02 +00:00
jolheiser 99bf189237 Move to woodpecker (#6)
continuous-integration/woodpecker the build was successful Details
Reviewed-on: #6
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-10-08 20:55:54 +00:00
jolheiser 7017051b60 Replace build badge with docs badge (#3)
Reviewed-on: #3
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-05-09 04:06:23 +00:00
jolheiser 94a9e1f48c Add ReadFile, interface guard, and drone CI (#2)
Reviewed-on: #2
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-05-09 04:02:16 +00:00
jolheiser ea7687d011
Implement ReadDir
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2021-02-19 20:41:44 -06:00
6 changed files with 91 additions and 33 deletions

8
.woodpecker.yml 100644
View File

@ -0,0 +1,8 @@
pipeline:
compliance:
image: golang:1.17
commands:
- go test -race ./...
- go vet ./...
when:
event: [ pull_request ]

View File

@ -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=.

View File

@ -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

View File

@ -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

View File

@ -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,16 +48,29 @@ 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) {
return fs.ReadDir(f.fs, name)
}
// 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),
fs: fs,
root: root,
cache: make(map[string]bool),
}
for _, opt := range opts {
@ -60,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) {

View File

@ -2,7 +2,7 @@ package overlay
import (
"embed"
"io"
"io/fs"
"os"
"strings"
"testing"
@ -48,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()
@ -62,6 +62,48 @@ func TestOverlay(t *testing.T) {
}
}
func TestWalk(t *testing.T) {
expected := []string{"test1.txt", "test2.txt"}
x, err := New("_test/disk", embedded, WithSub("_test/embed"), WithCaching(false))
if err != nil {
t.Log(err)
t.FailNow()
}
found := make([]string, 0)
if err := fs.WalkDir(x, ".", func(walkPath string, walkEntry fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if walkEntry.IsDir() {
return nil
}
found = append(found, walkEntry.Name())
return nil
}); err != nil {
t.Log(err)
t.FailNow()
}
for _, e := range expected {
exists := false
for _, f := range found {
if e == f {
exists = true
break
}
}
if !exists {
t.Logf("could not find %s in %s\n", e, found)
t.Fail()
}
}
}
var emptyFS embed.FS
func TestInvalid(t *testing.T) {