# Module /fs

Filesystem operations: reading, writing, copying, moving, recursive tree listing, glob matching, and path canonicalization.

```tangu
(use /fs)
```

## Functions & symbols

### canonicalize

`(fs/canonicalize path) -> String`

Resolves relative paths, symlinks, and `..` segments to an absolute normalized path.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Relative or symlinked path |

```tangu
(let abs-path (fs/canonicalize "../config.json"))
```

### close

`(fs/close file) -> None`

Flushes any buffered writes and closes an open file handle.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open file handle |

```tangu
(fs/close f)
```

### copy

`(fs/copy source target) -> Int`

Copies source file to target destination, returning bytes copied.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `source` | `String` | no | Source file |
| `target` | `String` | no | Target file |

```tangu
(fs/copy "a.txt" "b.txt")
```

### copy-directory

`(fs/copy-directory src dest) -> None`

Recursively copies a directory tree from `src` to `dest`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `src` | `String` | no | Source directory |
| `dest` | `String` | no | Destination directory |

```tangu
(fs/copy-directory "./assets" "./dist/assets")
```

### create

`(fs/create path) -> File`

Creates a new file (or truncates existing) opened for writing.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | File path to create or truncate |

```tangu
(let f (fs/create "./out.txt"))
```

### create-directory

`(fs/create-directory path) -> Bool`

Recursively creates the directory at path (equivalent to mkdir -p).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Directory path to create |

```tangu
(fs/create-directory "./build/dist")
```

### create-temp-dir

`(fs/create-temp-dir) -> String`

Creates a unique temporary directory on disk and returns its absolute path.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `prefix-or-options` | `(Or String Map)` | no | Optional prefix String or {:prefix "..." :suffix "..." :dir "..."} Map |

```tangu
(let dir (try (fs/create-temp-dir "build-")))
(let dir (try (fs/create-temp-dir {:prefix "test-" :suffix "-work"})))
```

### create-temp-file

`(fs/create-temp-file) -> String`

Creates a unique temporary file on disk (closing the handle immediately) and returns its absolute path.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `prefix-or-options` | `(Or String Map)` | no | Optional prefix String or {:prefix "..." :suffix "..." :dir "..."} Map |

```tangu
(let path (try (fs/create-temp-file "upload-")))
(let path (try (fs/create-temp-file {:prefix "doc-" :suffix ".json"})))
```

### exists?

`(fs/exists? path) -> Bool`

Returns true if the file or directory at path exists, otherwise false.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | The file or directory path to check |

```tangu
(if (fs/exists? "./data.json") (say "found") (else (say "missing")))
```

### flush

`(fs/flush file) -> None`

Flushes buffered writes in `file` to the operating system.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open writable file handle |

```tangu
(fs/flush f)
```

### get-current-dir

`(fs/get-current-dir) -> String`

Returns the absolute path of the current working directory.

### is-directory?

`(fs/is-directory? path) -> Bool`

Returns true if path exists and points to a directory, or false if the path does not exist.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Path to test |

### is-file?

`(fs/is-file? path) -> Bool`

Returns true if path exists and points to a regular file, or false if the path does not exist.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Path to test |

### is-symlink?

`(fs/is-symlink? path) -> Bool`

Returns true if path is a symbolic link (including dangling symlinks), or false if the path does not exist.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Path to test |

```tangu
(fs/is-symlink? "./latest")
```

### list

`(fs/list path) -> Array`

Lists file and folder paths in the directory. Accepts optional {:include-dirs true}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Directory path to list |

```tangu
(let files (fs/list "."))
```

### list-as-tree

`(fs/list-as-tree path) -> Array`

Recursively walks directory and returns hierarchy tree.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Root directory path |

### open

`(fs/open path options) -> File`

Opens a file handle with explicit mode flags (`:read`, `:write`, `:append`, `:create`, etc.).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | File path |
| `options` | `Map` | no | Mode flags {:read :write :append :truncate :create :create-new :buffered} |

```tangu
(let f (fs/open "app.log" {:append true :create true}))
```

### read

`(fs/read file buf-or-count) -> Int`

Reads raw bytes from `file` into `buf` and returns byte count read.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open readable file handle |
| `buf` | `Slab` | no | Mutable Slab buffer to fill |

```tangu
(fs/read f slab)
```

### read-all

`(fs/read-all file) -> Slab`

Reads all remaining bytes from `file` until EOF into a byte `Slab`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open readable file handle |

```tangu
(fs/read-all f)
```

### read-byte

`(fs/read-byte file) -> (Or U8 None)`

Reads a single byte (`U8`) from `file`, or `()` at EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open readable file handle |

```tangu
(fs/read-byte f)
```

