Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:memo: Improve workspace filesystem documentation https://github.com/siyuan-note/siyuan/issues/18266

Daniel203ac3d9758e
diff --git a/docs/SY-FORMAT.zh-CN.md b/docs/SY-FORMAT.zh-CN.md
index 3305bfface..756439e13c 100644
--- a/docs/SY-FORMAT.zh-CN.md
+++ b/docs/SY-FORMAT.zh-CN.md
@@ -3,7 +3,7 @@
> 版本基准:Spec `2`(当前所有文件)。
> 核验样本:`20200825162036-4dx365o.sy`(排版元素)、`20200905090211-2vixtlf.sy`(内容块类型)。
> 本文档所有结论均基于真实样本与 Lute / 思源内核代码核验;标注 `【推断】` 的字段表示未在样本中直接出现,建议生成前再用真实样本核验。
-> 配套文档:[`WORKSPACE.zh-CN.md`](./WORKSPACE.zh-CN.md) 讲工作区在磁盘上的整体布局(笔记本、父子文档、附件的组织方式);本文档专注 `.sy` 文件**内部**的 JSON 结构。
+> 配套文档:[`WORKSPACE.zh-CN.md`](./WORKSPACE.zh-CN.md) 讲工作区在磁盘上的整体布局(笔记本、父子文档、资源文件的组织方式);本文档专注 `.sy` 文件**内部**的 JSON 结构。
## 0. 一句话本质
diff --git a/docs/WORKSPACE.md b/docs/WORKSPACE.md
index 970c1ae662..8c5e493d56 100644
--- a/docs/WORKSPACE.md
+++ b/docs/WORKSPACE.md
@@ -6,7 +6,7 @@
## 0. In one sentence
-A SiYuan workspace is a **self-describing** directory tree: notebooks, documents, and assets all live on disk as readable files and directories. **The filename is the ID; the directory structure is the document hierarchy.** There is no binary database recording "where documents are" — the file system itself is the single source of truth. The kernel reconstructs all notebooks and documents simply by walking the directories.
+A SiYuan workspace is a **self-describing** directory tree: notebooks, documents, and assets live on disk as files and directories. **The filename is the ID; the directory structure is the document hierarchy.** The file system contains the authoritative source content, while SQLite databases under `temp/` are rebuildable indexes and caches. Normal-notebook content is readable directly; encrypted-notebook content is ciphertext and must be accessed through the unlocked kernel.
---
@@ -14,12 +14,16 @@ A SiYuan workspace is a **self-describing** directory tree: notebooks, documents
```
<workspace root>/ e.g. F:\SiYuan\
+├── .lock # Runtime workspace lock
├── conf/
│ ├── conf.json # ★Workspace-level config (appearance/system/sync/editor...)
-│ ├── ca.crt / cert.pem / key.pem # TLS certificates
-│ └── windowState.json
+│ ├── appearance/ # Installed themes, icons, and language resources
+│ ├── ca.crt / ca.key / cert.pem / key.pem # TLS certificates and keys
+│ └── windowState.json # Desktop window state
├── data/ # DataDir — root of all notebook data
-│ ├── .siyuan/ # Workspace-level hidden config (*ignore rules, etc.)
+│ ├── .siyuan/ # Workspace-level hidden config
+│ │ ├── syncignore / searchignore / embeddingignore / indexignore / refsearchignore
+│ │ └── notebook-crypto-backup.json # Optional encrypted-notebook global recovery metadata
│ ├── assets/ # ★Global assets (images/audio/video/files)
│ ├── templates/ # Global templates (.md)
│ ├── widgets/ # Widgets
@@ -27,20 +31,27 @@ A SiYuan workspace is a **self-describing** directory tree: notebooks, documents
│ ├── emojis/ # Custom emoji
│ ├── snippets/ # Code snippets (CSS/JS)
│ ├── public/ # Static resources
-│ ├── storage/ # Runtime indexes / cache / databases
+│ ├── storage/ # Persistent structured data (attribute views, plugins, etc.)
│ └── <boxID>/ # ★Notebook (dir name = notebook ID)
│ ├── .siyuan/
│ │ ├── conf.json # ★Notebook config (BoxConf: name/icon/closed...)
-│ │ ├── sort.json # Custom sort mapping {docID: order}
-│ │ └── boxDoc.json # Optional top-level notebook document marker for .sy.zip import/export
+│ │ ├── sort.json # Optional custom sort mapping {docID: order}
+│ │ ├── boxDoc.json # Optional top-level notebook document marker for .sy.zip import/export
+│ │ └── notebook-crypt-backup.json # Optional encrypted-notebook key-envelope backup
│ ├── <boxID>.sy # Optional top-level notebook document (rootID = notebook ID)
│ ├── <docID>.sy # Document (JSON tree; filename = doc rootID)
-│ └── <docID>/ # ★Children of that document (same-named pair)
-│ ├── <childID>.sy
-│ └── <childID>/ ... # Nesting of arbitrary depth
+│ ├── <docID>/ # ★Children of that document (same-named pair)
+│ │ ├── <childID>.sy
+│ │ └── <childID>/ ... # Nesting of arbitrary depth
+│ ├── assets/ # Notebook-local assets (mandatory for encrypted notebooks)
+│ └── storage/av/ # Encrypted-notebook attribute-view definitions
├── repo/ # Data repo (snapshots/sync)
├── history/ # Edit-history archive
-└── temp/ # Temp/export files
+├── corrupted/ # Optional quarantine for corrupted data
+└── temp/ # Rebuildable indexes, queues, caches, logs, and exports
+ ├── siyuan.db / blocktree.db / asset_content.db / history.db
+ ├── siyuan-encrypted-<boxID>.db / siyuan-encrypted-<boxID>-blocktree.db
+ └── queue/ / export/ / os/ / siyuan.log
```
---
@@ -49,16 +60,17 @@ A SiYuan workspace is a **self-describing** directory tree: notebooks, documents
The workspace root is chosen by the user (e.g. `F:\SiYuan\`). Whether a directory is a valid workspace is decided by: `conf/conf.json` exists and contains a `kernelVersion` field.
-Fixed sub-directories under the root:
+Workspace-root entries:
-| Sub-dir | Purpose |
+| Entry | Purpose |
|---|---|
-| `conf/` | Workspace-level config + TLS certificates |
+| `.lock` | Runtime lock preventing two kernels from opening the same workspace |
+| `conf/` | Workspace-level config, appearance resources, and TLS material |
| `data/` | **All notebook data** (the core of this document) |
| `repo/` | Data-snapshot repo (used for sync/history) |
| `history/` | Edit-history archive |
-| `temp/` | Temp/export files |
-| `corrupted/` | Quarantine for corrupted data (auto-moved by the kernel) |
+| `temp/` | Rebuildable indexes, queues, caches, logs, and exports |
+| `corrupted/` | Optional quarantine for corrupted data (auto-created when needed) |
---
@@ -66,7 +78,7 @@ Fixed sub-directories under the root:
The first level of `data/` mixes two kinds of entries:
-1. **Reserved resource directories:** `.siyuan/`, `assets/`, `templates/`, `widgets/`, `plugins/`, `emojis/`, `snippets/`, `public/`, `storage/`.
+1. **Fixed data directories:** `.siyuan/`, `assets/`, `templates/`, `widgets/`, `plugins/`, `emojis/`, `snippets/`, `public/`, `storage/`.
2. **Notebook directories:** each is a folder named with the notebook ID.
### Reserved-filename list (★ avoid when writing files)
@@ -80,7 +92,11 @@ func IsReservedFilename(baseName string) bool {
}
```
-That is, `assets` / `templates` / `widgets` / `emojis` / `.siyuan`, as well as anything starting with `.`, **cannot** be used as a notebook or document name.
+That is, the physical path segments `assets` / `templates` / `widgets` / `emojis` / `.siyuan`, as well as anything starting with `.`, are reserved. This restriction applies to on-disk IDs and path segments, not to the notebook names or document titles displayed to users.
+
+### Persistent data vs rebuildable indexes
+
+`data/storage/` contains persistent structured data such as attribute-view definitions and plugin state. The SQLite files in `temp/` are derived indexes and caches: normal notebooks use the global `siyuan.db` and `blocktree.db`, while each unlocked encrypted notebook uses independent SQLCipher databases. Deleting or rebuilding an index must not be confused with deleting the source `.sy`, asset, or database-definition files.
### How a notebook is recognized
@@ -120,6 +136,8 @@ Code evidence:
When the top-level notebook document feature is enabled, the kernel creates `<boxID>.sy` directly inside the notebook directory. Its document rootID is exactly the notebook ID, so the notebook and its top-level document share one ID.
+The switch is stored at `fileTree.boxDocEnabled` in `conf/conf.json`. It defaults to enabled for new workspaces; an existing configuration that does not contain the field is initialized as disabled.
+
The top-level notebook document is a virtual parent for every ordinary document stored directly under `<boxID>/`. Unlike an ordinary parent document, its children remain in the notebook directory and are **not** moved into a `<boxID>/<boxID>/` directory. For example:
```
@@ -142,8 +160,9 @@ Each notebook directory contains a `.siyuan/` hidden folder, which can contain:
| File | Purpose |
|---|---|
| `conf.json` | Notebook config (BoxConf) |
-| `sort.json` | Custom sort mapping `{docID: order}` |
+| `sort.json` | Optional custom sort mapping `{docID: order}` |
| `boxDoc.json` | Optional; identifies the top-level notebook document in `.sy.zip` notebook exports/imports |
+| `notebook-crypt-backup.json` | Optional; encrypted-notebook backup of the wrapped notebook data-encryption key |
The `BoxConf` struct:
@@ -160,6 +179,8 @@ The `BoxConf` struct:
| `dailyNoteSavePath` | string | Storage path for new daily notes (supports templates) |
| `dailyNoteTemplatePath` | string | Template path for new daily notes |
| `sortMode` | int | Sort mode |
+| `encrypted` | bool | Whether this is an encrypted notebook |
+| `boxCrypt` | object or null | Wrapped notebook data-encryption key and envelope metadata; non-null for encrypted notebooks |
> ⚠️ **The notebook ID is NOT in `conf.json`** — the ID is the notebook directory name itself (see §3).
@@ -167,6 +188,8 @@ Read/write sites: `GetConf` / `SaveConf`. The path is hard-coded as `<DataDir>/<
`boxDoc.json` contains a metadata spec version and the top-level document ID. At runtime the kernel derives that document ID directly from `box.ID`; the metadata file is retained so `.sy.zip` import can identify the source notebook's top-level document and remap it to the destination notebook ID.
+Encrypted notebooks retain recovery metadata in `conf.json` and `notebook-crypt-backup.json`, but their `.sy`, notebook-local assets, and attribute-view definitions are ciphertext. See [ENCRYPTED-NOTEBOOK.md](./ENCRYPTED-NOTEBOOK.md) for the encryption boundary, database isolation, and recovery design.
+
---
## 6. Workspace-wide config: `<workspace>/conf/conf.json`
@@ -178,7 +201,7 @@ Be careful to distinguish the two `conf.json` files:
| `conf/conf.json` | **Workspace root** | Workspace-level global config (appearance / langs / system / editor / sync / repo, etc.) |
| `<boxID>/.siyuan/conf.json` | **Inside a notebook** | That notebook only (BoxConf) |
-In practice `conf/conf.json` is ~42 KB and holds UI appearance, account, sync, AI, flashcard, and all other workspace-level settings.
+`conf/conf.json` holds UI appearance, account, sync, AI, flashcard, and other workspace-level settings.
---
@@ -195,6 +218,10 @@ In practice `conf/conf.json` is ~42 KB and holds UI appearance, account, sync, A
| `refsearchignore` | Ref-search ignore rules |
| `publishAccess.json` | Publishing access control |
| `filesys_status_check/` | File-system consistency-check state |
+| `notebook-crypto-backup.json` | Global encrypted-notebook KDF and verifier backup used for sync/recovery |
+| `master-password-migration.json` | Temporary crash-recovery state during master-password migration |
+
+Most entries are optional and are created only when the corresponding feature is used.
> There is **no** `conf.json` here — the workspace config lives at the workspace root's `conf/conf.json` (see §6).
@@ -204,11 +231,13 @@ In practice `conf/conf.json` is ~42 KB and holds UI appearance, account, sync, A
Two coexisting locations:
-1. **Global `data/assets/`** — the main one. Naming convention looks like `<orig-name>-<docID-suffix>.<ext>` (e.g. `640-20240927104411-0jh7x96.webp`). Referenced inside documents as the relative path `assets/xxx`.
-2. **Per-notebook `<notebook>/assets/`** — mostly used by the built-in guide notebook; ordinary user notebooks generally don't have one.
+1. **Global `data/assets/`** — the main one. Naming convention looks like `<original-base>-<NodeID>.<ext>` (e.g. `640-20240927104411-0jh7x96.webp`). Referenced inside documents as the relative path `assets/xxx`.
+2. **Per-notebook `<notebook>/assets/`** — used by the built-in guide and required for encrypted notebooks; ordinary unencrypted user notebooks generally don't have one.
Asset-link prefix recognition: only `assets/`, `emojis/`, `plugins/`, `public/`, and `widgets/` are accepted as legal asset-link prefixes.
+Encrypted notebooks must use notebook-local assets. Their contents and `.names.json` original-name mapping are encrypted, and their physical filenames use a desensitized `<16-random-characters>-<blockID>.<ext>` form; direct inspection does not recover the original asset name. See [ENCRYPTED-NOTEBOOK.md §7](./ENCRYPTED-NOTEBOOK.md#7-sy--assets--database-file-encryption).
+
---
## 9. ID and path relationships
@@ -225,7 +254,9 @@ Asset-link prefix recognition: only `assets/`, `emojis/`, `plugins/`, `public/`,
- A document's `.sy` filename (without `.sy`) = the document rootID.
- A notebook's directory name = the notebook ID.
- A top-level notebook document's filename and rootID both equal the notebook ID: `<boxID>.sy`.
-- The kernel enforces `filename ID == root.ID` and auto-corrects mismatches.
+- The kernel enforces `filename ID == root.ID`; unencrypted documents are auto-corrected on mismatch, while encrypted documents reject the mismatch.
+
+These are physical identifiers. A notebook's display name comes from `BoxConf.name`, and a document's displayed title comes from its root attributes; neither display value has to match the NodeID format.
### Document absolute path
@@ -237,10 +268,10 @@ Extracting the doc ID from a path (`GetTreeID`): simply `filepath.Base(path)` wi
## 10. How HPath (human-readable path) is derived
-HPath is **not stored** — it is computed on the fly when loading a document (`LoadTreeByData`):
+HPath is **not authoritative data stored in the `.sy` source file** — it is derived when loading a document (`LoadTreeByData`) and may be cached in rebuildable indexes such as `blocktree.db`:
1. Split the document's own relative path by `/`, drop the leading empty segment and the trailing self segment, yielding the **ID segments of each ancestor document**.
-2. For each ancestor ID, build `<ancestorID>.sy` and read its `title` via `DocIAL()` (a **streaming read of `Properties` only**, without parsing the whole tree).
+2. For each ancestor ID, build `<ancestorID>.sy` and read its `title` via `DocIAL()` (a **streaming read of `Properties` only**, without parsing the whole tree). Encrypted `.sy` files must first be read and decrypted in full before the properties can be parsed.
3. Join the titles with `/` and append the current document's title → an HPath like `/Parent/Child/Current`.
4. If some ancestor `.sy` is missing, the kernel **auto-creates** an `Untitled` parent (issue #7376).
@@ -252,16 +283,19 @@ The top-level notebook document is handled specially: its API HPath is `/`, its
## 11. Notes on operating the file system directly
-> Consistent with [SY-FORMAT.md §0.5](./SY-FORMAT.md): **prefer HTTP API / MCP / CLI** for mutating data, so the kernel handles index synchronization. Direct file-system operations are only for bulk offline migration, cold init, and similar scenarios.
+> Consistent with [SY-FORMAT.md §0.5](./SY-FORMAT.md#05-when-to-readwrite-sy-directly-priority-order): **prefer HTTP API / MCP / CLI** for mutating data, so the kernel handles index synchronization. Direct file-system operations are only for bulk offline migration, cold init, and similar scenarios.
+
+Do not directly mutate encrypted-notebook persistent files. Raw file APIs reject reading, enumerating, or modifying encrypted-notebook directories. Offline inspection of protected content exposes ciphertext, although non-content metadata such as `conf.json` remains readable. Use the dedicated APIs after unlocking the notebook so encryption, authentication, and isolated indexes remain consistent.
When writing files directly:
-1. ☐ Notebook/document names must match the NodeID format and **avoid reserved names** (§3).
+1. ☐ Notebook and document physical IDs must match the NodeID format and **avoid reserved names** (§3).
2. ☐ A document's `.sy` filename equals its rootID (§9).
3. ☐ A document with children must have the **paired same-named directory**; moving the parent means moving that directory too (§4).
4. ☐ Do not move root-level documents into `<boxID>/<boxID>/`; the top-level notebook document uses a virtual parent-child mapping (§4).
5. ☐ A notebook's name/icon/closed state lives in `<boxID>/.siyuan/conf.json`; the name and icon are synchronized with `<boxID>.sy` when that document exists (§5).
-6. ☐ After changes you usually need to **rebuild the index**, or search/block-refs/breadcrumbs will be stale.
+6. ☐ Treat `data/storage/` as persistent source data and `temp/*.db` as rebuildable indexes; do not interchange their lifecycle (§3).
+7. ☐ After changes you usually need to **rebuild the index**, or search/block-refs/breadcrumbs will be stale.
---
diff --git a/docs/WORKSPACE.zh-CN.md b/docs/WORKSPACE.zh-CN.md
index 72d55b14c8..5a34f644c9 100644
--- a/docs/WORKSPACE.zh-CN.md
+++ b/docs/WORKSPACE.zh-CN.md
@@ -6,7 +6,7 @@
## 0. 一句话本质
-SiYuan 工作区是一棵**自描述**的目录树:笔记本、文档、附件都以可读的文件/目录形式存在磁盘上。**文件名即 ID,目录结构即文档层级**。没有用二进制数据库来记录「文档在哪」——文件系统本身就是真相来源(single source of truth),内核通过遍历目录即可还原全部笔记本与文档结构。
+SiYuan 工作区是一棵**自描述**的目录树:笔记本、文档和资源文件以文件或目录形式存在磁盘上。**文件名即 ID,目录结构即文档层级**。文件系统保存权威的原始内容,`temp/` 下的 SQLite 数据库则是可重建的索引和缓存。普通笔记本内容可以直接读取;加密笔记本内容以密文落盘,必须通过已解锁的内核访问。
---
@@ -14,33 +14,44 @@ SiYuan 工作区是一棵**自描述**的目录树:笔记本、文档、附件
```
<工作区根>/ 例 F:\SiYuan\
+├── .lock # 运行时工作区锁
├── conf/
-│ ├── conf.json # ★工作区总配置(appearance/system/sync/editor...)
-│ ├── ca.crt / cert.pem / key.pem # TLS 证书
-│ └── windowState.json
+│ ├── conf.json # ★工作区总配置(appearance/system/sync/editor...)
+│ ├── appearance/ # 已安装的主题、图标和语言资源
+│ ├── ca.crt / ca.key / cert.pem / key.pem # TLS 证书和密钥
+│ └── windowState.json # 桌面端窗口状态
├── data/ # DataDir — 所有笔记本数据的根
-│ ├── .siyuan/ # 工作区级隐藏配置(*ignore 规则等)
-│ ├── assets/ # ★全局附件(图片/音视频/文件)
-│ ├── templates/ # 全局模板(.md)
+│ ├── .siyuan/ # 工作区级隐藏配置
+│ │ ├── syncignore / searchignore / embeddingignore / indexignore / refsearchignore
+│ │ └── notebook-crypto-backup.json # 可选的加密笔记本全局恢复元数据
+│ ├── assets/ # ★全局资源文件(图片/音视频/文件)
+│ ├── templates/ # 全局模板(.md)
│ ├── widgets/ # 挂件
│ ├── plugins/ # 插件
│ ├── emojis/ # 自定义 emoji
-│ ├── snippets/ # 代码片段(CSS/JS)
+│ ├── snippets/ # 代码片段(CSS/JS)
│ ├── public/ # 静态资源
-│ ├── storage/ # 运行时索引/缓存/数据库
-│ └── <boxID>/ # ★笔记本(目录名 = 笔记本 ID)
+│ ├── storage/ # 持久化结构化数据(属性视图、插件状态等)
+│ └── <boxID>/ # ★笔记本(目录名 = 笔记本 ID)
│ ├── .siyuan/
-│ │ ├── conf.json # ★笔记本配置(BoxConf:name/icon/closed...)
-│ │ ├── sort.json # 自定义排序映射 {docID: order}
-│ │ └── boxDoc.json # 可选的顶层笔记本文档标记,用于 .sy.zip 导入导出
+│ │ ├── conf.json # ★笔记本配置(BoxConf:name/icon/closed...)
+│ │ ├── sort.json # 可选的自定义排序映射 {docID: order}
+│ │ ├── boxDoc.json # 可选的顶层笔记本文档标记,用于 .sy.zip 导入导出
+│ │ └── notebook-crypt-backup.json # 可选的加密笔记本密钥包络备份
│ ├── <boxID>.sy # 可选的顶层笔记本文档(rootID = 笔记本 ID)
-│ ├── <docID>.sy # 文档(JSON 树;文件名 = 文档 rootID)
-│ └── <docID>/ # ★该文档的子文档目录(同名配对)
-│ ├── <childID>.sy
-│ └── <childID>/ ... # 任意深度嵌套
-├── repo/ # 数据仓库(快照/同步)
+│ ├── <docID>.sy # 文档(JSON 树;文件名 = 文档 rootID)
+│ ├── <docID>/ # ★该文档的子文档目录(同名配对)
+│ │ ├── <childID>.sy
+│ │ └── <childID>/ ... # 任意深度嵌套
+│ ├── assets/ # 笔记本本地资源文件(加密笔记本必须使用)
+│ └── storage/av/ # 加密笔记本属性视图定义
+├── repo/ # 数据仓库(快照/同步)
├── history/ # 编辑历史归档
-└── temp/ # 临时/导出文件
+├── corrupted/ # 可选的损坏数据隔离目录
+└── temp/ # 可重建索引、队列、缓存、日志和导出文件
+ ├── siyuan.db / blocktree.db / asset_content.db / history.db
+ ├── siyuan-encrypted-<boxID>.db / siyuan-encrypted-<boxID>-blocktree.db
+ └── queue/ / export/ / os/ / siyuan.log
```
---
@@ -49,16 +60,17 @@ SiYuan 工作区是一棵**自描述**的目录树:笔记本、文档、附件
工作区根由用户选定(如 `F:\SiYuan\`)。判定一个目录是否为合法工作区:`conf/conf.json` 存在且含 `kernelVersion` 字段。
-根目录下固定子目录:
+工作区根目录条目:
-| 子目录 | 用途 |
+| 条目 | 用途 |
|---|---|
-| `conf/` | 工作区级配置 + TLS 证书 |
+| `.lock` | 防止两个内核同时打开同一工作区的运行时锁 |
+| `conf/` | 工作区级配置、外观资源及 TLS 证书和密钥 |
| `data/` | **所有笔记本数据**(本文档核心) |
| `repo/` | 数据快照仓库(用于同步/历史) |
| `history/` | 编辑历史归档 |
-| `temp/` | 临时/导出文件 |
-| `corrupted/` | 损坏数据隔离区(内核自动迁入) |
+| `temp/` | 可重建索引、队列、缓存、日志和导出文件 |
+| `corrupted/` | 可选的损坏数据隔离区(需要时由内核自动创建) |
---
@@ -66,12 +78,12 @@ SiYuan 工作区是一棵**自描述**的目录树:笔记本、文档、附件
`data/` 第一层是两类东西混排:
-1. **保留资源目录**:`.siyuan/`、`assets/`、`templates/`、`widgets/`、`plugins/`、`emojis/`、`snippets/`、`public/`、`storage/`。
+1. **固定数据目录**:`.siyuan/`、`assets/`、`templates/`、`widgets/`、`plugins/`、`emojis/`、`snippets/`、`public/`、`storage/`。
2. **笔记本目录**:每个是一个以笔记本 ID 命名的文件夹。
### 保留文件名清单(★ AI 落盘时务必避开)
-`IsReservedFilename`:
+`IsReservedFilename`:
```go
func IsReservedFilename(baseName string) bool {
@@ -80,15 +92,19 @@ func IsReservedFilename(baseName string) bool {
}
```
-即 `assets` / `templates` / `widgets` / `emojis` / `.siyuan` 以及任意 `.` 开头的名字**都不能**用作笔记本名或文档名。
+即物理路径段 `assets` / `templates` / `widgets` / `emojis` / `.siyuan` 以及任意以 `.` 开头的名称均为保留名称。该限制针对磁盘上的 ID 和路径段,不限制用户界面中显示的笔记本名称或文档标题。
+
+### 持久化数据与可重建索引
+
+`data/storage/` 保存属性视图定义和插件状态等持久化结构化数据。`temp/` 中的 SQLite 文件是派生索引和缓存:普通笔记本使用全局 `siyuan.db` 和 `blocktree.db`,每个已解锁的加密笔记本则使用独立的 SQLCipher 数据库。删除或重建索引不能与删除源 `.sy`、资源文件或数据库定义文件混为一谈。
### 笔记本的判定规则
`ListNotebooks` 枚举 `data/` 时,逐个目录判定:
1. 跳过保留名;
-2. 必须是目录(非文件);
-3. 目录名必须符合 NodeID 格式(`ast.IsNodeIDPattern`);
+2. 必须是目录(非文件);
+3. 目录名必须符合 NodeID 格式(`ast.IsNodeIDPattern`);
4. 满足以上三条 → 该目录名就是 `box.ID`(笔记本 ID)。
---
@@ -99,13 +115,13 @@ func IsReservedFilename(baseName string) bool {
> **文档 `A` 的磁盘表示 = `A.sy` 文件 + `A/` 目录(后者仅在 A 有子文档时存在)。A 的子文档全部放在 `A/` 目录里;子文档若有自己的子文档,则再嵌套 `A/B/` 目录,任意深度。**
-真实示例(来自 `find` 输出):
+真实示例(来自 `find` 输出):
```
20221126104620-m06prws/ # 父文档 A 的目录
20221126104620-m06prws.sy # 父文档 A 本体
20221126104620-m06prws/20230928134805-z11t56h.sy # A 的子文档 B
-20221126104620-m06prws/20230928134805-z11t56h/ # B 的子目录(孙子层)
+20221126104620-m06prws/20230928134805-z11t56h/ # B 的子目录(孙子层)
20221126104620-m06prws/20230928134805-z11t56h/20240304105333-v7g5j1s.sy
```
@@ -114,12 +130,14 @@ func IsReservedFilename(baseName string) bool {
- 展开子文档(`Ls`):传入 `.sy` 路径,去掉 `.sy` 后缀,检查同名目录是否存在,存在则列出其子项。
- 子文档深度(`GetChildDocDepth`):同理 walk 同名目录。
-> ⚠️ 这意味着:**移动 / 改名一个有子文档的文档,必须同步移动它的同名目录**,否则子文档会失联。
+> ⚠️ 这意味着:**移动或改名一个有子文档的文档,必须同步移动它的同名目录**,否则子文档会失联。
### 顶层笔记本文档例外
启用顶层笔记本文档功能后,内核会在笔记本目录中直接创建 `<boxID>.sy`。该文档的 rootID 就是笔记本 ID,因此笔记本与其顶层文档共用同一个 ID。
+开关保存在 `conf/conf.json` 的 `fileTree.boxDocEnabled` 中。新工作区默认启用;已有配置中缺少该字段时初始化为关闭。
+
顶层笔记本文档是笔记本根层所有普通文档的虚拟父文档。与普通父文档不同,它的子文档仍然位于笔记本目录中,**不会**移动到 `<boxID>/<boxID>/` 目录。例如:
```
@@ -142,8 +160,9 @@ data/<boxID>/
| 文件 | 用途 |
|---|---|
| `conf.json` | 笔记本配置(BoxConf) |
-| `sort.json` | 文档自定义排序映射 `{docID: order}` |
+| `sort.json` | 可选的文档自定义排序映射 `{docID: order}` |
| `boxDoc.json` | 可选;在笔记本 `.sy.zip` 导入导出中标识顶层笔记本文档 |
+| `notebook-crypt-backup.json` | 可选;加密笔记本中已封装数据加密密钥的备份 |
`BoxConf` 结构:
@@ -160,6 +179,8 @@ data/<boxID>/
| `dailyNoteSavePath` | string | 新建日记的存储路径(支持模板) |
| `dailyNoteTemplatePath` | string | 新建日记的模板路径 |
| `sortMode` | int | 排序方式 |
+| `encrypted` | bool | 是否为加密笔记本 |
+| `boxCrypt` | object 或 null | 已封装的笔记本数据加密密钥及包络元数据,在加密笔记本中为非空 |
> ⚠️ **笔记本 ID 不在 `conf.json` 里**——ID 就是笔记本目录名本身(见 §3 的判定规则)。
@@ -167,6 +188,8 @@ data/<boxID>/
`boxDoc.json` 包含元数据规范版本和顶层文档 ID。运行时内核直接从 `box.ID` 推导该文档 ID;保留此元数据文件是为了让 `.sy.zip` 导入过程能够识别源笔记本的顶层文档,并将其重新映射为目标笔记本 ID。
+加密笔记本在 `conf.json` 和 `notebook-crypt-backup.json` 中保留恢复元数据,但其 `.sy`、笔记本本地资源文件和属性视图定义均以密文落盘。加密边界、数据库隔离和恢复设计详见 [ENCRYPTED-NOTEBOOK.zh-CN.md](./ENCRYPTED-NOTEBOOK.zh-CN.md)。
+
---
## 6. 工作区总配置:`<workspace>/conf/conf.json`
@@ -178,7 +201,7 @@ data/<boxID>/
| `conf/conf.json` | **工作区根** | 工作区级总配置(appearance / langs / system / editor / sync / repo 等全局段) |
| `<boxID>/.siyuan/conf.json` | **笔记本内** | 仅该笔记本(BoxConf) |
-实测 `conf/conf.json` 约 42KB,包含 UI 外观、账号、同步、AI、闪卡等全部工作区级设置。
+`conf/conf.json` 包含 UI 外观、账号、同步、AI、闪卡等工作区级设置。
---
@@ -195,19 +218,25 @@ data/<boxID>/
| `refsearchignore` | 反链搜索忽略 |
| `publishAccess.json` | 发布访问控制 |
| `filesys_status_check/` | 文件系统一致性检查状态 |
+| `notebook-crypto-backup.json` | 用于同步和恢复的全局加密笔记本 KDF 及校验器备份 |
+| `master-password-migration.json` | 修改主密码期间临时使用的崩溃恢复状态 |
+
+其中大多数条目都是可选的,仅在使用相应功能时创建。
> 这里**没有** `conf.json`——工作区总配置在工作区根的 `conf/conf.json`(见 §6)。
---
-## 8. 附件(assets)
+## 8. 资源文件(assets)
两套并存:
-1. **工作区级全局 `data/assets/`**——主用。命名规则形如 `<原名>-<docID后缀>.<ext>`(如 `640-20240927104411-0jh7x96.webp`)。文档内以 `assets/xxx` 相对路径引用。
-2. **笔记本级 `<notebook>/assets/`**——主要是内置 guide 笔记本用,普通用户笔记本一般没有。
+1. **工作区级全局 `data/assets/`**——主用。命名规则形如 `<原始名称>-<NodeID>.<ext>`(如 `640-20240927104411-0jh7x96.webp`)。文档内以 `assets/xxx` 相对路径引用。
+2. **笔记本级 `<notebook>/assets/`**——内置用户指南使用,加密笔记本也必须使用;普通未加密用户笔记本一般没有。
+
+资源文件链接前缀识别:仅认 `assets/`、`emojis/`、`plugins/`、`public/`、`widgets/` 这几个前缀为合法资源文件链接。
-附件链接前缀识别:仅认 `assets/`、`emojis/`、`plugins/`、`public/`、`widgets/` 这几个前缀为合法附件链接。
+加密笔记本必须使用笔记本本地资源文件。资源文件内容和记录原始名称映射的 `.names.json` 均会加密,物理文件名采用脱敏的 `<16 位随机字符>-<blockID>.<ext>` 格式,因此直接检查磁盘无法还原原始资源文件名称。详见 [ENCRYPTED-NOTEBOOK.zh-CN.md §7](./ENCRYPTED-NOTEBOOK.zh-CN.md#7-sy--assets--数据库文件加解密)。
---
@@ -225,11 +254,13 @@ data/<boxID>/
- 文档的 `.sy` 文件名(去 `.sy` 后缀)= 文档 rootID。
- 笔记本的目录名 = 笔记本 ID。
- 顶层笔记本文档的文件名和 rootID 都等于笔记本 ID,即 `<boxID>.sy`。
-- 内核强制 `文件名 ID == root.ID`,不一致会自动订正。
+- 内核强制 `文件名 ID == root.ID`;未加密文档不一致时会自动订正,加密文档则拒绝加载。
+
+以上均为物理标识。笔记本显示名称来自 `BoxConf.name`,文档显示标题来自根块属性,两者都不需要符合 NodeID 格式。
### 文档绝对路径
-文档在磁盘上的绝对路径 = `data/<boxID>/<相对路径>`,其中 `<相对路径>` 形如 `/20221126104620-m06prws/20230928134805-z11t56h.sy`(以 `/` 开头,POSIX 风格)。
+文档在磁盘上的绝对路径 = `data/<boxID>/<相对路径>`,其中 `<相对路径>` 形如 `/20221126104620-m06prws/20230928134805-z11t56h.sy`(以 `/` 开头,POSIX 风格)。
从路径取文档 ID(`GetTreeID`):即 `filepath.Base(路径)` 去掉 `.sy` 后缀。
@@ -237,10 +268,10 @@ data/<boxID>/
## 10. HPath(人类可读路径)如何派生
-HPath **不存储**,加载文档时现场计算(`LoadTreeByData`):
+HPath **不是存储在 `.sy` 原始文件中的权威数据**,而是在加载文档时通过 `LoadTreeByData` 派生,并可能缓存到 `blocktree.db` 等可重建索引中:
1. 文档自身的相对路径按 `/` 拆分,去掉开头空段和结尾自身段,得到**各级父文档的 ID 段**。
-2. 对每个父 ID,拼出 `<父ID>.sy`,用 `DocIAL()`(**流式只读 `Properties`**,不解析整棵树)读出该父文档的 `title`。
+2. 对每个父 ID,拼出 `<父ID>.sy`,用 `DocIAL()`(**流式只读 `Properties`**,不解析整棵树)读出该父文档的 `title`。加密 `.sy` 文件必须先完整读取并解密,然后才能解析属性。
3. 把各级 title 用 `/` 串起来,末尾接当前文档 title → 得到形如 `/父标题/子标题/当前标题` 的 HPath。
4. 若某父 `.sy` 缺失,内核会**自动补建**一个 `Untitled` 父文档(对应 issue #7376)。
@@ -252,16 +283,19 @@ HPath **不存储**,加载文档时现场计算(`LoadTreeByData`):
## 11. 直接操作文件系统的注意事项
-> 与 [SY-FORMAT.zh-CN.md §0.5](./SY-FORMAT.zh-CN.md) 一致:**修改数据优先走 HTTP API / MCP / CLI**,由内核负责索引同步。直接操作文件系统仅用于批量离线迁移、冷初始化等场景。
+> 与 [SY-FORMAT.zh-CN.md §0.5](./SY-FORMAT.zh-CN.md#05-何时直接读写-sy优先级) 一致:**修改数据优先走 HTTP API / MCP / CLI**,由内核负责索引同步。直接操作文件系统仅用于批量离线迁移、冷初始化等场景。
+
+不要直接修改加密笔记本的持久化文件。原始文件 API 会拒绝读取、枚举或修改加密笔记本目录。离线检查受保护内容时只能看到密文,但 `conf.json` 等非内容元数据仍可读取。应在解锁笔记本后使用专用 API,以保持加密、认证和隔离索引的一致性。
直接落盘时务必:
-1. ☐ 笔记本/文档命名符合 NodeID 格式,且**避开保留名**(§3)。
-2. ☐ 文档 `.sy` 文件名 == 文档 rootID(§9)。
+1. ☐ 笔记本和文档的物理 ID 符合 NodeID 格式,且**避开保留名**(§3)。
+2. ☐ 文档 `.sy` 文件名 == 文档 rootID(§9)。
3. ☐ 有子文档的文档,必须**配套同名目录**;移动父文档时连带移动同名目录(§4)。
4. ☐ 不要将根层文档移动到 `<boxID>/<boxID>/` 中;顶层笔记本文档使用虚拟父子路径映射(§4)。
5. ☐ 笔记本的名称、图标和关闭状态写在 `<boxID>/.siyuan/conf.json` 中;当 `<boxID>.sy` 存在时,名称和图标会与它同步(§5)。
-6. ☐ 改动后通常需要**重建索引**,否则搜索、块引用和面包屑会失效。
+6. ☐ 将 `data/storage/` 视为持久化源数据,将 `temp/*.db` 视为可重建索引,不要混淆二者的生命周期(§3)。
+7. ☐ 改动后通常需要**重建索引**,否则搜索、块引用和面包屑会失效。
---
@@ -269,7 +303,7 @@ HPath **不存储**,加载文档时现场计算(`LoadTreeByData`):
| 文档 | 范畴 | 回答的问题 |
|---|---|---|
-| **本文档(WORKSPACE)** | 文件系统层 | 「一个工作区在磁盘上长什么样?笔记本/文档/附件怎么组织?」 |
-| **SY-FORMAT** | AST 层 | 「一个 `.sy` 文件内部的 JSON 树长什么样?有哪些节点类型/字段?」 |
+| **本文档(WORKSPACE)** | 文件系统层 | 「一个工作区在磁盘上长什么样?笔记本、文档和资源文件怎么组织?」 |
+| **SY-FORMAT** | AST 层 | 「一个 `.sy` 文件内部的 JSON 树长什么样?有哪些节点类型和字段?」 |
两者互补:本文档告诉你 `.sy` 文件**放在哪、叫什么名、与谁同目录**;SY-FORMAT 告诉你 `.sy` 文件**里面写什么**。