Minimal cross-platform direct I/O abstraction for Rust.
- Linux:
O_DIRECT - macOS:
F_NOCACHE - Windows:
FILE_FLAG_NO_BUFFERING
[dependencies]
odirect = "0.5.0"use odirect::{open_direct_file, AccessMode};
fn main() {
let file = open_direct_file(
"data.bin",
AccessMode::ReadWrite,
);
match file {
Ok(_) => println!("opened successfully"),
Err(e) => println!("error: {}", e),
}
}AccessMode::Read
AccessMode::Write
AccessMode::ReadWriteDirect/unbuffered I/O behavior differs across operating systems:
- Linux: Uses
O_DIRECTto bypass the page cache. - macOS: Uses
F_NOCACHEto disable file caching. - Windows: Uses
FILE_FLAG_NO_BUFFERINGfor unbuffered I/O.
All platforms may require aligned buffers and aligned reads/writes (typically 512 or 4096 bytes, depending on the underlying device) when performing direct I/O operations. Durability guarantees (fsync, F_FULLFSYNC, FlushFileBuffers) must be handled separately by the caller after writes.