siyuan-note/siyuan · error

read history snapshot

Error message

read history snapshot [%s] failed: %w

What it means

While enumerating history snapshots, each <ts>-<op> snapshot directory under the history dir is read to find per-box (boxID) subdirectories. This error is thrown when one snapshot directory cannot be listed; unlike a missing top-level history dir, a single bad snapshot aborts the whole scan because it may hold encrypted recovery material whose presence must not be misjudged. The snapshot name and OS error are wrapped.

Solutions

  1. Check permissions/ownership of the named snapshot directory (<ts>-<op>) and fix read access
  2. Identify and release whatever locks the directory (antivirus, backup, sync client) and retry
  3. If the snapshot is provably corrupt and not needed for recovery, remove just that snapshot dir so the scan can proceed
  4. Run filesystem checks (fsck/chkdsk) if the wrapped cause indicates I/O errors

Example fix

// before: scanning an unhealthy history tree directly
boxes, err := encryptedNotebookHistoryBoxDirs()
// after: probe each snapshot dir first and repair/flag the bad one
for _, name := range snapshotNames {
    if _, err := os.ReadDir(filepath.Join(util.HistoryDir, name)); err != nil {
        log.Printf("snapshot %s unreadable, excluding: %v", name, err)
    }
}
boxes, err = encryptedNotebookHistoryBoxDirs()
Defensive patterns

Strategy: try-catch

Validate before calling

entries, _ := os.ReadDir(util.HistoryDir)
for _, e := range entries {
    if _, err := os.ReadDir(filepath.Join(util.HistoryDir, e.Name())); err != nil {
        // snapshot unreadable — repair or exclude before scanning
    }
}

Type guard

func snapshotReadable(snapshotDir string) bool {
    f, err := os.Open(snapshotDir)
    if err != nil { return false }
    defer f.Close()
    return true
}

Try / catch

boxes, err := encryptedNotebookHistoryBoxDirs()
if err != nil {
    if strings.Contains(err.Error(), "read history snapshot") {
        // quarantine the named snapshot and rescan
    }
    return err
}

Prevention

When it happens

Trigger: os.ReadDir on a specific history/<ts>-<op> snapshot directory fails with a non-ENOENT-style fatal error during scanEncryptedNotebookHistory or verifyKEKAgainstEncryptedHistory — e.g. permission denied, I/O error, or the entry was replaced by something unreadable mid-scan.

Common situations: Partial deletion/cleanup of history left a corrupt snapshot entry; snapshot dir owned by another user after migration; Windows file lock from backup/indexing software; failing disk sector under the history tree.

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/9b7d0dae7e4d9ad2. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:803

}

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
			}
			if encrypted {
				ret = append(ret, filepath.Join(snapshotDir, boxEntry.Name()))
			}
		}
	}
	return ret, nil
}

func hasEncryptedNotebookDeleteHistory(boxID string) (bool, error) {

View on GitHub (pinned to 9f775e8a12)