{"record":{"id":"a6b6d96a35dc56c0","repo":"siyuan-note/siyuan","slug":"stat-encrypted-notebook-history-backup-s-failed","errorCode":null,"errorMessage":"stat encrypted notebook history backup [%s] failed: %w","messagePattern":"stat encrypted notebook history backup \\[(.+?)\\] failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":939,"sourceCode":"func HasEncryptedNotebookHistory() bool {\n\thasHistory, err := scanEncryptedNotebookHistory()\n\tif err != nil {\n\t\tlogging.LogErrorf(\"scan encrypted notebook history failed: %s\", err)\n\t\treturn true\n\t}\n\treturn hasHistory\n}\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}","sourceCodeStart":921,"sourceCodeEnd":957,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L921-L957","documentation":"isEncryptedHistoryBoxDir decides whether a history box dir is an encrypted notebook box by probing .siyuan/notebook-crypto-backup.json. A successful stat or a clean not-exist is handled; this error is thrown when os.Stat fails with any other error (permission denied, I/O error, path too long), aborting the classification because the box's encryption status cannot be safely determined.","triggerScenarios":"encryptedNotebookHistoryBoxDirs, hasEncryptedNotebookDeleteHistory, or IsEncryptedHistoryPath calling isEncryptedHistoryBoxDir where os.Stat on <boxDir>/.siyuan/notebook-crypto-backup.json returns e.g. EACCES, ELOOP, or ENOTDIR (the .siyuan path component being a regular file).","commonSituations":"A file named .siyuan (instead of a directory) left by a bad restore; permissions tightened after copying the workspace between machines; symlink loops introduced by sync tools.","solutions":["Inspect <boxDir>/.siyuan — it must be a directory; fix or remove it if a file/symlink replaced it","Fix permissions on the box directory and its .siyuan path for the kernel process user","Resolve symlink loops or stale links created by sync/backup tooling","Address the OS-level cause reported in the wrapped error (disk I/O, path length) and rescan"],"exampleFix":"// before: assuming stat failure means \"not encrypted\"\nenc, err := isEncryptedHistoryBoxDir(boxDir)\nif err != nil { enc = false }\n// after: surface the real cause and skip only genuinely unreadable boxes\nenc, err := isEncryptedHistoryBoxDir(boxDir)\nif err != nil {\n    log.Printf(\"cannot classify history box %s: %v\", boxDir, err)\n    return // do not treat as unencrypted; keep recovery material\n}","handlingStrategy":"try-catch","validationCode":"siyuanPath := filepath.Join(boxDir, \".siyuan\")\nif info, err := os.Stat(siyuanPath); err == nil && !info.IsDir() {\n    return fmt.Errorf(\"%s must be a directory\", siyuanPath)\n}","typeGuard":"func boxDirClassifiable(boxDir string) bool {\n    f, err := os.Open(filepath.Join(boxDir, \".siyuan\"))\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) {\n        // fix the .siyuan path problem before classifying\n    }\n    return err // do not default to \"not encrypted\"\n}","preventionTips":["Never replace .siyuan directories with files during restores","Keep consistent ownership when migrating workspaces","Avoid symlink tricks on workspace metadata","Fix disk/path issues before running history scans"],"tags":["filesystem","stat","history","encryption"],"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"}