{"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":"io::Error","httpStatus":null,"severity":"error","filePath":"src/uu/cp/src/platform/windows.rs","lineNumber":98,"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":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/uutils/coreutils/blob/325183372aaf86d8ae6feaf4f9548f74fe815102/src/uu/cp/src/platform/windows.rs#L80-L116","documentation":"write_all_at() loops over seek_write calls at a given offset; if a call returns Ok(0) (zero bytes written) the loop would spin forever, so it raises WriteZero 'failed to write whole buffer'. This mirrors std's write_all semantics. It means the underlying Windows file could not accept any bytes at that offset.","triggerScenarios":"file.seek_write(buf, offset) returns Ok(0) while writing sparse file data in sparse_copy/sparse_copy_without_hole_fd, e.g., after the file handle becomes invalid, the volume is full, or an offset beyond a hard limit is used.","commonSituations":"Copying very large/sparse files on Windows with insufficient disk space; copying to a file on a network share or removable volume that fails mid-write; antivirus or backup software locking the destination file.","solutions":["Free disk space on the destination volume and retry the copy","Retry the cp operation without --sparse (fall back to normal copy) to rule out sparse-write issues","Check the destination is not locked by another process (antivirus, indexer, backup) and that the volume is writable","If persistent, report with the Windows error/last-os-error from the io::Error chain"],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"// before copying on Windows\nlet free: u64 = get_free_bytes(dest_volume);\nif free < expected_size { return Err(\"insufficient disk space\"); }","typeGuard":"null","tryCatchPattern":"match result {\n    Err(e) if e.kind() == std::io::ErrorKind::WriteZero => {\n        eprintln!(\"write failed at offset: {e}; check disk space/locks\");\n        // retry without sparse mode or free space first\n    }\n    other => other?,\n}","preventionTips":["Monitor free space on the destination volume before large copies","Avoid copying sparse files onto network shares/removable media","Exclude destination directories from AV real-time scanning during bulk copies"],"tags":["io","windows","sparse-file","write-zero"],"backgroundTag":"write-zero","analyzedSha":"325183372aaf86d8ae6feaf4f9548f74fe815102","analyzedAt":"2026-08-31T11:11:36.175Z","contentChangedAt":"2026-08-31T11:11:36.175Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}