# File storage

Status: **available** (Phase E): managed local storage.

Metadata lives in the database (`vault_files`, `file_revisions`,
`file_tags`); contents live behind the storage layer
(`backend/app/core/files/`). Only managed local storage exists today;
the layer is written so another adapter (S3-compatible, private remote)
can sit behind the same calls. None is required.

## Layout

```
FILES_DIR (default ~/.phantom/files, env PHANTOM_FILES_DIR)
  tmp/<random>                        uploads being received
  v<vault_id>/objects/ab/<sha256>     immutable contents
```

- Content-addressed **per vault**: identical contents inside a vault are
  stored once; nothing is shared between vaults, so deleting a vault
  removes one directory and its files never mix with another vault's.
- Directories are created 0700 and files 0600 on POSIX
  (`core/secure_files.py`); on Windows the profile's ACL applies.
- A storage reference is `local:v<vault_id>/<sha256>`. Only the storage
  produces it, and it is parsed back with a strict pattern that must name
  the caller's vault. The resolved path must stay under the root after
  following links, and contents are opened with `O_NOFOLLOW`: a symbolic
  link planted in the storage is refused, never followed.

## Writing

1. The name, folder and file count are checked before any byte is read.
2. The content streams into `tmp/` in pieces, hashed and counted as it
   arrives, and stops at the smaller of the file limit and what is left
   of the vault's quota. A `Content-Length` over the limit is refused
   before reading. Over the limit, the partial file is removed.
3. The type is read from the first 8 KiB.
4. Markdown of at most 1 MiB of UTF-8 becomes a note instead (see
   [Markdown files and notes](markdown-notes.md)) and the received copy
   is dropped.
5. Otherwise, under the vault's folder lock, the name is checked again,
   the content is flushed to disk and renamed into `objects/` (atomic on
   one filesystem; dropped when that checksum is already stored), and the
   file and revision rows are inserted in one transaction.
6. If that transaction fails, the placed content is removed unless
   another revision already referenced it.

## Removing

Contents are removed only when no revision references them: after a
purge, after retention drops old revisions, and after a failed insert.
When a vault is deleted, its rows go with it (`ON DELETE CASCADE`) and its
directory is removed. On start, before the API serves, a sweep removes
everything in `tmp/`, directories of vaults that no longer exist and
contents no revision references, and logs what it removed.

## Reading

Downloads stream from storage. A missing content answers `409` with
reason `missing`, a refused reference `404`. Text previews and agent
reads are bounded; whole-text reads (agents, the text endpoint) check the
SHA-256 first.

## Limits

| Setting | Default | Meaning |
|---|---|---|
| `FILES_MAX_BYTES` | 100 MiB | largest content one upload or save may store |
| `FILES_VAULT_QUOTA` | 5 GiB | stored contents per vault (each distinct content once) |
| `FILES_REVISIONS_KEPT` | 50 | newest revisions kept per file |
| `FILES_REVISION_BYTES` | 1 GiB | bytes the revisions of one file may keep |

## Backup and restore

The database plus `FILES_DIR` are the complete state; one vault's files
are its `v<vault_id>` directory plus its rows. Copy both while Phantom is
stopped. A portable export with a manifest is not built yet.
