# Files

https://virtx.sh/docs/sdk/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](https://virtx.sh/docs/tutorials/reading-writing-files/).

| Member | Does |
| --- | --- |
| [`stat_file`](#stat_file) | What is at `path`: whether it is a directory, its size, its modification time, and its `etag`. |
| [`exists`](#exists) | Whether anything, a file, or a directory is at `path`. |
| [`read_dir`](#read_dir) | The entries of a directory, each with its `name` and `metadata`. |
| [`read_file`](#read_file) | A file's bytes: from `offset`, `length` bytes or all that is left. |
| [`read_text`](#read_text) | A whole UTF-8 file as text. |
| [`write_file`](#write_file) | Writes a whole file, created if it does not exist; its parent directory must exist. |
| [`create_dir`](#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`](#remove_file) | Removes a file, or a directory with everything in it. |
| [`rename_file`](#rename_file) | Moves a file or directory; refused onto something that exists unless `overwrite`. |
| [`copy_file`](#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`.

**Python**

```python
async def stat_file(path: str) -> Metadata
```

**Node.js**

```ts
statFile(path: string): Promise<Metadata>
```

**Rust**

```rust
fn stat_file(&self, path: impl Into<String>) -> Call<Metadata>
```

## `exists`

Whether anything, a file, or a directory is at `path`.

**Python**

```python
async def exists(path: str) -> bool
async def is_file(path: str) -> bool
async def is_dir(path: str) -> bool
```

**Node.js**

```ts
exists(path: string): Promise<boolean>
isFile(path: string): Promise<boolean>
isDir(path: string): Promise<boolean>
```

**Rust**

```rust
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`.

**Python**

```python
async def read_dir(path: str) -> list[DirEntry]
```

**Node.js**

```ts
readDir(path: string): Promise<DirEntry[]>
```

**Rust**

```rust
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.

**Python**

```python
async def read_file(path: str, offset: int = 0, length: int | None = None) -> bytes
```

**Node.js**

```ts
readFile(path: string, offset?: number, length?: number): Promise<Buffer>
```

**Rust**

```rust
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.

**Python**

```python
async def read_text(path: str) -> str
```

**Node.js**

```ts
readText(path: string): Promise<string>
```

**Rust**

```rust
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.

**Python**

```python
async def write_file(path: str, content: bytes | str, if_match: str | None = None) -> None
```

**Node.js**

```ts
writeFile(path: string, content: Buffer | Uint8Array | string, ifMatch?: string): Promise<void>
```

**Rust**

```rust
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.

**Python**

```python
async def create_dir(path: str) -> None
async def create_dir_all(path: str) -> None
```

**Node.js**

```ts
createDir(path: string): Promise<void>
createDirAll(path: string): Promise<void>
```

**Rust**

```rust
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`.

**Python**

```python
async def remove_file(path: str, missing_ok: bool = False) -> None
```

**Node.js**

```ts
removeFile(path: string, missingOk?: boolean): Promise<void>
```

**Rust**

```rust
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`.

**Python**

```python
async def rename_file(from_path: str, to_path: str, overwrite: bool = False) -> None
```

**Node.js**

```ts
renameFile(from: string, to: string, overwrite?: boolean): Promise<void>
```

**Rust**

```rust
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.

**Python**

```python
async def copy_file(from_path: str, to_path: str, overwrite: bool = False) -> None
```

**Node.js**

```ts
copyFile(from: string, to: string, overwrite?: boolean): Promise<void>
```

**Rust**

```rust
fn copy_file(&self, from: impl Into<String>, to: impl Into<String>, overwrite: bool) -> Call<()>
```

## Errors

A call that fails raises a [`VirtxError`](https://virtx.sh/docs/sdk/errors/) 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](https://virtx.sh/docs/sdk/client/#errors)), 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](https://virtx.sh/docs/sdk/errors/#retries).
