use crate::common::ISyncIO;
use std::os::unix::prelude::FileExt;
use std::path::Path;
use tokio::fs::OpenOptions;
pub struct FileIO {
fd: std::fs::File,
}
impl FileIO {
pub async fn open(path: &Path, flags: i32) -> Self {
let async_fd = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(flags)
.open(path)
.await
.unwrap();
let fd = async_fd.into_std().await;
FileIO { fd }
}
}
impl ISyncIO for FileIO {
fn read_at_sync(&self, offset: u64, len: u64) -> Vec<u8> {
let mut buf = vec![0u8; len.try_into().unwrap()];
self.fd.read_exact_at(&mut buf, offset).unwrap();
buf
}
fn write_at_sync(&self, offset: u64, data: &[u8]) -> () {
self.fd.write_all_at(data, offset).unwrap();
}
fn sync_data_sync(&self) -> () {
self.fd.sync_data().unwrap();
}
}