siyuan-note/siyuan · error

read history dir failed

Error message

read history dir failed: %w

What it means

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.

Solutions

  1. Fix permissions on the workspace history directory so the kernel process can read it (chmod/chown or run as the owning user)
  2. Check whether the history path is actually a directory; restore it if a file replaced it
  3. Check the wrapped %w cause (EACCES vs EIO) and address the OS-level problem (disk, antivirus lock, quota)
  4. Restore the history directory from backup if it is damaged

Example fix

// before: failing the whole scan on one unreadable dir
// after: pre-flight check before calling history scans
if info, err := os.Stat(util.HistoryDir); err == nil && !info.IsDir() {
    return fmt.Errorf("history path is not a directory: %s", util.HistoryDir)
}
hasHistory, err := scanEncryptedNotebookHistory()
Defensive patterns

Strategy: fallback

Validate before calling

if info, err := os.Stat(util.HistoryDir); err != nil || !info.IsDir() {
    // history dir missing or not a directory — handle before scanning
}

Type guard

func historyDirReadable() bool {
    f, err := os.Open(util.HistoryDir)
    if err != nil { return false }
    f.Close()
    return true
}

Try / catch

boxes, err := scanEncryptedNotebookHistory()
if err != nil {
    var perr *fs.PathError
    if errors.As(err, &perr) && errors.Is(perr.Err, syscall.EACCES) {
        // fix permissions or fall back to treating history as present
    }
    return err
}

Prevention

When it happens

Trigger: Calling scanEncryptedNotebookHistory or verifyKEKAgainstEncryptedHistory when util.HistoryDir exists but os.ReadDir fails with EACCES, EIO, EMFILE, or a similar non-ENOENT error.

Common situations: 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.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/c181a65f60542ce7. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:793

// (box 目录已删)。因此 DisableEncryptedNotebook 不能只靠 ListAllEncryptedBoxIDs 判定——
// 已删除加密笔记本的历史仍依赖当前 MasterSalt/KEKVerifier 才能恢复,禁用并删除备份会让这些
// 历史永久锁死,违反设计 §19。本函数扫描历史目录识别这类依赖。
//
// 判定信号:历史条目 <HistoryDir>/<ts>-<op>/<boxID>/.siyuan/ 下存在
// notebook-crypto-backup.json(专为 box 删除后的恢复设计),或 conf.json 标记 Encrypted=true。
// boxID 用 ast.IsNodeIDPattern 校验,避免误判 assets/storage 等非 box 目录。
func scanEncryptedNotebookHistory() (bool, error) {
	boxDirs, err := encryptedNotebookHistoryBoxDirs()
	return len(boxDirs) > 0, err
}

func encryptedNotebookHistoryBoxDirs() (ret []string, err error) {
	entries, err := os.ReadDir(util.HistoryDir)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		return nil, fmt.Errorf("read history dir failed: %w", err)
	}
	for _, entry := range entries {
		if !entry.IsDir() {
			continue
		}
		// 历史快照目录:<ts>-<op>,其下是各 boxID 子目录
		snapshotDir := filepath.Join(util.HistoryDir, entry.Name())
		boxEntries, readErr := os.ReadDir(snapshotDir)
		if readErr != nil {
			return nil, fmt.Errorf("read history snapshot [%s] failed: %w", entry.Name(), readErr)
		}
		for _, boxEntry := range boxEntries {
			if !boxEntry.IsDir() || !ast.IsNodeIDPattern(boxEntry.Name()) {
				continue
			}
			encrypted, checkErr := isEncryptedHistoryBoxDir(filepath.Join(snapshotDir, boxEntry.Name()))
			if checkErr != nil {
				return nil, checkErr

View on GitHub (pinned to 9f775e8a12)