{"record":{"id":"716f6d09af9717d4","repo":"siyuan-note/siyuan","slug":"read-encrypted-notebook-history-conf-s-failed","errorCode":null,"errorMessage":"read encrypted notebook history conf [%s] failed: %w","messagePattern":"read encrypted notebook history conf \\[(.+?)\\] failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":950,"sourceCode":"// 再 fallback 到 conf.json 的 Encrypted 标志。\nfunc isEncryptedHistoryBoxDir(boxDir string) (bool, error) {\n\tsiyuanDir := filepath.Join(boxDir, \".siyuan\")\n\tbackupPath := filepath.Join(siyuanDir, \"notebook-crypto-backup.json\")\n\tif _, err := os.Stat(backupPath); err == nil {\n\t\treturn true, nil\n\t} else if !os.IsNotExist(err) {\n\t\treturn false, fmt.Errorf(\"stat encrypted notebook history backup [%s] failed: %w\", boxDir, err)\n\t}\n\tconfPath := filepath.Join(siyuanDir, \"conf.json\")\n\tif _, err := os.Stat(confPath); err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn false, nil\n\t\t}\n\t\treturn false, fmt.Errorf(\"stat encrypted notebook history conf [%s] failed: %w\", boxDir, err)\n\t}\n\tdata, err := filelock.ReadFile(confPath)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"read encrypted notebook history conf [%s] failed: %w\", boxDir, err)\n\t}\n\tvar boxConf conf.BoxConf\n\tif err = gulu.JSON.UnmarshalJSON(data, &boxConf); err != nil {\n\t\treturn false, fmt.Errorf(\"parse encrypted notebook history conf [%s] failed: %w\", boxDir, err)\n\t}\n\treturn boxConf.Encrypted, nil\n}\n\n// cachedDEKs 缓存已解锁加密笔记本的 DEK，按 boxID 索引。\n// KEK 不全局缓存（\"严格每笔记本单独解锁\"语义）：UnlockBox 临时派生 KEK 解出 DEK 后即丢弃 KEK，\n// 仅保留 per-box DEK 供后续读写加解密。\nvar (\n\tcachedDEKs     = map[string][]byte{}\n\tcachedDEKsLock sync.RWMutex\n)\n\n// boxLastAccess 记录每个加密笔记本最近一次真实用户交互或显式保活时间（unix 纳秒），供自动锁定 cron 使用。\n// key: boxID, value: *atomic.Int64。UnlockBox 成功时初始化，Unmount 时清理。","sourceCodeStart":932,"sourceCodeEnd":968,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L932-L968","documentation":"After confirming .siyuan/conf.json exists in a history box, it is read via filelock.ReadFile to inspect the box's Encrypted flag. This error is thrown when the stat succeeded but the read fails (e.g. the file disappeared between stat and read, permissions deny reading, or a locking/I/O error occurs), so the box's encryption status cannot be determined.","triggerScenarios":"os.Stat succeeds on <boxDir>/.siyuan/conf.json but filelock.ReadFile fails: file deleted or renamed by a concurrent history cleanup, read permission denied, sharing violation on Windows, or disk I/O error.","commonSituations":"Concurrent history cleanup running while recovery scans; workspace files copied with read-only-for-others ACLs; Windows indexer holding the file open; failing disk.","solutions":["Re-run the scan — transient races with history cleanup often resolve on retry once nothing is mutating history","Grant the kernel process read permission on the conf.json file","Stop competing processes (backup, sync, antivirus) that delete or lock files under history/ and retry","If the file is gone, restore .siyuan/conf.json from backup so the box can still be classified"],"exampleFix":"// before: single read with no retry\nenc, err := isEncryptedHistoryBoxDir(boxDir)\n// after: bounded retry for transient read failures\nvar enc bool\nvar err error\nfor i := 0; i < 3; i++ {\n    enc, err = isEncryptedHistoryBoxDir(boxDir)\n    if err == nil { break }\n    time.Sleep(100 * time.Millisecond)\n}","handlingStrategy":"retry","validationCode":"confPath := filepath.Join(boxDir, \".siyuan\", \"conf.json\")\nif f, err := os.Open(confPath); err != nil {\n    // unreadable now — fix permissions or stop competing processes first\n} else { f.Close() }","typeGuard":"func fileReadableNow(path string) bool {\n    f, err := os.Open(path)\n    if err != nil { return false }\n    f.Close()\n    return true\n}","tryCatchPattern":"var enc bool\nvar err error\nfor i := 0; i < 3; i++ {\n    enc, err = isEncryptedHistoryBoxDir(boxDir)\n    if err == nil { break }\n    time.Sleep(100 * time.Millisecond) // handles races with cleanup\n}","preventionTips":["Don't run history cleanup concurrently with recovery scans","Exclude the workspace from file-locking indexers/AV","Grant read ACLs to the kernel process user","Retry transient reads before concluding corruption"],"tags":["filesystem","read","history","conf"],"backgroundTag":"file-read-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}