use crate::error::Result;
use async_trait::async_trait;
use std::any::Any;
use std::path::Path;
#[async_trait]
pub trait DiskAdaptor: Send + Sync {
async fn open(&mut self, path: &Path) -> Result<()>;
async fn write(&mut self, offset: u64, data: &[u8]) -> Result<()>;
async fn read(&mut self, offset: u64, length: u64) -> Result<Vec<u8>>;
async fn close(&mut self) -> Result<()>;
async fn truncate(&mut self, length: u64) -> Result<()>;
async fn flush(&mut self) -> Result<()>;
async fn size(&self) -> Result<u64>;
fn as_any(&self) -> &dyn Any;
#[cfg(unix)]
fn unix_raw_fd(&self) -> Option<std::os::unix::io::RawFd>;
#[cfg(windows)]
fn windows_raw_handle(&self) -> Option<std::os::windows::io::RawHandle>;
}
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
pub(crate) fn advise_drop_cache(file: &impl std::os::fd::AsRawFd, offset: u64, length: u64) {
let Ok(offset) = libc::off_t::try_from(offset) else {
return;
};
let Ok(length) = libc::off_t::try_from(length) else {
return;
};
let _ =
unsafe { libc::posix_fadvise(file.as_raw_fd(), offset, length, libc::POSIX_FADV_DONTNEED) };
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) fn advise_drop_cache(_file: &impl std::os::fd::AsRawFd, _offset: u64, _length: u64) {}