Fallen
Fallen

Reputation: 335

Does Rust's standard library support direct IO file access?

Is there a way to specify O_DIRECT with Rust's standard library, or do you need to use libc?

Upvotes: 5

Views: 3038

Answers (1)

Shepmaster
Shepmaster

Reputation: 430791

You can use the Unix specific extension trait os::unix::fs::OpenOptionsExt:

use std::{fs::OpenOptions, os::unix::fs::OpenOptionsExt};

const O_DIRECT: i32 = 0o0040000; // Double check value

fn main() {
    OpenOptions::new()
        .read(true)
        .custom_flags(O_DIRECT)
        .open("/etc/passwd")
        .expect("Can't open");
}

The value of O_DIRECT is platform-specific, however. I'd probably end up using libc to provide the value.

Upvotes: 6

Related Questions