Skip to content

Commit

Permalink
fix: direct io syscall not available on mac (#16574)
Browse files Browse the repository at this point in the history
dio fallback on mac

Signed-off-by: coldWater <[email protected]>
  • Loading branch information
forsaken628 authored Oct 9, 2024
1 parent 0c8d546 commit add514c
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions src/common/base/src/base/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -109,31 +108,69 @@ struct DmaFile {
buf: Option<DmaBuffer>,
}

impl DmaFile {
/// Attempts to open a file in read-only mode.
async fn open(path: impl AsRef<Path>) -> io::Result<DmaFile> {
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<Path>) -> io::Result<DmaFile> {
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<Path>) -> io::Result<DmaFile> {
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<Path>) -> io::Result<DmaFile> {
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<Path>) -> io::Result<DmaFile> {
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<Path>) -> io::Result<DmaFile> {
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)
}
Expand Down

0 comments on commit add514c

Please sign in to comment.