Selaa lähdekoodia

Add `File::phys`

徐启航 1 vuosi sitten
vanhempi
commit
a72d5775e4

+ 5 - 0
src/lib/h2o_async/src/io.rs

@@ -39,6 +39,11 @@ impl Stream {
         }
         }
     }
     }
 
 
+    #[inline]
+    pub fn into_phys(this: Self) -> solvent::mem::Phys {
+        Self::into_raw(this).phys
+    }
+
     /// # Safety
     /// # Safety
     ///
     ///
     /// The stream must holds the unique reference to the `Phys`, or the others
     /// The stream must holds the unique reference to the `Phys`, or the others

+ 4 - 1
src/lib/h2o_fs/src/file.rs

@@ -5,9 +5,10 @@ mod stream;
 use alloc::boxed::Box;
 use alloc::boxed::Box;
 
 
 use async_trait::async_trait;
 use async_trait::async_trait;
+use solvent::prelude::Phys;
 use solvent_async::io::Stream;
 use solvent_async::io::Stream;
 use solvent_core::io::RawStream;
 use solvent_core::io::RawStream;
-use solvent_rpc::io::Error;
+use solvent_rpc::io::{Error, file::PhysOptions};
 
 
 #[cfg(feature = "runtime")]
 #[cfg(feature = "runtime")]
 pub use self::handle::{handle, handle_mapped};
 pub use self::handle::{handle, handle_mapped};
@@ -41,4 +42,6 @@ pub trait File: Entry {
     }
     }
 
 
     async fn resize(&self, new_len: usize) -> Result<(), Error>;
     async fn resize(&self, new_len: usize) -> Result<(), Error>;
+
+    async fn phys(&self, options: PhysOptions) -> Result<Phys, Error>;
 }
 }

+ 1 - 0
src/lib/h2o_fs/src/file/handle.rs

@@ -138,6 +138,7 @@ async fn handle_impl<S: StreamIo>(
                 log::warn!("file RPC received unknown request");
                 log::warn!("file RPC received unknown request");
                 break;
                 break;
             }
             }
+            FileRequest::Phys { options, responder } => responder.send(file.phys(options).await),
         };
         };
 
 
         if let Err(err) = res {
         if let Err(err) = res {

+ 14 - 2
src/lib/h2o_fs/src/file/stream.rs

@@ -2,10 +2,10 @@ use alloc::boxed::Box;
 use core::ops::Deref;
 use core::ops::Deref;
 
 
 use async_trait::async_trait;
 use async_trait::async_trait;
-use solvent::prelude::Channel;
+use solvent::prelude::{Channel, Phys};
 use solvent_async::io::Stream;
 use solvent_async::io::Stream;
 use solvent_core::{io::SeekFrom, sync::Arsc};
 use solvent_core::{io::SeekFrom, sync::Arsc};
-use solvent_rpc::io::Error;
+use solvent_rpc::io::{file::PhysOptions, Error};
 
 
 use super::File;
 use super::File;
 
 
@@ -55,6 +55,8 @@ pub trait StreamIo {
     async fn resize(&mut self, new_len: usize) -> Result<(), Error>;
     async fn resize(&mut self, new_len: usize) -> Result<(), Error>;
 
 
     async fn seek(&mut self, pos: SeekFrom) -> Result<usize, Error>;
     async fn seek(&mut self, pos: SeekFrom) -> Result<usize, Error>;
+
+    async fn phys(&self, options: PhysOptions) -> Result<Phys, Error>;
 }
 }
 
 
 #[cfg(feature = "runtime")]
 #[cfg(feature = "runtime")]
@@ -145,6 +147,11 @@ mod runtime {
             self.seeker = new;
             self.seeker = new;
             Ok(new)
             Ok(new)
         }
         }
+
+        #[inline]
+        async fn phys(&self, options: PhysOptions) -> Result<Phys, Error> {
+            self.inner.phys(options).await
+        }
     }
     }
 
 
     impl<F: File> Drop for DirectFile<F> {
     impl<F: File> Drop for DirectFile<F> {
@@ -229,6 +236,11 @@ mod runtime {
         async fn seek(&mut self, pos: SeekFrom) -> Result<usize, Error> {
         async fn seek(&mut self, pos: SeekFrom) -> Result<usize, Error> {
             Ok(self.stream()?.seek(pos).await?)
             Ok(self.stream()?.seek(pos).await?)
         }
         }
+        
+        #[inline]
+        async fn phys(&self, options: PhysOptions) -> Result<Phys, Error> {
+            self.inner.phys(options).await
+        }
     }
     }
 
 
     impl<F: File> Drop for StreamFile<F> {
     impl<F: File> Drop for StreamFile<F> {

+ 9 - 1
src/lib/h2o_fs/src/mem/file.rs

@@ -5,7 +5,7 @@ use async_trait::async_trait;
 use solvent::prelude::{Channel, Phys};
 use solvent::prelude::{Channel, Phys};
 use solvent_async::io::Stream;
 use solvent_async::io::Stream;
 use solvent_core::{io::RawStream, path::Path, sync::Arsc};
 use solvent_core::{io::RawStream, path::Path, sync::Arsc};
-use solvent_rpc::io::{file::FileServer, Error, FileType, Metadata, OpenOptions, Permission};
+use solvent_rpc::io::{file::{FileServer, PhysOptions}, Error, FileType, Metadata, OpenOptions, Permission};
 
 
 use crate::{
 use crate::{
     dir::EventTokens,
     dir::EventTokens,
@@ -107,4 +107,12 @@ impl File for MemFile {
     async fn resize(&self, _: usize) -> Result<(), Error> {
     async fn resize(&self, _: usize) -> Result<(), Error> {
         unimplemented!("Default implementation in `StreamFile`")
         unimplemented!("Default implementation in `StreamFile`")
     }
     }
+
+    async fn phys(&self, options: PhysOptions) -> Result<Phys, Error> {
+        if self.locked.load(Acquire) {
+            return Err(Error::WouldBlock)
+        }
+        let copy = options == PhysOptions::Copy;
+        self.phys.create_sub(0, self.phys.len(), copy).map_err(Error::Other)
+    }
 }
 }

+ 10 - 1
src/lib/h2o_rpc/imp/io/file.rs

@@ -1,6 +1,6 @@
 #[cfg(feature = "runtime")]
 #[cfg(feature = "runtime")]
 use entry::EntryServer;
 use entry::EntryServer;
-use solvent::ipc::{Channel, Packet};
+use solvent::{ipc::{Channel, Packet}, mem::Phys};
 
 
 use super::*;
 use super::*;
 
 
@@ -11,6 +11,13 @@ bitflags::bitflags! {
     }
     }
 }
 }
 
 
+#[derive(SerdePacket, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
+#[repr(u32)]
+pub enum PhysOptions {
+    Shared = 0,
+    Copy = 1,
+}
+
 #[protocol(EventFlags)]
 #[protocol(EventFlags)]
 pub trait File: entry::Entry {
 pub trait File: entry::Entry {
     /// Lock the entire file excluively until the connection is closed.
     /// Lock the entire file excluively until the connection is closed.
@@ -32,4 +39,6 @@ pub trait File: entry::Entry {
     fn write_at(offset: usize, buf: Vec<u8>) -> Result<usize, Error>;
     fn write_at(offset: usize, buf: Vec<u8>) -> Result<usize, Error>;
 
 
     fn resize(new_len: usize) -> Result<(), Error>;
     fn resize(new_len: usize) -> Result<(), Error>;
+
+    fn phys(options: PhysOptions) -> Result<Phys, Error>;
 }
 }