{"record":{"id":"0715b324e3c537f2","repo":"BigPizzaV3/CodexPlusPlus","slug":"restore-conflict-file-already-exists-path","errorCode":null,"errorMessage":"restore conflict: file already exists: {path}","messagePattern":"restore conflict: file already exists: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codex-plus-data/src/storage.rs","lineNumber":1189,"sourceCode":"            .map(|(key, _)| vec![key])\n            .unwrap_or_default()\n    } else {\n        keys\n    }\n}\n\nfn detect_file_restore_conflicts(tables: &Map<String, Value>) -> anyhow::Result<()> {\n    let Some(files) = tables.get(\"__files\").and_then(Value::as_array) else {\n        return Ok(());\n    };\n    let allowed_paths = allowed_backup_file_paths(tables);\n    for file in files {\n        if let Some(path) = file.get(\"path\").and_then(Value::as_str) {\n            if !allowed_paths.contains(path) {\n                anyhow::bail!(\"unexpected backup file path: {path}\");\n            }\n            if Path::new(path).exists() {\n                anyhow::bail!(\"restore conflict: file already exists: {path}\");\n            }\n            if let Some(content) = file.get(\"content_b64\").and_then(Value::as_str) {\n                base64::Engine::decode(&base64::engine::general_purpose::STANDARD, content)?;\n            }\n        }\n    }\n    Ok(())\n}\n\nfn allowed_backup_file_paths(tables: &Map<String, Value>) -> HashSet<String> {\n    tables\n        .get(\"threads\")\n        .and_then(Value::as_array)\n        .into_iter()\n        .flatten()\n        .filter_map(|row| row.get(\"rollout_path\").and_then(Value::as_str))\n        .filter(|path| !path.trim().is_empty())\n        .map(ToString::to_string)","sourceCodeStart":1171,"sourceCodeEnd":1207,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-data/src/storage.rs#L1171-L1207","documentation":"While pre-checking a backup restore, detect_file_restore_conflicts checks whether each file listed in the __files array already exists on disk. Restore must not clobber existing rollout files, so if Path::new(path).exists() is true the operation bails out with this conflict error before any rows or files are written.","triggerScenarios":"Calling restore_backups when a rollout file recorded in the backup's __files array (a threads.rollout_path) already exists at that path on the target machine — typically a second restore of the same backup, or restoring onto a machine that already ran those sessions.","commonSituations":"Running the same restore twice; restoring onto the original machine after only deleting the DB (not the session files); switching machines where sessions were synced separately; failed partial restore left files behind.","solutions":["Delete or move the existing files listed in the error message, then re-run the restore.","Skip the restore if the sessions already exist locally — the data is likely already present.","Restore into a different codex home directory (pass a different home) where the paths don't exist.","If a previous restore failed midway, clean up the partially restored files first."],"exampleFix":"// before\nrestore_backups(&backup)?; // fails: rollout-2026.jsonl already on disk\n// after\nfor f in &files { let p = Path::new(f); if p.exists() { fs::rename(p, p.with_extension(\"bak\"))?; } }\nrestore_backups(&backup)?;\n","handlingStrategy":"validation","validationCode":"for (const f of backup.__files ?? []) {\n  if (fs.existsSync(f.path)) throw new Error(`restore would clobber existing file: ${f.path}`);\n}","typeGuard":null,"tryCatchPattern":"match restore_backups(&backup) {\n    Err(e) if e.to_string().contains(\"file already exists\") => { /* move existing files aside or skip */ }\n    other => other?,\n}","preventionTips":["Dry-run conflict detection before every restore.","Restore into a clean target home directory.","Archive (rename) rather than delete existing rollout files when re-restoring.","Track which backups were already restored to avoid double-restore."],"tags":["restore","backup","conflict","filesystem"],"backgroundTag":"file-already-exists","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}