rust-lang/rust · error · io::Error

must specify at least one of read, write, or append access

Error message

must specify at least one of read, write, or append access

What it means

In get_access_mode(), when read, write, and append are all false AND no creation flag (create/create_new/truncate) is set either, there is no valid access mode to request from the OS, so the open is rejected as InvalidInput (unix.rs:1170-1173).

Source

Thrown at library/std/src/sys/fs/unix.rs:1170

    }

    fn get_access_mode(&self) -> io::Result<c_int> {
        match (self.read, self.write, self.append) {
            (true, false, false) => Ok(libc::O_RDONLY),
            (false, true, false) => Ok(libc::O_WRONLY),
            (true, true, false) => Ok(libc::O_RDWR),
            (false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND),
            (true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND),
            (false, false, false) => {
                // If no access mode is set, check if any creation flags are set
                // to provide a more descriptive error message
                if self.create || self.create_new || self.truncate {
                    Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        "creating or truncating a file requires write or append access",
                    ))
                } else {
                    Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        "must specify at least one of read, write, or append access",
                    ))
                }
            }
        }
    }

    fn get_creation_mode(&self) -> io::Result<c_int> {
        match (self.write, self.append) {
            (true, false) => {}
            (false, false) => {
                if self.truncate || self.create || self.create_new {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        "creating or truncating a file requires write or append access",
                    ));
                }

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Add at least one of .read(true), .write(true), or .append(true).
  2. For plain reading, use File::open(p) (which sets read) instead of a bare OpenOptions.

Example fix

// before
std::fs::OpenOptions::new().open("f")?;
// after
std::fs::OpenOptions::new().read(true).open("f")?
Defensive patterns

Strategy: validation

Try / catch

match std::fs::OpenOptions::new().open(p) {
    Ok(f) => f,
    Err(e) if e.kind() == std::io::ErrorKind::InvalidInput => {
        return Err(e); // fix builder: set read/write/append
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: `OpenOptions::new().open(p)` with no access or creation setters at all.

Common situations: Building an OpenOptions and forgetting any mode; expecting OpenOptions to default to read like C fopen.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/a5494d3fc328d1e1. Report an issue: GitHub.