Files
Reference for reading and writing the files of a sandbox or a volume.
A Sandbox and a Volume share these calls, each on its own files, by a path relative to its root such as dir/file.txt; the empty path is the root, and the server refuses one that leads outside.
In Rust, they come from the Files trait, so use virtx::Files;.
See Reading and writing files.
| Member | Does |
|---|---|
stat_file |
What is at path: whether it is a directory, its size, its modification time, and its etag. |
exists |
Whether anything, a file, or a directory is at path. |
read_dir |
The entries of a directory, each with its name and metadata. |
read_file |
A file’s bytes: from offset, length bytes or all that is left. |
read_text |
A whole UTF-8 file as text. |
write_file |
Writes a whole file, created if it does not exist; its parent directory must exist. |
create_dir |
Creates a directory whose parent exists; create_dir_all creates its parents too, as mkdir -p, and is refused with 405 when a file is there. |
remove_file |
Removes a file, or a directory with everything in it. |
rename_file |
Moves a file or directory; refused onto something that exists unless overwrite. |
copy_file |
Copies a file or directory with everything in it, replacing as rename_file does. |
stat_file
What is at path: whether it is a directory, its size, its modification time, and its etag.
async def stat_file(path: str) -> MetadatastatFile(path: string): Promise<Metadata>fn stat_file(&self, path: impl Into<String>) -> Call<Metadata>exists
Whether anything, a file, or a directory is at path.
async def exists(path: str) -> boolasync def is_file(path: str) -> boolasync def is_dir(path: str) -> boolexists(path: string): Promise<boolean>isFile(path: string): Promise<boolean>isDir(path: string): Promise<boolean>fn exists(&self, path: impl Into<String>) -> Call<bool>fn is_file(&self, path: impl Into<String>) -> Call<bool>fn is_dir(&self, path: impl Into<String>) -> Call<bool>read_dir
The entries of a directory, each with its name and metadata.
async def read_dir(path: str) -> list[DirEntry]readDir(path: string): Promise<DirEntry[]>fn read_dir(&self, path: impl Into<String>) -> Call<Vec<DirEntry>>read_file
A file’s bytes: from offset, length bytes or all that is left.
async def read_file(path: str, offset: int = 0, length: int | None = None) -> bytesreadFile(path: string, offset?: number, length?: number): Promise<Buffer>fn read_file(&self, path: impl Into<String>, offset: u64, length: Option<u64>) -> Call<Vec<u8>>read_text
A whole UTF-8 file as text.
async def read_text(path: str) -> strreadText(path: string): Promise<string>fn read_text(&self, path: impl Into<String>) -> Call<String>write_file
Writes a whole file, created if it does not exist; its parent directory must exist. With if_match, the etag a stat gave, only while the file still has it, and refused with 412 otherwise.
async def write_file(path: str, content: bytes | str, if_match: str | None = None) -> NonewriteFile(path: string, content: Buffer | Uint8Array | string, ifMatch?: string): Promise<void>fn write_file(&self, path: impl Into<String>, content: impl AsRef<[u8]>, if_match: Option<&str>) -> Call<()>create_dir
Creates a directory whose parent exists; create_dir_all creates its parents too, as mkdir -p, and is refused with 405 when a file is there.
async def create_dir(path: str) -> Noneasync def create_dir_all(path: str) -> NonecreateDir(path: string): Promise<void>createDirAll(path: string): Promise<void>fn create_dir(&self, path: impl Into<String>) -> Call<()>fn create_dir_all(&self, path: impl Into<String>) -> Call<()>remove_file
Removes a file, or a directory with everything in it. With missing_ok, nothing being there is fine, as rm -f.
async def remove_file(path: str, missing_ok: bool = False) -> NoneremoveFile(path: string, missingOk?: boolean): Promise<void>fn remove_file(&self, path: impl Into<String>, missing_ok: bool) -> Call<()>rename_file
Moves a file or directory; refused onto something that exists unless overwrite.
async def rename_file(from_path: str, to_path: str, overwrite: bool = False) -> NonerenameFile(from: string, to: string, overwrite?: boolean): Promise<void>fn rename_file(&self, from: impl Into<String>, to: impl Into<String>, overwrite: bool) -> Call<()>copy_file
Copies a file or directory with everything in it, replacing as rename_file does.
async def copy_file(from_path: str, to_path: str, overwrite: bool = False) -> NonecopyFile(from: string, to: string, overwrite?: boolean): Promise<void>fn copy_file(&self, from: impl Into<String>, to: impl Into<String>, overwrite: bool) -> Call<()>Errors
A call that fails raises a VirtxError in Python, throws one in Node.js, and returns one in Rust. Branch on its status and code, not its message. These are the errors the calls on this page return most often.
| Error | From | Cause |
|---|---|---|
source client |
Any call | The path’s .. leads above the root. The client resolves . and .. itself, so a/../b is sent as b, and refuses a path that climbs above the root without sending it. |
403 |
Writes, remove_file, rename_file |
A write to a volume mounted read-only, or a move or remove of the root itself. |
404 |
Any call but exists, is_file and is_dir |
Nothing is at that path, or the sandbox or volume doesn’t exist. For remove_file, pass missing_ok to treat it as done. exists, is_file and is_dir answer false instead. |
405 |
create_dir_all |
A file is at that path. |
409 |
write_file, create_dir |
The parent directory doesn’t exist. Create it with create_dir_all. |
409, ended |
Any call on a sandbox | The sandbox’s VM has stopped for good. Create another sandbox. |
412 |
copy_file, rename_file |
The destination exists. Pass overwrite. |
412 |
write_file with if_match |
The file changed since you read its etag. Read it again with stat_file, then retry. |
| An error that mentions UTF-8 | read_text |
The file isn’t UTF-8 text. Read its bytes with read_file. |
Any call can also fail with 401, unauthorized or 403, forbidden (see Client), or with source transport when the connection drops. The client retries 503, capacity and a 429, quota for the request rate by itself for up to 30 seconds: see Retries.