mikros: Implement OpenOptions and add File(Creation/Write/Open)Mode structs in os for RPC use

This commit is contained in:
pjht 2024-11-11 15:49:56 -06:00
parent e44f9abc78
commit fd1fa35d35
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
2 changed files with 123 additions and 9 deletions

View File

@ -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.

View File

@ -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,75 @@ pub fn file_type(&self) -> io::Result<FileType> {
}
}
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<FileOpenMode> {
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<File> {
let path = path.canonicalize()?;