Skip to main content
Read and write files inside a running sandbox over the same host-guest channel as command execution: no SSH, no network. See Filesystem for usage examples. For bulk file transfer, prefer a volume.

Methods

The accessor is obtained from a running sandbox via sb.FS(). Every method takes a context.Context first and returns an error (wrapped, inspectable with m.IsKind).

fs.Read()

Read the entire contents of a file as raw bytes. The default FFI buffer is 1 MiB; for files larger than ~750 KiB (after base64 inflation) the runtime returns BufferTooSmall on the single-shot path, and this method transparently falls back to ReadStream so callers get a uniform bytes-returning interface up to runtime memory limits.

Parameters

ctxcontext.Context
Cancels the read.
pathstring
Absolute path inside the guest, e.g. “/app/config.json”.

Returns

[]byte
File contents.

fs.ReadString()

Read a file and return its contents as a string. The bytes are reinterpreted as UTF-8 without validation.

Parameters

ctxcontext.Context
Cancels the read.
pathstring
Absolute path inside the guest.

Returns

string
File contents.

fs.Write()

Write bytes to a file, creating it if it does not exist and truncating it if it does.

Parameters

ctxcontext.Context
Cancels the write.
pathstring
Absolute path inside the guest.
data[]byte
Bytes to write.

fs.WriteString()

Write a UTF-8 string to a file. Convenience wrapper over Write.

Parameters

ctxcontext.Context
Cancels the write.
pathstring
Absolute path inside the guest.
contentstring
Text to write.

fs.List()

List the entries in a directory inside the sandbox.

Parameters

ctxcontext.Context
Cancels the listing.
pathstring
Absolute directory path inside the guest.

Returns

Directory entries.

fs.Stat()

Get detailed metadata for a file or directory.

Parameters

ctxcontext.Context
Cancels the stat.
pathstring
Absolute path inside the guest.

Returns

File metadata.

fs.Mkdir()

Create a directory and any missing parents inside the sandbox.

Parameters

ctxcontext.Context
Cancels the operation.
pathstring
Absolute directory path inside the guest.

fs.Remove()

Delete a single file. Use RemoveDir for directories.

Parameters

ctxcontext.Context
Cancels the operation.
pathstring
Absolute file path inside the guest.

fs.RemoveDir()

Remove a directory recursively.

Parameters

ctxcontext.Context
Cancels the operation.
pathstring
Absolute directory path inside the guest.

fs.Copy()

Copy a file within the sandbox.

Parameters

ctxcontext.Context
Cancels the operation.
srcstring
Source path inside the guest.
dststring
Destination path inside the guest.

fs.Rename()

Rename or move a file or directory within the sandbox.

Parameters

ctxcontext.Context
Cancels the operation.
srcstring
Current path inside the guest.
dststring
New path inside the guest.

fs.Exists()

Report whether a file or directory exists at the given path.

Parameters

ctxcontext.Context
Cancels the check.
pathstring
Absolute path inside the guest.

Returns

bool
true if the path exists.

fs.CopyFromHost()

Copy a file from the host machine into the sandbox. For transferring many files, consider a bind-mounted volume instead.

Parameters

ctxcontext.Context
Cancels the copy.
hostPathstring
Source path on the host.
guestPathstring
Destination path inside the guest.

fs.CopyToHost()

Copy a file from the sandbox to the host machine.

Parameters

ctxcontext.Context
Cancels the copy.
guestPathstring
Source path inside the guest.
hostPathstring
Destination path on the host.

fs.ReadStream()

Open a streaming reader for a file. Use this for files too large to fit in memory; data is delivered in chunks. The caller must Close the returned *FsReadStream. Read falls back to this automatically when a file exceeds the single-shot buffer.

Parameters

ctxcontext.Context
Cancels opening the stream.
pathstring
Absolute path inside the guest.

Returns

Open read stream; close it when done.

fs.WriteStream()

Open a streaming writer for a file. Use this for files too large to fit in memory. The caller must call Close(ctx) on the returned *FsWriteStream to finalise the write.

Parameters

ctxcontext.Context
Cancels opening the stream.
pathstring
Absolute path inside the guest.

Returns

Open write stream; Close(ctx) it to finalise.

Types

FsEntry

Returned by List()

A single directory listing entry.

FsEntryKind

Used by FsEntry.Kind

Classifies a directory listing entry. Defined as type FsEntryKind string.

FsStat

Returned by Stat()

Detailed file metadata.

FsReadStream

Returned by ReadStream()

An open streaming read from a guest file. Must be closed with Close when done. On a write error or partial write, WriteTo and CopyTo return the partial byte count and leave the stream open so the caller can recover; close it explicitly in either case.

FsWriteStream

Returned by WriteStream()

An open streaming write to a guest file. Must be closed with Close(ctx) to finalise the write.