{"record":{"id":"c181a65f60542ce7","repo":"siyuan-note/siyuan","slug":"read-history-dir-failed-w","errorCode":null,"errorMessage":"read history dir failed: %w","messagePattern":"read history dir failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":793,"sourceCode":"// （box 目录已删）。因此 DisableEncryptedNotebook 不能只靠 ListAllEncryptedBoxIDs 判定——\n// 已删除加密笔记本的历史仍依赖当前 MasterSalt/KEKVerifier 才能恢复，禁用并删除备份会让这些\n// 历史永久锁死，违反设计 §19。本函数扫描历史目录识别这类依赖。\n//\n// 判定信号：历史条目 <HistoryDir>/<ts>-<op>/<boxID>/.siyuan/ 下存在\n// notebook-crypto-backup.json（专为 box 删除后的恢复设计），或 conf.json 标记 Encrypted=true。\n// boxID 用 ast.IsNodeIDPattern 校验，避免误判 assets/storage 等非 box 目录。\nfunc scanEncryptedNotebookHistory() (bool, error) {\n\tboxDirs, err := encryptedNotebookHistoryBoxDirs()\n\treturn len(boxDirs) > 0, err\n}\n\nfunc encryptedNotebookHistoryBoxDirs() (ret []string, err error) {\n\tentries, err := os.ReadDir(util.HistoryDir)\n\tif err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn nil, fmt.Errorf(\"read history dir failed: %w\", err)\n\t}\n\tfor _, entry := range entries {\n\t\tif !entry.IsDir() {\n\t\t\tcontinue\n\t\t}\n\t\t// 历史快照目录：<ts>-<op>，其下是各 boxID 子目录\n\t\tsnapshotDir := filepath.Join(util.HistoryDir, entry.Name())\n\t\tboxEntries, readErr := os.ReadDir(snapshotDir)\n\t\tif readErr != nil {\n\t\t\treturn nil, fmt.Errorf(\"read history snapshot [%s] failed: %w\", entry.Name(), readErr)\n\t\t}\n\t\tfor _, boxEntry := range boxEntries {\n\t\t\tif !boxEntry.IsDir() || !ast.IsNodeIDPattern(boxEntry.Name()) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tencrypted, checkErr := isEncryptedHistoryBoxDir(filepath.Join(snapshotDir, boxEntry.Name()))\n\t\t\tif checkErr != nil {\n\t\t\t\treturn nil, checkErr","sourceCodeStart":775,"sourceCodeEnd":811,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L775-L811","documentation":"encryptedNotebookHistoryBoxDirs enumerates history snapshot directories under util.HistoryDir to locate encrypted-notebook recovery boxes. This error is thrown when the history directory cannot be read at all for a reason other than not existing (e.g. permissions or an I/O error); a missing directory deliberately returns nil so callers treat history as absent. The OS error is wrapped for context.","triggerScenarios":"Calling scanEncryptedNotebookHistory or verifyKEKAgainstEncryptedHistory when util.HistoryDir exists but os.ReadDir fails with EACCES, EIO, EMFILE, or a similar non-ENOENT error.","commonSituations":"Workspace data directory permissions changed (moved from another user/machine); the history path is a file instead of a directory; disk errors; AV/backup software holding the directory locked on Windows.","solutions":["Fix permissions on the workspace history directory so the kernel process can read it (chmod/chown or run as the owning user)","Check whether the history path is actually a directory; restore it if a file replaced it","Check the wrapped %w cause (EACCES vs EIO) and address the OS-level problem (disk, antivirus lock, quota)","Restore the history directory from backup if it is damaged"],"exampleFix":"// before: failing the whole scan on one unreadable dir\n// after: pre-flight check before calling history scans\nif info, err := os.Stat(util.HistoryDir); err == nil && !info.IsDir() {\n    return fmt.Errorf(\"history path is not a directory: %s\", util.HistoryDir)\n}\nhasHistory, err := scanEncryptedNotebookHistory()","handlingStrategy":"fallback","validationCode":"if info, err := os.Stat(util.HistoryDir); err != nil || !info.IsDir() {\n    // history dir missing or not a directory — handle before scanning\n}","typeGuard":"func historyDirReadable() bool {\n    f, err := os.Open(util.HistoryDir)\n    if err != nil { return false }\n    f.Close()\n    return true\n}","tryCatchPattern":"boxes, err := scanEncryptedNotebookHistory()\nif err != nil {\n    var perr *fs.PathError\n    if errors.As(err, &perr) && errors.Is(perr.Err, syscall.EACCES) {\n        // fix permissions or fall back to treating history as present\n    }\n    return err\n}","preventionTips":["Keep workspace directories owned by the user running the kernel","Don't replace directories with files during restores","Exclude the workspace from aggressive AV/backup locking","Preserve history/ when copying workspaces between machines"],"tags":["filesystem","io","history","directory"],"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"}