### read-char

`(fs/read-char file) -> (Or Char None)`

Reads a single UTF-8 `Char` from `file`, or `()` at EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open text file handle |

```tangu
(fs/read-char f)
```

### read-line

`(fs/read-line file) -> (Or String None)`

Reads the next UTF-8 line (stripping trailing newline) from `file`, or `()` at EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open text file handle |

```tangu
(fs/read-line f)
```

### read-link

`(fs/read-link path) -> String`

Reads the immediate target path of a symbolic link without resolving intermediate links.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Symbolic link path to read |

```tangu
(let target (try (fs/read-link "./current")))
```

### read-slab-from-file

`(fs/read-slab-from-file path) -> Slab`

Reads raw bytes of a file into a binary Slab buffer.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Path to binary file |

### read-string

`(fs/read-string file) -> String`

Reads all remaining UTF-8 text from `file` until EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open text file handle |

```tangu
(fs/read-string f)
```

### read-string-from-file

`(fs/read-string-from-file path) -> String`

Reads the complete UTF-8 contents of a file at path into a String. Returns an Error if the file cannot be read.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Path of the file to read |

```tangu
(let content (fs/read-string-from-file "config.json"))
```

### remove-directory

`(fs/remove-directory path) -> None`

Recursively removes the directory at path and all its contents by default. Pass {:recursive false} to fail on non-empty directories.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Directory path to remove |
| `options` | `(Or Map)` | no | Optional {:recursive false} to require an empty directory |

```tangu
(fs/remove-directory "./tmp")
(fs/remove-directory "./empty-dir" {:recursive false})
```

### remove-file

`(fs/remove-file path) -> None`

Deletes a regular file or symlink at `path`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | File path to unlink |

```tangu
(fs/remove-file "./tmp.txt")
```

### rename

`(fs/rename from to) -> Bool`

Renames or moves a file or directory from one path to another.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `from` | `String` | no | Current path |
| `to` | `String` | no | New path |

```tangu
(fs/rename "old.txt" "new.txt")
```

### stat

`(fs/stat path) -> Map`

Returns a metadata Map with :size (Int), :modified-at (Instant), :created-at (Instant or ()), :accessed-at (Instant or ()), :is-file (Bool), :is-dir (Bool), :is-symlink (Bool), :readonly (Bool), and :mode (Int or ()).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | File, directory, or symlink path to inspect |
| `options` | `(Or Map)` | no | Optional {:follow-symlinks false} to inspect the link itself without following |

```tangu
(let info (try (fs/stat "./config.json")))
(let link-info (try (fs/stat "./latest" {:follow-symlinks false})))
```

### symlink

`(fs/symlink target link-path) -> Bool`

Creates a symbolic link at link-path pointing to target. Returns true on success or an :io Error.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `target` | `String` | no | Target path that the symbolic link points to |
| `link-path` | `String` | no | Path where the symbolic link will be created |

```tangu
(try (fs/symlink "./releases/v1" "./current"))
```

### write

`(fs/write file data) -> Int`

Writes bytes or text to `file` and returns bytes written.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open writable file handle |
| `data` | `String | Slab` | no | Text or binary bytes to write |

```tangu
(fs/write f "line\n")
```

### write-byte

`(fs/write-byte file byte) -> None`

Writes a single byte to `file`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open writable file handle |
| `byte` | `U8 | Int` | no | Byte to write |

```tangu
(fs/write-byte f 10u8)
```

### write-line

`(fs/write-line file line) -> None`

Writes `line` followed by a newline to `file`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open writable file handle |
| `line` | `String` | no | Line string to write |

```tangu
(fs/write-line f "entry")
```

### write-slab-to-file

`(fs/write-slab-to-file path slab) -> Int`

Writes binary Slab buffer to file at path. Returns number of bytes written.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Destination path |
| `slab` | `Slab` | no | Binary bytes to write |

### write-string

`(fs/write-string file text) -> None`

Writes `text` to `file` without appending a newline.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open writable file handle |
| `text` | `String` | no | UTF-8 string to write |

```tangu
(fs/write-string f "chunk")
```

### write-string-to-file

`(fs/write-string-to-file path content) -> Int`

Writes content to path, automatically creating parent directories if necessary. Returns the number of bytes written.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Destination file path |
| `content` | `String` | no | Text content to write |

```tangu
(fs/write-string-to-file "./output/log.txt" "Task completed.")
```

### writeln

`(fs/writeln file text) -> None`

Writes `text` followed by a newline to `file`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `file` | `File` | no | Open writable file handle |
| `text` | `String` | no | UTF-8 string to write |

```tangu
(fs/writeln f "ok")
```
