Documentation
¶
Overview ¶
Package erofs reads and creates EROFS filesystem images.
Reading ¶
Use Open to read an existing EROFS image through Go's standard fs.FS interface:
img, err := erofs.Open(f) data, err := fs.ReadFile(img, "etc/hostname")
Writing ¶
Use Create to build a new EROFS image. Entries can be added one at a time, or bulk-copied from any fs.FS via Writer.CopyFrom:
w := erofs.Create(outFile) w.CopyFrom(srcFS) w.Close()
For metadata-only images that reference data in an external source (e.g. for container layer indexing), pass MetadataOnly to CopyFrom:
w := erofs.Create(outFile) w.CopyFrom(srcFS, erofs.MetadataOnly()) w.Close()
Index ¶
- Variables
- func EroFS(r io.ReaderAt, opts ...Opt) (fs.FS, error)deprecated
- func Open(r io.ReaderAt, opts ...OpenOpt) (fs.FS, error)
- type CopyOpt
- type CreateOpt
- type File
- type OpenOpt
- type Optdeprecated
- type Stat
- type Writer
- func (fsys *Writer) Chmod(name string, mode fs.FileMode) error
- func (fsys *Writer) Chown(name string, uid, gid int) error
- func (fsys *Writer) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (fsys *Writer) Close() error
- func (fsys *Writer) CopyFrom(src fs.FS, opts ...CopyOpt) error
- func (fsys *Writer) Create(name string) (*File, error)
- func (fsys *Writer) Mkdir(name string, perm fs.FileMode) error
- func (fsys *Writer) Mknod(name string, mode uint16, rdev uint32) error
- func (fsys *Writer) Open(name string) (fs.File, error)
- func (fsys *Writer) SetNlink(name string, nlink uint32) error
- func (fsys *Writer) Setxattr(name, attr, value string) error
- func (fsys *Writer) Stat(name string) (fs.FileInfo, error)
- func (fsys *Writer) Symlink(oldname, newname string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalid occurs when an invalid value is detected in the erofs data. // Whether this invalid data is the result of corruption or bad input // is up to the caller to decide. // This error may be wrapped with more details. ErrInvalid = fs.ErrInvalid // ErrInvalidSuperblock occurs when the super block could not be validated // when initially loading the erofs input. Unlike other corruption cases, // invalid super block should be returned immediately ErrInvalidSuperblock = fmt.Errorf("invalid super block: %w", ErrInvalid) // ErrNotImplemented is returned when a feature is known but not implemented // yet by this library ErrNotImplemented = errors.New("not implemented") // ErrNotDirectory is returned when a path component is not a directory. ErrNotDirectory = errors.New("not a directory") // ErrIsDirectory is returned when an operation expected a file but found // a directory. ErrIsDirectory = errors.New("is a directory") // ErrLoop is returned when too many symlinks are encountered during // path resolution. ErrLoop = fmt.Errorf("too many symlinks: %w", ErrInvalid) )
Errors
Functions ¶
func Open ¶ added in v0.2.0
Open returns a FileSystem reading from the given ReaderAt. The ReaderAt must be a valid EROFS block file. No additional memory mapping is done and must be handled by the caller.
Example ¶
package main
import (
"fmt"
"io/fs"
"log"
"os"
erofs "github.com/erofs/go-erofs"
)
func main() {
f, err := os.Open("testdata/basic-default.erofs")
if err != nil {
log.Fatal(err)
}
img, err := erofs.Open(f)
if err != nil {
_ = f.Close()
log.Fatal(err)
}
defer func() { _ = f.Close() }()
_ = fs.WalkDir(img, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
fmt.Println(path)
return nil
})
}
Output:
Types ¶
type CopyOpt ¶ added in v0.3.0
type CopyOpt func(*Writer)
CopyOpt configures a CopyFrom operation.
func Merge ¶ added in v0.3.0
func Merge() CopyOpt
Merge enables overlay merge semantics for the current CopyFrom. AUFS-style whiteout files (.wh.<name>) delete the named entry from prior layers, and opaque markers (.wh..wh..opq) delete all children of their parent directory. The whiteout entries themselves are not added to the image.
When using Merge with a source containing AUFS whiteout files, do not pre-convert them; the Writer processes raw whiteout entries directly.
func MetadataOnly ¶ added in v0.3.0
func MetadataOnly() CopyOpt
MetadataOnly configures the current CopyFrom to emit only metadata. Regular files with pre-existing chunk mappings use chunk-based layout referencing an external device; file data is not copied.
type CreateOpt ¶ added in v0.3.0
type CreateOpt func(*createOptions)
CreateOpt configures EROFS image creation.
func WithBuildTime ¶ added in v0.3.0
WithBuildTime sets the filesystem build timestamp.
func WithDataFile ¶ added in v0.3.0
WithDataFile sets an external data file for metadata-only mode. File.Write appends to this file at block-aligned offsets; chunk indexes reference those blocks with DeviceID=1.
func WithTempDir ¶ added in v0.3.0
WithTempDir overrides the temp directory for the spool file. Only used when no data file is provided.
type File ¶ added in v0.3.0
type File struct {
// contains filtered or unexported fields
}
File is a writable regular file returned by Writer.Create. Data is written via Write or ReadFrom, then committed with Close.
func (*File) Chmod ¶ added in v0.3.0
Chmod sets permission bits on the file, matching os.File.Chmod.
func (*File) Chown ¶ added in v0.3.0
Chown sets the owner UID and GID on the file, matching os.File.Chown.
func (*File) Close ¶ added in v0.3.0
Close commits the file entry. For data file mode, pads to block boundary and records chunk indexes.
type OpenOpt ¶ added in v0.2.0
type OpenOpt func(*options)
OpenOpt is an option for configuring the EROFS reader
func WithExtraDevices ¶
WithExtraDevices specifies additional devices to read chunk data from
type Stat ¶
type Stat struct {
Mode fs.FileMode
Size int64
InodeLayout uint8
Rdev uint32
Ino int64
UID uint32
GID uint32
Mtime uint64
MtimeNs uint32
Nlink int
Xattrs map[string]string
}
Stat is the raw erofs stat data returned by Sys() on fs.FileInfo values. It is a plain data struct analogous to syscall.Stat_t.
For cross-platform fs.FS compatibility, callers should prefer type-asserting the fs.FileInfo to accessor interfaces rather than inspecting Stat fields directly. The returned fs.FileInfo implements the following single-method interfaces:
Ownership: UID() uint32, GID() uint32 InodeInfo: Ino() uint64, Nlink() uint64 DeviceInfo: Rdev() uint64 Xattrs: GetAllXattr() map[string]string, GetXattr(string) (string, bool)
type Writer ¶ added in v0.3.0
type Writer struct {
// contains filtered or unexported fields
}
Writer is a writable filesystem that produces an EROFS image on Close. Files are added via Create, Mkdir, Symlink, and Mknod, then finalized by calling Close which serializes the complete EROFS image.
func Create ¶ added in v0.3.0
func Create(out io.WriteSeeker, opts ...CreateOpt) *Writer
Create returns a Writer that produces an EROFS image on Close. Options configure build time, data file, and temp directory.
Example ¶
var buf testBuffer
w := erofs.Create(&buf)
f, err := w.Create("/hello.txt")
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte("hello world\n")); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
if err := w.Mkdir("/dir", 0o755); err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
// Read back
img, err := erofs.Open(bytes.NewReader(buf.Bytes()))
if err != nil {
log.Fatal(err)
}
data, err := fs.ReadFile(img, "hello.txt")
if err != nil {
log.Fatal(err)
}
fmt.Print(string(data))
Output: hello world
func (*Writer) Chmod ¶ added in v0.3.0
Chmod sets permission bits on the named path, preserving type bits.
func (*Writer) Chtimes ¶ added in v0.3.0
Chtimes sets the access and modification times on the named path. EROFS only stores mtime; atime is retained for read-back before Close.
func (*Writer) Close ¶ added in v0.3.0
Close writes the EROFS image. The FS must not be used after Close.
func (*Writer) CopyFrom ¶ added in v0.3.0
CopyFrom walks an fs.FS and adds all entries. Opens files for data when Entry.Data is nil. Reads symlink targets via readLinker interface when Entry.LinkTarget is empty. If src implements blockSizer, the image block size is set accordingly.
func (*Writer) Create ¶ added in v0.3.0
Create creates a regular file with default mode 0644. The caller must Close the returned File.
func (*Writer) Mkdir ¶ added in v0.3.0
Mkdir creates a directory. Only permission bits from perm are used; type bits are forced to directory. Mkdir("/", perm) sets root permissions.
func (*Writer) Mknod ¶ added in v0.3.0
Mknod creates a device, FIFO, or socket. mode must include type bits (e.g. disk.StatTypeChrdev | 0o666).
func (*Writer) Open ¶ added in v0.3.0
Open opens the named file for reading. For regular files, the file must have been closed (data finalized) before it can be opened for reading. For directories, the returned file implements fs.ReadDirFile.
func (*Writer) SetNlink ¶ added in v0.3.0
SetNlink overrides the computed link count on the named path.