Skip to content

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).

docs_src/client/files.py
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

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 directory dir of 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 into host_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 outside host_dir fails 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:

To copy the contents of a directory without its name, send an archive of those contents with upload_archive().

archive = await sandbox.download_archive("/work/project")  # entries: project/...
await sandbox.exec(["mkdir", "-p", "/backup"], check=True)
await sandbox.upload_archive("/backup", archive)  # /backup/project/...