From add514cffba5bd4a1a7d9bbb287076723df1b595 Mon Sep 17 00:00:00 2001 From: coldWater Date: Wed, 9 Oct 2024 15:15:18 +0800 Subject: [PATCH] fix: direct io syscall not available on mac (#16574) dio fallback on mac Signed-off-by: coldWater --- src/common/base/src/base/dma.rs | 77 ++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/src/common/base/src/base/dma.rs b/src/common/base/src/base/dma.rs index 063b9d5467fd..192a6ecc5e46 100644 --- a/src/common/base/src/base/dma.rs +++ b/src/common/base/src/base/dma.rs @@ -26,7 +26,6 @@ use std::path::Path; use std::ptr::Alignment; use std::ptr::NonNull; -use rustix::fs::OFlags; use tokio::fs::File; use tokio::io::AsyncSeekExt; @@ -109,31 +108,69 @@ struct DmaFile { buf: Option, } -impl DmaFile { - /// Attempts to open a file in read-only mode. - async fn open(path: impl AsRef) -> io::Result { - let file = File::options() - .read(true) - .custom_flags(OFlags::DIRECT.bits() as i32) - .open(path) - .await?; +#[cfg(target_os = "linux")] +pub mod linux { + use rustix::fs::OFlags; + + use super::*; + + impl DmaFile { + /// Attempts to open a file in read-only mode. + pub(super) async fn open(path: impl AsRef) -> io::Result { + let file = File::options() + .read(true) + .custom_flags(OFlags::DIRECT.bits() as i32) + .open(path) + .await?; - open_dma(file).await + open_dma(file).await + } + + /// Opens a file in write-only mode. + pub(super) async fn create(path: impl AsRef) -> io::Result { + let file = File::options() + .write(true) + .create(true) + .truncate(true) + .custom_flags((OFlags::DIRECT | OFlags::EXCL).bits() as i32) + .open(path) + .await?; + + open_dma(file).await + } } +} - /// Opens a file in write-only mode. - async fn create(path: impl AsRef) -> io::Result { - let file = File::options() - .write(true) - .create(true) - .truncate(true) - .custom_flags((OFlags::DIRECT | OFlags::EXCL).bits() as i32) - .open(path) - .await?; +#[cfg(not(target_os = "linux"))] +pub mod not_linux { + use rustix::fs::OFlags; + + use super::*; - open_dma(file).await + impl DmaFile { + /// Attempts to open a file in read-only mode. + pub(super) async fn open(path: impl AsRef) -> io::Result { + let file = File::options().read(true).open(path).await?; + + open_dma(file).await + } + + /// Opens a file in write-only mode. + pub(super) async fn create(path: impl AsRef) -> io::Result { + let file = File::options() + .write(true) + .create(true) + .truncate(true) + .custom_flags(OFlags::EXCL.bits() as i32) + .open(path) + .await?; + + open_dma(file).await + } } +} +impl DmaFile { fn set_buffer(&mut self, buf: DmaBuffer) { self.buf = Some(buf) }