{"record":{"id":"9899e14ecbc180bd","repo":"uutils/coreutils","slug":"failed-to-write-whole-buffer","errorCode":null,"errorMessage":"failed to write whole buffer","messagePattern":"failed to write whole buffer","errorType":"error_code","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"src/uu/cp/src/platform/windows.rs","lineNumber":100,"sourceCode":"            Ok(n) => total += n,\n            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}\n            Err(e) => return Err(e),\n        }\n    }\n    Ok(total)\n}\n\n/// Write the whole `buf` to `file` at `offset`, looping over partial writes.\n///\n/// The Windows-only `seek_write` has no `write_all`-style counterpart (unlike\n/// the Unix `write_all_at`), so dropped tails on partial writes are handled\n/// here. The `Interrupted` arm mirrors std's own write loops, although Windows\n/// file I/O does not produce it.\nfn write_all_at(file: &File, mut buf: &[u8], mut offset: u64) -> std::io::Result<()> {\n    while !buf.is_empty() {\n        match file.seek_write(buf, offset) {\n            Ok(0) => {\n                return Err(std::io::Error::new(\n                    std::io::ErrorKind::WriteZero,\n                    \"failed to write whole buffer\",\n                ));\n            }\n            Ok(n) => {\n                buf = &buf[n..];\n                offset += n as u64;\n            }\n            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}\n            Err(e) => return Err(e),\n        }\n    }\n    Ok(())\n}\n\n/// Error from [`sparse_copy`], separating \"the destination filesystem cannot\n/// do sparse files\" — which callers handle by falling back to a plain copy —\n/// from real I/O failures.","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/uutils/coreutils/blob/92995feca29e324a7c559ede91db81c32d9f5d59/src/uu/cp/src/platform/windows.rs#L82-L118","documentation":"`write_all_at` loops over `seek_write` until the whole buffer is written; if the Windows file write returns Ok(0) (zero bytes written), the function cannot make progress and throws `WriteZero` with this message. It mirrors std's `write_all` behavior for zero-length writes.","triggerScenarios":"A call to `FileExt::seek_write` returns 0 bytes written while copying data in `sparse_copy` (cp on Windows) or in the `write_all_at_writes_at_offset` test.","commonSituations":"Writing to a file that has been closed or opened without write access, writing past constraints on special/locked files, or a filesystem/driver reporting a zero-byte write on very large offsets during sparse copy.","solutions":["Check the destination file is opened with write (not just read) access and is a regular file","Retry the copy; transient zero-byte writes can occur with some drivers/filesystems","Verify the destination has free space and is not size-locked/quota-exhausted","If it persists, file a bug with the exact offset and buffer size"],"exampleFix":"// before\nlet n = file.seek_write(buf, offset)?; // may return 0, loop forever\n// after\nlet n = file.seek_write(buf, offset)?;\nif n == 0 { return Err(io::Error::new(io::ErrorKind::WriteZero, \"failed to write whole buffer\")); }","handlingStrategy":"try-catch","validationCode":"let md = std::fs::metadata(dest)?;\nif !md.is_file() { return Err(\"destination is not a regular file\"); }","typeGuard":"fn is_writable_regular_file(p: &Path) -> bool {\n    std::fs::File::options().append(true).open(p).is_ok()\n}","tryCatchPattern":"match write_all_at(&file, buf, offset) {\n    Err(e) if e.kind() == std::io::ErrorKind::WriteZero => {\n        eprintln!(\"short write at offset {offset}: {e}\");\n        // reopen destination with write access and retry\n    }\n    r => r?,\n}","preventionTips":["Always open destinations with write access before copying","Verify free space and quotas before large copies","Validate the destination is a regular file, not a device or locked handle","Treat Ok(0) writes as fatal, never as a retryable no-op without a retry cap"],"tags":["windows","io","file-write"],"backgroundTag":"file-write-failed","analyzedSha":"92995feca29e324a7c559ede91db81c32d9f5d59","analyzedAt":"2026-09-20T12:39:35.476Z","contentChangedAt":"2026-09-20T12:39:35.476Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}