Files¶
Files move between the host and a sandbox through the socket, as tar archives
the daemon extracts and builds. This page covers single files, whole
directories and archives. To give a sandbox a host directory without copying
it, mount it at creation instead (workspace and workspaces, see
Creating a sandbox).
import sys
from pathlib import Path
import fastsandbox
async def main(project: Path) -> None:
client = await fastsandbox.connect()
async with client.create(kit="shell") as sandbox, sandbox.running():
# One file, then an executable script: the directory must exist.
await sandbox.write_file("/tmp/settings.toml", 'level = "debug"\n')
await sandbox.write_file("/tmp/check.sh", '#!/bin/sh\nls -R "$1"\n', mode=0o755)
print(await sandbox.read_text("/tmp/settings.toml"), end="")
# A directory lands under its own name: /work/<project.name>.
await sandbox.exec(["mkdir", "-p", "/work"], check=True)
await sandbox.upload_dir(project, "/work")
await sandbox.exec(["/tmp/check.sh", f"/work/{project.name}"], check=True)
# And comes back the same way, under results/<project.name>.
await sandbox.download_dir(f"/work/{project.name}", project.parent / "results")
if __name__ == "__main__":
fastsandbox.run(main(Path(sys.argv[1])))
The sandbox must run (see Sessions): a transfer to or
from a stopped sandbox raises
ConflictError.
Single files¶
write_file(path, contents, mode=None)writes a string (as UTF-8) or bytes to the absolutepath, replacing an existing file. Files are created as root, with mode0644unlessmodesays otherwise (0o755for a script).read_file(path)reads a regular file as bytes;read_text(path, encoding="utf-8")decodes it.
The directory must exist
The daemon extracts into an existing directory. A file written into a
directory that does not exist fails with a server error
(ApiError, status 500): create the
directory first, with sandbox.exec(["mkdir", "-p", dir]).
Reading a path that does not exist raises
NotFoundError; reading a directory
with read_file(), ArchiveError.
Directories¶
upload_dir(host_dir, dir)copies a host directory into the existing directorydirof the sandbox, under its own name:upload_dir("./project", "/work")gives/work/project/.... Symbolic links are copied as links.download_dir(path, host_dir)copies a directory or a file of the sandbox intohost_dir, created when missing, under its own name too:download_dir("/work/out", "./results")gives./results/out/.... Entries whose path holds..are skipped, and an entry written through a symbolic link that leads outsidehost_dirfails the call; symbolic links themselves are written as they are, and may point outside it. Download into an empty directory, and look before you follow a link.
A host directory that cannot be read, or written, raises
HostFileError, an OSError with the
path.
Archives¶
A program that stores or streams archives itself moves them as they are:
download_archive(path)gives a tar archive ofpath, its entries named after the last component of the path;upload_archive(dir, archive)extracts a tar archive into the existing directorydir.
To copy the contents of a directory without its name, send an archive of
those contents with upload_archive().