overlayfs

package module
v0.1.1 Latest Latest
Warning

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

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

README

overlayfs

Build status GoDoc

overlayfs is a simple package to overlay io/fs.FS values for Go.

Example

func ExampleFrom() {
	fs1 := fstest.MapFS{
		"dir1/dir00": &fstest.MapFile{
			Mode: fs.ModeDir,
		},
		"dir1/dir11/f111.txt": &fstest.MapFile{
			Data: []byte(`hello 1`),
			Mode: 0644,
		},
		"dir1/dir11/f112.txt": &fstest.MapFile{
			Data: []byte(`hello 2`),
			Mode: 0644,
		},
	}
	fs2 := fstest.MapFS{
		"dir1/dir22": &fstest.MapFile{
			Mode: fs.ModeDir,
		},
		"dir1/dir11/f111.txt": &fstest.MapFile{
			Data: []byte(`hello 1 from f2`),
			Mode: 0644,
		},
		"dir1/dir11/f113.txt": &fstest.MapFile{
			Data: []byte(`hello 3`),
			Mode: 0644,
		},
	}

	ovfs := overlayfs.From(fs1, fs2)

	f1, err := fs.ReadFile(ovfs, "dir1/dir11/f111.txt")
	if err != nil {
		log.Fatalf("could not read file: %v", err)
	}
	fmt.Printf("f1: %q\n", f1)

	f2, err := fs.ReadFile(ovfs, "dir1/dir11/f112.txt")
	if err != nil {
		log.Fatalf("could not read file: %v", err)
	}
	fmt.Printf("f2: %q\n", f2)

	f3, err := fs.ReadFile(ovfs, "dir1/dir11/f113.txt")
	if err != nil {
		log.Fatalf("could not read file: %v", err)
	}
	fmt.Printf("f3: %q\n", f3)

	// Output:
	// f1: "hello 1 from f2"
	// f2: "hello 2"
	// f3: "hello 3"
}```

## License

- `overlayfs` is released under the `BSD-3` license.

Documentation

Overview

Package overlyafs provides a way to overlay multiple fs.FS.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FS

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

FS overlays multiple fs.FS.

func From

func From(stack ...fs.FS) FS

From creates a new overlay fs from the provided stack of FS. FS contents are considered in the reverse order of the provided slice. ie: the last stacked layer wins.

Example
package main

import (
	"fmt"
	"io/fs"
	"log"
	"testing/fstest"

	"git.sr.ht/~sbinet/overlayfs"
)

func main() {
	fs1 := fstest.MapFS{
		"dir1/dir00": &fstest.MapFile{
			Mode: fs.ModeDir,
		},
		"dir1/dir11/f111.txt": &fstest.MapFile{
			Data: []byte(`hello 1`),
			Mode: 0644,
		},
		"dir1/dir11/f112.txt": &fstest.MapFile{
			Data: []byte(`hello 2`),
			Mode: 0644,
		},
	}
	fs2 := fstest.MapFS{
		"dir1/dir22": &fstest.MapFile{
			Mode: fs.ModeDir,
		},
		"dir1/dir11/f111.txt": &fstest.MapFile{
			Data: []byte(`hello 1 from f2`),
			Mode: 0644,
		},
		"dir1/dir11/f113.txt": &fstest.MapFile{
			Data: []byte(`hello 3`),
			Mode: 0644,
		},
	}

	ovfs := overlayfs.From(fs1, fs2)

	f1, err := fs.ReadFile(ovfs, "dir1/dir11/f111.txt")
	if err != nil {
		log.Fatalf("could not read file: %v", err)
	}
	fmt.Printf("f1: %q\n", f1)

	f2, err := fs.ReadFile(ovfs, "dir1/dir11/f112.txt")
	if err != nil {
		log.Fatalf("could not read file: %v", err)
	}
	fmt.Printf("f2: %q\n", f2)

	f3, err := fs.ReadFile(ovfs, "dir1/dir11/f113.txt")
	if err != nil {
		log.Fatalf("could not read file: %v", err)
	}
	fmt.Printf("f3: %q\n", f3)

}
Output:

f1: "hello 1 from f2"
f2: "hello 2"
f3: "hello 3"

func (FS) Open

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

Open opens the named file.

Open tries each overlaid filesystem in-turn, in the reverse order they were overlaid. ie: the last stacked layer wins.

func (FS) Sub

func (fsys FS) Sub(dir string) (fs.FS, error)

Sub returns an FS corresponding to the subtree rooted at dir.

Sub tries each overlaid filesystem in-turn, in the reverse order they were overlaid. ie: the last stacked layer wins.

Jump to

Keyboard shortcuts

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