layerfs

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2021 License: BSD-3-Clause Imports: 2 Imported by: 2

README

go-layerfs

Latest Release Build Status Coverage Status Go ReportCard GoDoc

This is a simple wrapper around multiple fs.FS instances, recursively merging them together dynamically.

If you have two directories, of which one is called examples/upper and the other examples/lower, you can layer upper over lower like this:

import (
	"os"
	"path/filepath"
	"github.com/dschmidt/go-layerfs"
)
upper, _ := filepath.Abs("examples/upper")
lower, _ := filepath.Abs("examples/lower")
fsys := layerfs.New(os.DirFS(upper), os.DirFS(lower))

If examples/upper looks like this

.
├── dir1
│   ├── f11.txt (content: foo)
│   └── f12.txt (content: foo)
├── f1.txt (content: foo)
└── f2.txt (content: foo)

and examples/lower looks like this

.
├── dir1
│   ├── f12.txt (content: bar)
│   └── f13.txt (content: bar)
├── f2.txt (content: bar)
└── f3.txt (content: bar)

then your fsys looks like this:

.
├── dir1
│   ├── f11.txt (content: foo)
│   ├── f12.txt (content: foo)
│   └── f13.txt (content: bar)
├── f1.txt (content: foo)
├── f2.txt (content: foo)
└── f3.txt (content: bar)

Example usage

You can run examples/file_server.go like this:

go run examples/file_server.go

2021/11/17 22:59:22 Listening on :8090...

Then requests via httpie should give you these results:

http GET http://localhost:8090/files
HTTP/1.1 200 OK
Content-Length: 123
Content-Type: text/html; charset=utf-8
Date: Wed, 17 Nov 2021 22:03:21 GMT
Last-Modified: Wed, 17 Nov 2021 21:55:53 GMT

<pre>
<a href="dir1/">dir1/</a>
<a href="f1.txt">f1.txt</a>
<a href="f2.txt">f2.txt</a>
<a href="f3.txt">f3.txt</a>
</pre>
http GET http://localhost:8090/files/f1.txt
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 3
Content-Type: text/plain; charset=utf-8
Date: Wed, 17 Nov 2021 22:05:29 GMT
Last-Modified: Wed, 17 Nov 2021 21:56:26 GMT

foo
http GET http://localhost:8090/files/f3.txt
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 3
Content-Type: text/plain; charset=utf-8
Date: Wed, 17 Nov 2021 22:05:56 GMT
Last-Modified: Wed, 17 Nov 2021 21:56:30 GMT

bar

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLayerForDirEntry

func GetLayerForDirEntry(d fs.DirEntry) (fs.FS, error)

GetLayerForDirEntry returns the source layer for a DirEntry.

Types

type DirEntry

type DirEntry struct {
	fs.DirEntry
	// contains filtered or unexported fields
}

DirEntry wraps a fs.DirEntry and keeps a reference to the source layer. (implements fs.DirEntry).

func (*DirEntry) GetFs

func (d *DirEntry) GetFs() fs.FS

GetFs returns the source layer.

func (*DirEntry) Info

func (d *DirEntry) Info() (fs.FileInfo, error)

Info returns an on-demand constructed FileInfo pointing to the source layer.

type DirFile

type DirFile struct {
	fs.File
	// contains filtered or unexported fields
}

DirFile wraps a fs.File and allows ReadDir to read entries from layerfs instead of the source layer dir. (implements fs.File and fs.ReadDirFile).

func (*DirFile) GetFs

func (f *DirFile) GetFs() fs.FS

GetFs returns the source layer.

func (*DirFile) ReadDir

func (f *DirFile) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir reads entries from the layerfs.

type FileInfo

type FileInfo struct {
	fs.FileInfo
	// contains filtered or unexported fields
}

FileInfo wraps a fs.FileInfo and keeps a reference to the source layer. (implements fs.FileInfo).

func (*FileInfo) GetFs

func (f *FileInfo) GetFs() fs.FS

GetFs returns the source layer.

type LayerFs

type LayerFs struct {
	// contains filtered or unexported fields
}

LayerFs implements several interfaces from io/fs and delegates function calls sequentially to the underlying layers until one does not return an error. If all layers return errors, LayerFs returns an error In case of ReadDir LayerFs merges the DirEntry instances returned by the underlying layers.

func New

func New(layers ...fs.FS) *LayerFs

New creates a new LayerFs instance based on 0-n fs.FS layers.

func (*LayerFs) Open

func (fsys *LayerFs) Open(name string) (fs.File, error)

Open opens the named file (implements fs.FS).

func (*LayerFs) ReadDir

func (fsys *LayerFs) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads the named directory (implements fs.ReadDirFS).

func (*LayerFs) ReadFile

func (fsys *LayerFs) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns its contents (implements fs.ReadFileFS).

func (*LayerFs) Stat

func (fsys *LayerFs) Stat(name string) (fs.FileInfo, error)

Stat returns a FileInfo describing the file (implements fs.StatFS).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL