Skip to content

Put files into a sandbox, get what it wrote back out, and move, copy, or remove files in it, without running a command.

Your code can read and write a sandbox’s files directly, without running a command such as cat or cp in the sandbox. Paths are relative to the sandbox’s root, so leave out the leading /: etc/hostname means /etc/hostname.

Put a file in, and get one out

This writes an input file into the sandbox, runs a command on it, and reads what the command wrote:

await sandbox.create_dir_all("work")
await sandbox.write_file("work/input.txt", "one two three\n")
await sandbox.exec(["sh", "-c", "wc -w < /work/input.txt > /work/count.txt"]).complete()
print(await sandbox.read_text("work/count.txt"))

write_file writes a whole file from a string (as UTF-8) or from bytes. The parent directory must exist, or the call fails with 409. create_dir_all creates a directory and any missing parents, like mkdir -p. read_text reads a whole UTF-8 file. read_file reads bytes: the whole file, or a range starting at an offset.

Look around

read_dir lists a directory, each entry with its name and metadata. stat_file returns a path’s metadata: its size, when it was last modified, and its etag. exists, is_file and is_dir check whether a path exists, is a file, or is a directory.

for entry in await sandbox.read_dir("work"):
print(entry.name, entry.metadata.size)
if await sandbox.is_file("work/count.txt"):
meta = await sandbox.stat_file("work/count.txt")
print(meta.size, meta.etag)

Move, copy, and remove

rename_file moves a file or a directory. copy_file copies a file, or a directory and everything in it. Both fail with 412 if the destination exists, unless you set overwrite. remove_file removes a file, or a directory and everything in it. It fails if the path doesn’t exist, unless you set missing_ok, like rm -f.

await sandbox.copy_file("work/count.txt", "work/count.bak")
await sandbox.rename_file("work/count.bak", "work/old.txt", overwrite=True)
await sandbox.remove_file("work", missing_ok=True)

Write only if unchanged

To avoid overwriting someone else’s change, pass the file’s etag from stat_file as if_match to write_file. The write succeeds only if the file still has that etag. If the file changed in the meantime, the write fails with 412 and the file is left as it was.

meta = await sandbox.stat_file("notes.txt")
await sandbox.write_file("notes.txt", "updated\n", if_match=meta.etag)

If two writes with the same etag arrive at the same time, one succeeds and the other fails with 412. After a 412, read the file again and retry. The server checks the etag before it starts writing, and again just before it replaces the file.

Reading files from an idle sandbox

The server reads a sandbox’s files through its VM. If the server stopped the VM because the sandbox was idle, reading a file boots the VM again first, which takes time. To read a finished task’s output without booting a VM, have the task write its output to a volume, and read it from the volume. See Mounting volumes.

Troubleshooting

  1. 409, from write_file or create_dir

    Cause: The parent directory does not exist.

    Fix: Make it first with create_dir_all.

  2. 405, from create_dir_all

    Cause: A file is at that path.

    Fix: Remove or rename the file.

  3. 404, from remove_file

    Cause: Nothing is at that path.

    Fix: Pass missing_ok to treat that as done.

  4. 412, from copy_file or rename_file

    Cause: The destination exists.

    Fix: Pass overwrite.

  5. 412, from write_file with if_match

    Cause: The file changed since its etag was read.

    Fix: Read it again with stat_file and retry.

  6. An error that mentions UTF-8, from read_text

    Cause: The file is not UTF-8 text.

    Fix: Read its bytes with read_file.

  7. 400

    Cause: The path has . or .. in it, or leads outside the sandbox’s root.

    Fix: Use a path inside the root.

  8. 403

    Cause: A write to a volume mounted read-only, or a move or remove of the root directory itself.

    Fix: Mount the volume read-write, or use a path under the root.

  9. 409, ended

    Cause: The sandbox’s VM has stopped permanently.

    Fix: Create another sandbox. Keep outputs on a volume so they survive this.

Next