siyuan-note/siyuan · error

mkdir notebook crypt backup dir failed

Error message

mkdir notebook crypt backup dir failed: %w

What it means

writeNotebookCryptBackup wraps the os.MkdirAll error for <DataDir>/<boxID>/.siyuan/ with this message. Creating the backup directory failed at the OS level — the underlying cause (permission, read-only filesystem, ENOSPC, path is a file) is in the wrapped error.

Solutions

  1. Check the wrapped %w error for the exact errno and fix it: chown/chmod the workspace data dir so the kernel user can write
  2. Verify <DataDir>/<boxID>/.siyuan is a directory and not a regular file; remove/rename the offending file
  3. Free disk space if ENOSPC, and remount the volume read-write
  4. After fixing, retry ChangeMasterPassword/encrypt so the backup is written before relying on the notebook

Example fix

# inspect
ls -la <DataDir>/<boxID>/.siyuan
# fix ownership (Docker case)
docker exec siyuan chown -R siyuan:siyuan /siyuan/workspace
# or remount rw
mount -o remount,rw /path/to/workspace
Defensive patterns

Strategy: try-catch

Validate before calling

siyuanDir := filepath.Join(util.DataDir, boxID, ".siyuan")
if info, err := os.Stat(filepath.Dir(siyuanDir)); err != nil || !info.IsDir() {
    return fmt.Errorf("notebook dir not writable: %v", err)
}

Try / catch

if err := model.WriteNotebookCryptBackup(boxID, crypt); err != nil {
    var pe *fs.PathError
    if errors.As(err, &pe) && strings.Contains(err.Error(), "mkdir") {
        // check perms/disk space/readonly mount, then retry once
        fixPermissions(pe.Path)
        return model.WriteNotebookCryptBackup(boxID, crypt)
    }
    return err
}

Prevention

When it happens

Trigger: CreateEncryptedBox or ChangeMasterPassword on a notebook whose .siyuan directory cannot be created: read-only data directory, disk full, a file named .siyuan exists, or insufficient permissions (e.g. running as another user, Docker volume owned by root).

Common situations: Docker/Kubernetes volumes with wrong ownership; SiYuan installed under a system-protected path; antivirus locking the directory on Windows; read-only mount after a crash; workspace moved to a new disk without permissions.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/crypto.go:2529

// 全局备份存 MasterSalt/KEKVerifier,per-notebook 备份存 WrappedDEK/WrapNonce。
const notebookCryptoBackupFilename = "notebook-crypto-backup.json"

func notebookCryptoBackupPath(boxID string) string {
	return filepath.Join(util.DataDir, boxID, ".siyuan", notebookCryptoBackupFilename)
}

// writeNotebookCryptBackup 写入加密笔记本的 BoxCrypt 备份。
// 仅在 Encrypted=true 的笔记本上调用,配合 CreateEncryptedBox / ChangeMasterPassword 写入。
func writeNotebookCryptBackup(boxID string, crypt *conf.BoxEncryption) error {
	if !ast.IsNodeIDPattern(boxID) {
		return errors.New("invalid notebook ID")
	}
	if err := validateBoxEncryption(crypt); err != nil {
		return err
	}
	backupPath := notebookCryptoBackupPath(boxID)
	if err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {
		return fmt.Errorf("mkdir notebook crypt backup dir failed: %w", err)
	}
	data, err := gulu.JSON.MarshalIndentJSON(crypt, "", "  ")
	if err != nil {
		return fmt.Errorf("marshal notebook crypt backup failed: %w", err)
	}
	if err := filelock.WriteFile(backupPath, data); err != nil {
		return fmt.Errorf("write notebook crypt backup failed: %w", err)
	}
	return nil
}

// readNotebookCryptBackup 读取加密笔记本的 BoxCrypt 备份。
// 备份文件不存在时返回 (nil, nil),调用方据此区分"非加密笔记本"和"备份不存在"。
func readNotebookCryptBackup(boxID string) (*conf.BoxEncryption, error) {
	if !ast.IsNodeIDPattern(boxID) {
		return nil, errors.New("invalid notebook ID")
	}
	backupPath := notebookCryptoBackupPath(boxID)

View on GitHub (pinned to 9f775e8a12)