siyuan-note/siyuan · error

stat encrypted notebook history backup

Error message

stat encrypted notebook history backup [%s] failed: %w

What it means

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.

Solutions

  1. Inspect <boxDir>/.siyuan — it must be a directory; fix or remove it if a file/symlink replaced it
  2. Fix permissions on the box directory and its .siyuan path for the kernel process user
  3. Resolve symlink loops or stale links created by sync/backup tooling
  4. Address the OS-level cause reported in the wrapped error (disk I/O, path length) and rescan

Example fix

// before: assuming stat failure means "not encrypted"
enc, err := isEncryptedHistoryBoxDir(boxDir)
if err != nil { enc = false }
// after: surface the real cause and skip only genuinely unreadable boxes
enc, err := isEncryptedHistoryBoxDir(boxDir)
if err != nil {
    log.Printf("cannot classify history box %s: %v", boxDir, err)
    return // do not treat as unencrypted; keep recovery material
}
Defensive patterns

Strategy: try-catch

Validate before calling

siyuanPath := filepath.Join(boxDir, ".siyuan")
if info, err := os.Stat(siyuanPath); err == nil && !info.IsDir() {
    return fmt.Errorf("%s must be a directory", siyuanPath)
}

Type guard

func boxDirClassifiable(boxDir string) bool {
    f, err := os.Open(filepath.Join(boxDir, ".siyuan"))
    if err != nil { return false }
    defer f.Close()
    return true
}

Try / catch

enc, err := isEncryptedHistoryBoxDir(boxDir)
if err != nil {
    var perr *fs.PathError
    if errors.As(err, &perr) {
        // fix the .siyuan path problem before classifying
    }
    return err // do not default to "not encrypted"
}

Prevention

When it happens

Trigger: 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).

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

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

Appendix: source

Thrown at kernel/model/crypto.go:939

func HasEncryptedNotebookHistory() bool {
	hasHistory, err := scanEncryptedNotebookHistory()
	if err != nil {
		logging.LogErrorf("scan encrypted notebook history failed: %s", err)
		return true
	}
	return hasHistory
}

// isEncryptedHistoryBoxDir 判断历史目录中的 boxID 子目录是否属于加密笔记本。
// 优先看 notebook-crypto-backup.json(删除前随 box 目录整体备份,是加密身份的权威标识),
// 再 fallback 到 conf.json 的 Encrypted 标志。
func isEncryptedHistoryBoxDir(boxDir string) (bool, error) {
	siyuanDir := filepath.Join(boxDir, ".siyuan")
	backupPath := filepath.Join(siyuanDir, "notebook-crypto-backup.json")
	if _, err := os.Stat(backupPath); err == nil {
		return true, nil
	} else if !os.IsNotExist(err) {
		return false, fmt.Errorf("stat encrypted notebook history backup [%s] failed: %w", boxDir, err)
	}
	confPath := filepath.Join(siyuanDir, "conf.json")
	if _, err := os.Stat(confPath); err != nil {
		if os.IsNotExist(err) {
			return false, nil
		}
		return false, fmt.Errorf("stat encrypted notebook history conf [%s] failed: %w", boxDir, err)
	}
	data, err := filelock.ReadFile(confPath)
	if err != nil {
		return false, fmt.Errorf("read encrypted notebook history conf [%s] failed: %w", boxDir, err)
	}
	var boxConf conf.BoxConf
	if err = gulu.JSON.UnmarshalJSON(data, &boxConf); err != nil {
		return false, fmt.Errorf("parse encrypted notebook history conf [%s] failed: %w", boxDir, err)
	}
	return boxConf.Encrypted, nil
}

View on GitHub (pinned to 9f775e8a12)