docs: file_uuid generation rules for M4
This commit is contained in:
69
docs_v1.0/M4_workspace/2026-05-17_file_uuid_rule.md
Normal file
69
docs_v1.0/M4_workspace/2026-05-17_file_uuid_rule.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# file_uuid 生成規則
|
||||
|
||||
## 公式
|
||||
|
||||
```
|
||||
file_uuid = SHA256(mac_address | birthday | physical_path_at_birth | filename)[0:32]
|
||||
```
|
||||
|
||||
- 使用 SHA-256 雜湊
|
||||
- 四個元件以 `|` 串接
|
||||
- 取 64 字元 hex 的前 32 字元作為 file_uuid
|
||||
- 結果為 32 字元小寫 hex(0-9, a-f),**不含連字號**
|
||||
|
||||
## 輸入元件
|
||||
|
||||
| 元件 | 格式 | 範例 | 說明 |
|
||||
|------|------|------|------|
|
||||
| `mac_address` | `xx:xx:xx:xx:xx:xx` | `a1:b2:c3:d4:e5:f6` | 內建網路介面 MAC,優先 en0 > en1 > en2 |
|
||||
| `birthday` | ISO 8601 + timezone | `2026-04-27T22:00:00+08:00` | **檔案 mtime**(fs::metadata().modified()),固定不變 |
|
||||
| `physical_path_at_birth` | 絕對路徑 | `/Users/demo/raw/video.mp4` | 註冊時的正規化絕對路徑,尾端 `/` 會去除 |
|
||||
| `filename` | 含副檔名 | `video.mp4` | 純檔名含副檔名 |
|
||||
|
||||
## 特性
|
||||
|
||||
- **Deterministic(確定性)**:相同輸入永遠產生相同輸出
|
||||
- **Location-bound(位置綁定)**:不同路徑產生不同 file_uuid → 搬移檔案 = 新 identity
|
||||
- **MAC-bound(機器綁定)**:不同機器產生的 file_uuid 不同
|
||||
- **No hyphen(無連字號)**:32 字元純 hex,與 `identity_uuid`(可含連字號)格式不同
|
||||
- **128 bits entropy**:32 hex chars = 128 bits
|
||||
|
||||
## MAC 位址擷取規則
|
||||
|
||||
1. 執行 `ifconfig -a`
|
||||
2. 解析 `ether` / `lladdr` 欄位
|
||||
3. 優先順序:en0(Wi-Fi) > en1 > en2 > 其他 en* > 所有其餘介面
|
||||
4. 排除 `00:00:00:00:00:00`、`ff:ff:ff:ff:ff:ff`、USB/Thunderbolt 非內建介面
|
||||
5. 無法取得時 fallback 為 `00:00:00:00:00:00`
|
||||
|
||||
## birthday 說明
|
||||
|
||||
`birthday` 實際上採用**檔案 mtime**(`fs::metadata().modified()`),而非 birthtime(btime)。原因:
|
||||
|
||||
- `rsync -a` 會保留 mtime,但不會保留 btime
|
||||
- 使用 mtime 確保檔案透過 rsync 在不同機器間傳輸後,file_uuid 仍保持一致
|
||||
- `birthday` 在第一次計算後永久固定,永不修改
|
||||
|
||||
## 驗證規則
|
||||
|
||||
```rust
|
||||
pub fn is_birth_uuid(uuid: &str) -> bool {
|
||||
uuid.len() == 32 && !uuid.contains('_')
|
||||
}
|
||||
```
|
||||
|
||||
## 與 identity_uuid 的差異
|
||||
|
||||
| | file_uuid | identity_uuid |
|
||||
|---|---|---|
|
||||
| 長度 | 32 chars | 32 chars |
|
||||
| 連字號 | 無 | 可有可無(API 皆可接受) |
|
||||
| 生成方式 | SHA256(mac\|birthday\|path\|filename) | gen_random_uuid() |
|
||||
| 確定性 | ✅ 確定性(相同輸入→相同輸出) | ❌ 每次不同 |
|
||||
| 用途 | 檔案識別、位置綁定 | 人物身分識別 |
|
||||
|
||||
## 原始碼位置
|
||||
|
||||
- **實作**:`src/core/storage/uuid.rs` — `compute_birth_uuid()`, `get_mac_address()`, `is_birth_uuid()`
|
||||
- **規格文件**:`docs_v1.0/REFERENCE/file_uuid_spec.md` V2.0
|
||||
- **設計文件**:`docs_v1.0/DESIGN/FILE_LIFECYCLE_V1.0.md`、`docs_v1.0/DESIGN/MARKBASE_DESIGN_V2.0.md`
|
||||
Reference in New Issue
Block a user