{"record":{"id":"6655b9a27756b087","repo":"rust-lang/rust","slug":"creating-or-truncating-a-file-requires-write-or-ap","errorCode":null,"errorMessage":"creating or truncating a file requires write or append access","messagePattern":"creating or truncating a file requires write or append access","errorType":"validation","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"library/std/src/sys/fs/unix.rs","lineNumber":1165,"sourceCode":"        self.custom_flags = flags;\n    }\n    #[cfg(not(target_os = \"wasi\"))]\n    pub fn mode(&mut self, mode: u32) {\n        self.mode = mode as mode_t;\n    }\n\n    fn get_access_mode(&self) -> io::Result<c_int> {\n        match (self.read, self.write, self.append) {\n            (true, false, false) => Ok(libc::O_RDONLY),\n            (false, true, false) => Ok(libc::O_WRONLY),\n            (true, true, false) => Ok(libc::O_RDWR),\n            (false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND),\n            (true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND),\n            (false, false, false) => {\n                // If no access mode is set, check if any creation flags are set\n                // to provide a more descriptive error message\n                if self.create || self.create_new || self.truncate {\n                    Err(io::Error::new(\n                        io::ErrorKind::InvalidInput,\n                        \"creating or truncating a file requires write or append access\",\n                    ))\n                } else {\n                    Err(io::Error::new(\n                        io::ErrorKind::InvalidInput,\n                        \"must specify at least one of read, write, or append access\",\n                    ))\n                }\n            }\n        }\n    }\n\n    fn get_creation_mode(&self) -> io::Result<c_int> {\n        match (self.write, self.append) {\n            (true, false) => {}\n            (false, false) => {\n                if self.truncate || self.create || self.create_new {","sourceCodeStart":1147,"sourceCodeEnd":1183,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/library/std/src/sys/fs/unix.rs#L1147-L1183","documentation":"When computing Unix open flags in get_access_mode(), the case (read, write, append) = (false, false, false) is invalid. If a creation/truncation flag (create, create_new, truncate) IS set, this more specific error is returned because creating or truncating requires write or append access (unix.rs:1161-1169).","triggerScenarios":"`OpenOptions::new().create(true).open(p)` or `.create_new(true).open(p)` or `.truncate(true).open(p)` with no .read/.write/.append set.","commonSituations":"Assuming .create(true) implies write access; copying an OpenOptions chain and dropping the access setter; building a 'touch'-style open incorrectly.","solutions":["Add .write(true) (or .append(true)) to the OpenOptions builder.","For exclusive create-if-absent, use .write(true).create_new(true).","Drop the creation flag if you only intend to read."],"exampleFix":"// before\nstd::fs::OpenOptions::new().create(true).open(\"f\")?;\n// after\nstd::fs::OpenOptions::new().write(true).create(true).open(\"f\")?","handlingStrategy":"validation","validationCode":"fn can_open(opts: &std::fs::OpenOptions) -> bool {\n    // create/create_new/truncate require write or append\n    let needs_write = true; // mirror std check via the public API is not possible;\n    // instead, validate your builder explicitly before open:\n    // ensure write or append is set whenever create/truncate is used.\n    needs_write && true\n}","typeGuard":null,"tryCatchPattern":"match std::fs::OpenOptions::new().create(true).open(p) {\n    Ok(f) => f,\n    Err(e) if e.kind() == std::io::ErrorKind::InvalidInput => {\n        return Err(e); // fix the builder: add .write(true)\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Whenever you set .create/.create_new/.truncate, also set .write(true) or .append(true).","Wrap OpenOptions construction in a helper that enforces the access+creation invariant."],"tags":["rust","std","fs","unix","openoptions","platform"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}