ACFS provides directory listing, recursive walks, metadata, and
streaming reads and writes that can never escape a root directory. Logical paths are confined
with os.Root, .. components are collapsed, and a symlink whose target escapes the root reports
ErrOutsideRoot instead of being followed.
- Module
kit.getarcane.app/acfs- Reference
- pkg.go.dev
go get kit.getarcane.app/acfs@latestConcepts
Section titled “Concepts”Every operation is a method on acfs.Root. Two paths are involved:
Root.Pathis the real host directory that acts as the boundary, for example/var/lib/docker/volumes/app/_data.Rootis a plain value, so build it with a literal:acfs.Root{Path: dir}. The zeroRoothas an emptyPath, and every operation on it fails.logicalPathis the method argument. It is an absolute path inside the root, for example/config/app.yaml./refers to the root itself.
root.LogicalPath(absPath) translates a host path into a logical path. It is purely lexical and
does not touch the filesystem. A host path outside root.Path returns ErrOutsideRoot.
Reading
Section titled “Reading”import ( "kit.getarcane.app/acfs" "kit.getarcane.app/acfs/types")
root := acfs.Root{Path: "/var/lib/docker/volumes/example/_data"}
entries, err := root.List(ctx, "/")if err != nil { return err}
// Zero WalkOptions means no depth or entry limit.res, err := root.Walk(ctx, "/logs", types.WalkOptions{}, func(entry types.Entry) error { fmt.Println(entry.Path, entry.Mode, entry.Size) return nil})
data, err := root.ReadFile(ctx, "/config.yaml")| Method | Description |
|---|---|
List / ListEach |
Direct children of a directory. ListEach streams them in name order without keeping them all in memory |
Walk |
Every descendant, depth first, in deterministic order. Symlinks are not followed. types.WalkOptions{MaxDepth, MaxEntries} bounds the walk, and the returned types.WalkResult reports the count and whether anything was left out |
Stat |
Metadata for one path. With types.StatOptions{FollowFinal: true}, the final link is resolved and Entry.Path is the resolved logical path |
Exists |
Reports existence without following the final link, so a dangling link counts as existing |
ReadFile |
Whole contents of a regular file |
OpenRead / ReadTo |
Streaming reads with an optional byte limit |
OpenReadSeek |
A seekable reader over the whole file, for example for HTTP range requests |
Writing
Section titled “Writing”Writes are atomic by default. The content is staged in a temporary file in the target’s directory and renamed into place, so a reader never sees a partly written file.
err = root.Write(ctx, "/config.yaml", data, acfs.WriteOptions{Mode: 0o644})Set InPlace to keep the existing file’s inode. You need this when the file is bind-mounted into a
running container, because a rename would leave the container holding the old inode:
err = root.Write(ctx, "/config.yaml", data, acfs.WriteOptions{ Mode: 0o644, InPlace: true,})| Method | Description |
|---|---|
Write |
Writes a byte slice, atomically or in place |
WriteFrom |
Atomically writes exactly WriteFromOptions.Size bytes from an io.Reader, with WriteFromOptions.Mode. The destination is unchanged if the transfer fails |
WriteAt |
Writes at an offset in an existing file, in place |
Mkdir / MkdirAll / MkdirTemp |
Directory creation. MkdirTemp returns a logical path and leaves cleanup to the caller |
Rename |
os.Rename semantics within one root. A cross-device rename returns EXDEV unchanged |
Remove |
Removes a file, link, or empty directory. Returns a not-exist error |
RemoveAll |
Removes a whole tree. A missing target is not an error |
Temporary files use the reserved .acfs-write- prefix. Paths containing that prefix are rejected,
and List, Walk, and CopyTo hide them.
Copying and mirroring
Section titled “Copying and mirroring”src := acfs.Root{Path: "/srv/staging"}dst := acfs.Root{Path: "/srv/live"}
// Copy one root into another. Symlinks, sockets, FIFOs, and devices are skipped.res, err := src.CopyTo(ctx, dst, types.CopyOptions{TolerateUnreadable: true})fmt.Println(res.Copied, res.Skipped)
// Make dst match src, updating files IN PLACE so bind mounts keep working.err = src.MirrorTo(ctx, dst, types.MirrorOptions{ Preserve: []string{".env"},})The two roots of CopyTo must be separate and must not nest.
MirrorTo first removes destination entries that are missing from the source or have a different
type, then copies over the rest. Files whose content already matches are left untouched, including
their inode and mtime. Unreadable source files and unwritable destination files are tolerated:
stopping half-way would leave a live directory in a worse, mixed state.
Batch changes
Section titled “Batch changes”Root.Apply runs an ordered types.ApplyManifest of create, update, rename, move, and delete
operations. It stops at the first failure and returns a *types.ApplyError that gives the index of
the failing change. The staging root holds the files that create_file and update_file changes
name in StagedName. It does not roll back. If you need a transaction, restore your own backup.
applied, err := root.Apply(ctx, acfs.Root{Path: stagingDir}, types.ApplyManifest{ Version: types.ProtocolVersion, Changes: []types.ApplyChange{ {Operation: types.ApplyCreateFolder, Path: "/conf.d"}, { Operation: types.ApplyRename, Path: "/old.yaml", TargetPath: "/new.yaml", }, { Operation: types.ApplyDelete, Path: "/tmp", Recursive: true, }, },})Errors
Section titled “Errors”ACFS sentinels classify policy failures:
ErrInvalidPath, ErrOutsideRoot, ErrSymlinkLoop, ErrSymlink, ErrIsDirectory,
ErrNotDirectory, ErrNotFile, ErrNotEmpty, ErrAlreadyExists, ErrRootRemoval,
ErrSizeMismatch.
Errors from the operating system are wrapped, not translated, so the standard predicates keep
working: errors.Is(err, fs.ErrNotExist), errors.Is(err, fs.ErrExist), and
errors.Is(err, os.ErrPermission).
acfs/types
Section titled “acfs/types”Holds the public data contracts: Entry, the option and result types, and the versioned JSON
response types used by the command-line protocol (see the CLI tab).
The acfs command exposes the library over a small JSON protocol, so a process can browse and
edit a directory, for example inside a helper container, without starting a new process for
every file. GoReleaser publishes a static Linux binary for each supported architecture.
acfs list|walk|stat|read|write|mkdir|remove|apply|versionOutput format
Section titled “Output format”| Command | Output |
|---|---|
list |
One JSON ListResponse with entries |
stat |
One JSON StatResponse with entry |
walk |
NDJSON WalkRecord lines, always ending with an end record. Supports depth and entry limits |
read |
A 16-byte ARCW protocol-v2 header followed by the raw file content |
apply |
One JSON ApplyResponse with the number of changes applied |
version |
One JSON VersionResponse including the supported protocol |
Every response includes a version field matching types.ProtocolVersion (currently 2).
Errors
Section titled “Errors”On failure, the command writes exactly one JSON ErrorResponse line to stderr and nothing
else:
{ "code": "outside_root", "message": "path escapes workspace root", "operation": "stat", "path": "/../etc", "version": 2}code is a stable types.ErrorCode:
invalid_path, outside_root, symlink, symlink_loop, not_found, already_exists,
not_directory, not_file, not_empty, is_directory, size_mismatch, root_removal,
internal.
For apply, changeIndex identifies the manifest entry that failed.
Read stream header
Section titled “Read stream header”To consume read output from Go, validate the header first, then read exactly the payload length:
n, err := acfs.ReadStreamHeader(stdout)if err != nil { return err}_, err = io.CopyN(dst, stdout, int64(n))acfs.WriteStreamHeader produces the same header when you implement your own server side.