From 11c7f632a2069cfb3dd90dc9e7851743481ae919 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 11 Nov 2024 15:49:56 -0600 Subject: [PATCH] mikros: Implement OpenOptions and add File(Creation/Write/Open)Mode structs in os for RPC use --- library/std/src/os/mikros/mod.rs | 51 ++++++++++++++++++ library/std/src/sys/pal/mikros/fs.rs | 80 ++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 9 deletions(-) diff --git a/library/std/src/os/mikros/mod.rs b/library/std/src/os/mikros/mod.rs index ddbd21c2b23..07f8179b5e7 100644 --- a/library/std/src/os/mikros/mod.rs +++ b/library/std/src/os/mikros/mod.rs @@ -14,6 +14,57 @@ #[stable(feature = "mikros", since = "1.80.0")] pub use errno::Errno; + +#[stable(feature = "mikros", since = "1.80.0")] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum FileCreationMode { + NoCreate, + Create, + ForceCreate, +} + +#[stable(feature = "mikros", since = "1.80.0")] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum FileWriteMode { + Start, + Truncate, + Append +} + +#[stable(feature = "mikros", since = "1.80.0")] +#[derive(Copy, Clone, Debug)] +pub enum FileOpenMode { + Read, + Write(FileWriteMode, FileCreationMode), + ReadWrite(FileWriteMode, FileCreationMode), +} + +impl FileOpenMode { + #[stable(feature = "mikros", since = "1.80.0")] + pub fn readable(self) -> bool { + match self { + Self::Read | Self::ReadWrite(_, _) => true, + Self::Write(_, _) => false, + } + } + + #[stable(feature = "mikros", since = "1.80.0")] + pub fn writable(self) -> bool { + match self { + Self::Write(_, _) | Self::ReadWrite(_, _) => true, + Self::Read => false, + } + } + + #[stable(feature = "mikros", since = "1.80.0")] + pub fn get_writing_details(self) -> Option<(FileWriteMode, FileCreationMode)> { + match self { + Self::Write(wr_mode, cr_mode) | Self::ReadWrite(wr_mode, cr_mode) => Some((wr_mode, cr_mode)), + Self::Read => None, + } + } +} + /// A prelude for conveniently writing platform-specific code. /// /// Includes all extension traits, and some important type definitions. diff --git a/library/std/src/sys/pal/mikros/fs.rs b/library/std/src/sys/pal/mikros/fs.rs index d5eb9e4e3ac..28cbf02d51a 100644 --- a/library/std/src/sys/pal/mikros/fs.rs +++ b/library/std/src/sys/pal/mikros/fs.rs @@ -6,7 +6,7 @@ use crate::hash::{Hash, Hasher}; use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; use crate::os::mikros::ipc::rpc; -use crate::os::mikros::{Errno, syscalls}; +use crate::os::mikros::{Errno, syscalls, FileOpenMode, FileCreationMode, FileWriteMode}; use crate::path::{Component, Path, PathBuf}; use crate::sys::time::SystemTime; use crate::sys::unsupported; @@ -149,7 +149,14 @@ pub struct DirEntry { } #[derive(Clone, Debug)] -pub struct OpenOptions {} +pub struct OpenOptions { + read: bool, + write: bool, + append: bool, + truncate: bool, + create: bool, + create_new: bool, +} #[derive(Copy, Clone, Debug, Default)] pub struct FileTimes {} @@ -313,19 +320,74 @@ pub fn file_type(&self) -> io::Result { } } + impl OpenOptions { pub fn new() -> OpenOptions { - OpenOptions {} + OpenOptions { + // generic + read: false, + write: false, + append: false, + truncate: false, + create: false, + create_new: false, + } } - pub fn read(&mut self, _read: bool) {} - pub fn write(&mut self, _write: bool) {} - pub fn append(&mut self, _append: bool) {} - pub fn truncate(&mut self, _truncate: bool) {} - pub fn create(&mut self, _create: bool) {} - pub fn create_new(&mut self, _create_new: bool) {} + pub fn read(&mut self, read: bool) { + self.read = read; + } + pub fn write(&mut self, write: bool) { + self.write = write; + } + pub fn append(&mut self, append: bool) { + self.append = append; + } + pub fn truncate(&mut self, truncate: bool) { + self.truncate = truncate; + } + pub fn create(&mut self, create: bool) { + self.create = create; + } + pub fn create_new(&mut self, create_new: bool) { + self.create_new = create_new; + } + + fn get_open_mode(&self) -> io::Result { + if !self.write && (self.append || self.truncate || self.create || self.create_new) { + return Err(Errno::EINVAL.into()); + } + if self.append && self.truncate { + return Err(Errno::EINVAL.into()); + } + if self.append && self.truncate { + return Err(Errno::EINVAL.into()); + } + if !self.read && !self.write { + return Err(Errno::EINVAL.into()); + } + if self.read && !self.write { + return Ok(FileOpenMode::Read); + } + let cr_mode = match (self.create, self.create_new) { + (false, false) => FileCreationMode::NoCreate, + (true, false) => FileCreationMode::Create, + (_, true) => FileCreationMode::ForceCreate, + }; + let wr_mode = match (self.append, self.truncate) { + (false, false) => FileWriteMode::Start, + (false, true) => FileWriteMode::Truncate, + (true, false) => FileWriteMode::Append, + (true, true) => unreachable!() + }; + match self.read { + false => Ok(FileOpenMode::Write(wr_mode, cr_mode)), + true => Ok(FileOpenMode::ReadWrite(wr_mode, cr_mode)), + } + } } + impl File { pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result { let path = path.canonicalize()?;