Documentation
¶
Overview ¶
Package afero exports compactfs directly to Go applications as an afero.Fs. It owns its adapter and file handles, not the supplied shared storage engine.
Index ¶
- type FS
- func (f *FS) Chmod(name string, mode os.FileMode) error
- func (f *FS) Chown(name string, uid, gid int) error
- func (f *FS) Chtimes(name string, atime, mtime time.Time) error
- func (f *FS) Close() error
- func (f *FS) Create(name string) (aferofs.File, error)
- func (f *FS) Mkdir(name string, perm os.FileMode) error
- func (f *FS) MkdirAll(name string, perm os.FileMode) error
- func (*FS) Name() string
- func (f *FS) Open(name string) (aferofs.File, error)
- func (f *FS) OpenFile(name string, flag int, perm os.FileMode) (aferofs.File, error)
- func (f *FS) Remove(name string) error
- func (f *FS) RemoveAll(name string) error
- func (f *FS) Rename(oldname, newname string) error
- func (f *FS) Stat(name string) (os.FileInfo, error)
- func (f *FS) Sync(ctx context.Context) error
- type File
- func (f *File) Close() error
- func (f *File) Name() string
- func (f *File) Read(data []byte) (int, error)
- func (f *File) ReadAt(data []byte, off int64) (int, error)
- func (f *File) ReadDir(count int) ([]fs.DirEntry, error)
- func (f *File) Readdir(count int) ([]os.FileInfo, error)
- func (f *File) Readdirnames(count int) ([]string, error)
- func (f *File) Seek(offset int64, whence int) (int64, error)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Sync() error
- func (f *File) Truncate(size int64) error
- func (f *File) Write(data []byte) (int, error)
- func (f *File) WriteAt(data []byte, off int64) (int, error)
- func (f *File) WriteString(value string) (int, error)
- type Options
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 implements afero.Fs without a FUSE mount, kernel device or daemon. Use one FS per core.FileSystem, and share it (or afero wrappers around it) in the app. Relative and absolute paths address the same virtual root, not the host cwd.
func New ¶
func New(storage *core.FileSystem, options ...Options) (*FS, error)
New adapts an existing storage engine. It accepts at most one Options value; omitting it selects a background context and local-cache durability.
Example ¶
package main
import (
"context"
"fmt"
"github.com/lfhy/compactfs/codec"
compactafero "github.com/lfhy/compactfs/export/afero"
"github.com/lfhy/compactfs/fs/backend"
"github.com/lfhy/compactfs/fs/chunk"
"github.com/lfhy/compactfs/fs/core"
"github.com/lfhy/compactfs/fs/meta"
"github.com/spf13/afero"
)
func main() {
ctx := context.Background()
metadata := meta.NewMemoryEngine()
namespace, err := meta.OpenNamespace(ctx, metadata)
if err != nil {
panic(err)
}
algorithm, err := codec.NewGzip(1)
if err != nil {
panic(err)
}
storage, err := core.New(core.Config{Namespace: namespace,
Chunks: &chunk.Store{Backend: backend.NewMemory(), Codec: algorithm}})
if err != nil {
panic(err)
}
view, err := compactafero.New(storage)
if err != nil {
panic(err)
}
if err := view.MkdirAll("documents", 0o750); err != nil {
panic(err)
}
if err := afero.WriteFile(view, "documents/hello.txt", []byte("hello compactfs"), 0o640); err != nil {
panic(err)
}
data, err := afero.ReadFile(view, "documents/hello.txt")
if err != nil {
panic(err)
}
fmt.Println(string(data))
if err := view.Close(); err != nil {
panic(err)
}
if err := storage.Close(); err != nil {
panic(err)
}
if err := metadata.Close(); err != nil {
panic(err)
}
}
Output: hello compactfs
func (*FS) Close ¶
Close invalidates adapter handles and synchronizes the configured durability boundary. It does not close the supplied engine, caches, metadata or worker. Failed syncs remain retryable by calling Close again.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File is an application handle. Offsets and directory cursors belong to the handle; mutable file content belongs to the shared inode in FS.
type Options ¶
type Options struct {
// Context is used for interface methods which cannot accept a context.
// Nil means context.Background. Close the adapter before cancelling it.
Context context.Context
// SyncBackend makes File.Sync, File.Close and FS.Close wait for backend
// persistence. The default confirms local raw data and metadata when a raw
// cache is configured; without that cache all syncs wait for the backend.
SyncBackend bool
}
Options controls the lifetime context and durability boundary of the adapter. Configure encoded, raw writeback and decoded caches in core.Config before New; the adapter shares those caches and does not own or reconfigure the engine.