{"record":{"id":"f5f94acee69a5999","repo":"siyuan-note/siyuan","slug":"write-notebook-crypto-backup-failed-w","errorCode":null,"errorMessage":"write notebook crypto backup failed: %w","messagePattern":"write notebook crypto backup failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":407,"sourceCode":"\t\tConf.m.Unlock()\n\t\treturn errors.New(\"cannot save incomplete notebook crypto configuration\")\n\t}\n\tConf.NotebookCrypto.Spec = nc.Spec\n\tConf.NotebookCrypto.BackupID = nc.BackupID\n\tConf.NotebookCrypto.CreatedAt = nc.CreatedAt\n\tConf.NotebookCrypto.Checksum = nc.Checksum\n\tConf.NotebookCrypto.KEKMAC = nc.KEKMAC // 保持 Conf 与备份文件的 KEKMAC 一致\n\tConf.m.Unlock()\n\tbackupPath := dataCryptoBackupPath()\n\tif err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {\n\t\treturn fmt.Errorf(\"mkdir notebook crypto backup dir failed: %w\", err)\n\t}\n\tdata, err := json.Marshal(nc)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"marshal notebook crypto backup failed: %w\", err)\n\t}\n\tif err := atomicWriteFile(backupPath, data); err != nil {\n\t\treturn fmt.Errorf(\"write notebook crypto backup failed: %w\", err)\n\t}\n\treturn nil\n}\n\n// writeNotebookCryptoBackupData 将指定的 NotebookCrypto 写入备份文件（不依赖 Conf.NotebookCrypto）。\n// kek 必须非 nil：在 Checksum 定型后计算 KEKMAC，保证落盘 MAC 与落盘内容一致。\nfunc writeNotebookCryptoBackupData(nc *conf.NotebookCrypto, kek []byte) error {\n\tif kek == nil {\n\t\treturn errors.New(\"cannot generate notebook crypto backup without KEK\")\n\t}\n\tprepareBackupForWrite(nc)\n\tnc.KEKMAC = computeKEKMAC(nc, kek)\n\tif !notebookCryptoConfigurationComplete(nc) {\n\t\treturn errors.New(\"cannot write incomplete notebook crypto backup\")\n\t}\n\tbackupPath := dataCryptoBackupPath()\n\tif err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {\n\t\treturn fmt.Errorf(\"mkdir notebook crypto backup dir failed: %w\", err)","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L389-L425","documentation":"saveNotebookCryptoBackup wraps an atomicWriteFile failure for <DataDir>/.siyuan/data-crypto-backup.json as \"write notebook crypto backup failed: %w\". The configuration was already validated and copied into Conf, but the durable backup file could not be written atomically; Conf now references a BackupID whose file may not exist, so this must be resolved before the backup can be considered reliable.","triggerScenarios":"Any saveNotebookCryptoBackup call (via EnableEncryptedNotebook, deriveKEK, ChangeMasterPassword) where atomicWriteFile fails — permission denied on the target file, disk full, target file locked by another process/antivirus, or I/O error on the volume.","commonSituations":"Full disk right after enabling encryption; the previous backup file is read-only or owned by root; sync/backup software holding the file open on Windows; failing disk producing EIO.","solutions":["Free disk space on the data volume if ENOSPC","Fix ownership/permissions of <DataDir>/.siyuan/data-crypto-backup.json and its directory","Close programs locking the file (antivirus, sync clients) and retry the operation that triggered the save","Inspect the wrapped OS error for the precise cause and address it (EACCES/EROFS/EIO)","After fixing, re-run the enabling/password-change flow so the backup file is regenerated and matches Conf.BackupID"],"exampleFix":"# before\n$ ls -l <DataDir>/.siyuan/data-crypto-backup.json  # read-only, stale\n# after\n$ rm <DataDir>/.siyuan/data-crypto-backup.json  # or chmod u+w\n$ # re-run enable-encryption / change-password to regenerate the backup","handlingStrategy":"retry","validationCode":"p := dataCryptoBackupPath()\nif fi, err := os.Stat(p); err == nil && fi.Mode().Perm()&0200 == 0 {\n    return errors.New(\"backup file not writable: \" + p)\n}\nif err := unix.Access(filepath.Dir(p), unix.W_OK); err != nil {\n    return errors.New(\"backup dir not writable\")\n}","typeGuard":"null","tryCatchPattern":"if err := saveNotebookCryptoBackup(kek); err != nil {\n    if strings.Contains(err.Error(), \"write notebook crypto backup failed\") {\n        // resolve wrapped OS error (ENOSPC/EACCES/EIO), then re-run the triggering flow\n        // so the backup file is regenerated and matches Conf.BackupID\n    }\n}","preventionTips":["Monitor disk space and volume health on the data partition","Keep the backup file writable by the kernel process user","Exclude data-crypto-backup.json from external lockers (sync/AV clients)","After any write failure and fix, re-run enable/password-change to regenerate the backup rather than trusting stale state"],"tags":["filesystem","backup","disk","atomic-write"],"backgroundTag":"file-write-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"}