{"record":{"id":"738f3748fde1739f","repo":"siyuan-note/siyuan","slug":"stat-encrypted-notebook-history-conf-s-failed","errorCode":null,"errorMessage":"stat encrypted notebook history conf [%s] failed: %w","messagePattern":"stat encrypted notebook history conf \\[(.+?)\\] failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":946,"sourceCode":"}\n\n// isEncryptedHistoryBoxDir 判断历史目录中的 boxID 子目录是否属于加密笔记本。\n// 优先看 notebook-crypto-backup.json（删除前随 box 目录整体备份，是加密身份的权威标识），\n// 再 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","sourceCodeStart":928,"sourceCodeEnd":964,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L928-L964","documentation":"When no notebook-crypto-backup.json exists in a history box, isEncryptedHistoryBoxDir probes .siyuan/conf.json to read the box's Encrypted flag. Not-exist means \"not an encrypted box\" (returns false, nil); this error is thrown when the stat itself fails with another error (permissions, I/O, ENOTDIR), since the box's status is then unknown and callers must not treat recovery material as absent.","triggerScenarios":"Callers (encryptedNotebookHistoryBoxDirs, hasEncryptedNotebookDeleteHistory, IsEncryptedHistoryPath) hitting a box dir where os.Stat on <boxDir>/.siyuan/conf.json fails with EACCES, EIO, ENOTDIR (e.g. .siyuan is a file), or a similar non-ENOENT error.","commonSituations":"Broken restore leaving .siyuan as a regular file; restrictive permissions after migrating the workspace between users; antivirus or backup locking; disk errors.","solutions":["Fix read permissions on <boxDir>/.siyuan/conf.json and its parent directories","If .siyuan is a file or broken symlink rather than a directory, restore the proper directory from backup","Release locks from security/backup software and retry the scan","Restore the box's .siyuan/conf.json from a backup copy, then rerun history detection"],"exampleFix":"// before: ignoring the error and assuming the box is plain\nenc, err := isEncryptedHistoryBoxDir(dir)\nif err != nil { continue }\n// after: verify the .siyuan layout before classification\nsiyuanInfo, sErr := os.Stat(filepath.Join(dir, \".siyuan\"))\nif sErr != nil || !siyuanInfo.IsDir() {\n    return fmt.Errorf(\"box %s: .siyuan missing or not a directory: %w\", dir, sErr)\n}\nenc, err = isEncryptedHistoryBoxDir(dir)","handlingStrategy":"try-catch","validationCode":"confPath := filepath.Join(boxDir, \".siyuan\", \"conf.json\")\nif _, err := os.Stat(confPath); err != nil && !os.IsNotExist(err) {\n    // stat-level problem — fix before calling history classification\n}","typeGuard":"func confReadable(confPath string) bool {\n    f, err := os.Open(confPath)\n    if err != nil { return false }\n    defer f.Close()\n    return true\n}","tryCatchPattern":"enc, err := isEncryptedHistoryBoxDir(boxDir)\nif err != nil {\n    var perr *fs.PathError\n    if errors.As(err, &perr) && errors.Is(perr.Err, syscall.EACCES) {\n        // repair permissions on .siyuan/conf.json, then retry\n    }\n    return err\n}","preventionTips":["Ensure .siyuan is always a plain readable directory","Restore conf.json from backup if the box metadata is damaged","Stop backup/AV tools from locking workspace files during scans","Keep conf.json intact when copying or syncing boxes"],"tags":["filesystem","stat","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"}