LCOV - code coverage report
Current view: top level - pageserver/src/virtual_file - open_options.rs (source / functions) Coverage Total Hit
Test: 98683a8629f0f7f0031d02e04512998d589d76ea.info Lines: 73.3 % 90 66
Test Date: 2025-04-11 16:58:57 Functions: 76.9 % 13 10

            Line data    Source code
       1              : //! Enum-dispatch to the `OpenOptions` type of the respective [`super::IoEngineKind`];
       2              : 
       3              : use std::os::fd::OwnedFd;
       4              : use std::path::Path;
       5              : 
       6              : use super::io_engine::IoEngine;
       7              : 
       8              : #[derive(Debug, Clone)]
       9              : pub enum OpenOptions {
      10              :     StdFs(std::fs::OpenOptions),
      11              :     #[cfg(target_os = "linux")]
      12              :     TokioEpollUring(tokio_epoll_uring::ops::open_at::OpenOptions),
      13              : }
      14              : 
      15              : impl Default for OpenOptions {
      16        12718 :     fn default() -> Self {
      17        12718 :         match super::io_engine::get() {
      18            0 :             IoEngine::NotSet => panic!("io engine not set"),
      19         6352 :             IoEngine::StdFs => Self::StdFs(std::fs::OpenOptions::new()),
      20              :             #[cfg(target_os = "linux")]
      21              :             IoEngine::TokioEpollUring => {
      22         6366 :                 Self::TokioEpollUring(tokio_epoll_uring::ops::open_at::OpenOptions::new())
      23              :             }
      24              :         }
      25        12718 :     }
      26              : }
      27              : 
      28              : impl OpenOptions {
      29        12718 :     pub fn new() -> OpenOptions {
      30        12718 :         Self::default()
      31        12718 :     }
      32              : 
      33         8448 :     pub fn read(&mut self, read: bool) -> &mut OpenOptions {
      34         8448 :         match self {
      35         4224 :             OpenOptions::StdFs(x) => {
      36         4224 :                 let _ = x.read(read);
      37         4224 :             }
      38              :             #[cfg(target_os = "linux")]
      39         4224 :             OpenOptions::TokioEpollUring(x) => {
      40         4224 :                 let _ = x.read(read);
      41         4224 :             }
      42              :         }
      43         8448 :         self
      44         8448 :     }
      45              : 
      46         6918 :     pub fn write(&mut self, write: bool) -> &mut OpenOptions {
      47         6918 :         match self {
      48         3452 :             OpenOptions::StdFs(x) => {
      49         3452 :                 let _ = x.write(write);
      50         3452 :             }
      51              :             #[cfg(target_os = "linux")]
      52         3466 :             OpenOptions::TokioEpollUring(x) => {
      53         3466 :                 let _ = x.write(write);
      54         3466 :             }
      55              :         }
      56         6918 :         self
      57         6918 :     }
      58              : 
      59        17996 :     pub fn create(&mut self, create: bool) -> &mut OpenOptions {
      60        17996 :         match self {
      61         8984 :             OpenOptions::StdFs(x) => {
      62         8984 :                 let _ = x.create(create);
      63         8984 :             }
      64              :             #[cfg(target_os = "linux")]
      65         9012 :             OpenOptions::TokioEpollUring(x) => {
      66         9012 :                 let _ = x.create(create);
      67         9012 :             }
      68              :         }
      69        17996 :         self
      70        17996 :     }
      71              : 
      72        13534 :     pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
      73        13534 :         match self {
      74         6760 :             OpenOptions::StdFs(x) => {
      75         6760 :                 let _ = x.create_new(create_new);
      76         6760 :             }
      77              :             #[cfg(target_os = "linux")]
      78         6774 :             OpenOptions::TokioEpollUring(x) => {
      79         6774 :                 let _ = x.create_new(create_new);
      80         6774 :             }
      81              :         }
      82        13534 :         self
      83        13534 :     }
      84              : 
      85        15356 :     pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
      86        15356 :         match self {
      87         7664 :             OpenOptions::StdFs(x) => {
      88         7664 :                 let _ = x.truncate(truncate);
      89         7664 :             }
      90              :             #[cfg(target_os = "linux")]
      91         7692 :             OpenOptions::TokioEpollUring(x) => {
      92         7692 :                 let _ = x.truncate(truncate);
      93         7692 :             }
      94              :         }
      95        15356 :         self
      96        15356 :     }
      97              : 
      98       387082 :     pub(in crate::virtual_file) async fn open(&self, path: &Path) -> std::io::Result<OwnedFd> {
      99       387082 :         match self {
     100       193866 :             OpenOptions::StdFs(x) => x.open(path).map(|file| file.into()),
     101              :             #[cfg(target_os = "linux")]
     102       193216 :             OpenOptions::TokioEpollUring(x) => {
     103       193216 :                 let system = super::io_engine::tokio_epoll_uring_ext::thread_local_system().await;
     104       193216 :                 system.open(path, x).await.map_err(|e| match e {
     105            0 :                     tokio_epoll_uring::Error::Op(e) => e,
     106            0 :                     tokio_epoll_uring::Error::System(system) => {
     107            0 :                         std::io::Error::new(std::io::ErrorKind::Other, system)
     108              :                     }
     109       193216 :                 })
     110              :             }
     111              :         }
     112       387082 :     }
     113              : }
     114              : 
     115              : impl std::os::unix::prelude::OpenOptionsExt for OpenOptions {
     116            0 :     fn mode(&mut self, mode: u32) -> &mut OpenOptions {
     117            0 :         match self {
     118            0 :             OpenOptions::StdFs(x) => {
     119            0 :                 let _ = x.mode(mode);
     120            0 :             }
     121              :             #[cfg(target_os = "linux")]
     122            0 :             OpenOptions::TokioEpollUring(x) => {
     123            0 :                 let _ = x.mode(mode);
     124            0 :             }
     125              :         }
     126            0 :         self
     127            0 :     }
     128              : 
     129            0 :     fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
     130            0 :         match self {
     131            0 :             OpenOptions::StdFs(x) => {
     132            0 :                 let _ = x.custom_flags(flags);
     133            0 :             }
     134              :             #[cfg(target_os = "linux")]
     135            0 :             OpenOptions::TokioEpollUring(x) => {
     136            0 :                 let _ = x.custom_flags(flags);
     137            0 :             }
     138              :         }
     139            0 :         self
     140            0 :     }
     141              : }
        

Generated by: LCOV version 2.1-beta