{"record":{"id":"fb1443cfb3f438e7","repo":"openai/codex","slug":"refusing-to-overwrite-existing-file","errorCode":null,"errorMessage":"refusing to overwrite existing file {}","messagePattern":"refusing to overwrite existing file (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"codex-rs/network-proxy/src/certs.rs","lineNumber":717,"sourceCode":"    let tmp_path = parent.join(format!(\".{file_name}.tmp.{pid}.{nanos}\"));\n\n    let mut file = open_create_new_with_mode(&tmp_path, mode)?;\n    file.write_all(contents)\n        .with_context(|| format!(\"failed to write {}\", tmp_path.display()))?;\n    file.sync_all()\n        .with_context(|| format!(\"failed to fsync {}\", tmp_path.display()))?;\n    drop(file);\n\n    // Create the final file using \"create-new\" semantics (no overwrite). `rename` on Unix can\n    // overwrite existing files, so prefer a hard-link, which fails if the destination exists.\n    match fs::hard_link(&tmp_path, path) {\n        Ok(()) => {\n            fs::remove_file(&tmp_path)\n                .with_context(|| format!(\"failed to remove {}\", tmp_path.display()))?;\n        }\n        Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {\n            let _ = fs::remove_file(&tmp_path);\n            return Err(anyhow!(\n                \"refusing to overwrite existing file {}\",\n                path.display()\n            ));\n        }\n        Err(_) => {\n            // Best-effort fallback for environments where hard links are not supported.\n            // This is still subject to a TOCTOU race, but the typical case is a private per-user\n            // config directory, where other users cannot create files anyway.\n            if path.exists() {\n                let _ = fs::remove_file(&tmp_path);\n                return Err(anyhow!(\n                    \"refusing to overwrite existing file {}\",\n                    path.display()\n                ));\n            }\n            fs::rename(&tmp_path, path).with_context(|| {\n                format!(\n                    \"failed to rename {} -> {}\",","sourceCodeStart":699,"sourceCodeEnd":735,"githubUrl":"https://github.com/openai/codex/blob/339751715c64496cb86246bfb3935f40e309dd3d/codex-rs/network-proxy/src/certs.rs#L699-L735","documentation":"Error \"refusing to overwrite existing file {}\" thrown in openai/codex.","triggerScenarios":"Thrown at codex-rs/network-proxy/src/certs.rs:717 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"339751715c64496cb86246bfb3935f40e309dd3d","analyzedAt":"2026-08-25T05:35:09.876Z","schemaVersion":2},"datasetVersion":"2026-08-25T06:17:31.827Z"}