Skip to content

Commit

Permalink
Add flags to file system
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD committed Jun 27, 2024
1 parent 93eb10a commit ce25046
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
113 changes: 113 additions & 0 deletions Modules/File_system/src/Generics/Fundamentals/Flags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use super::Permission_type;

#[inline]
fn Set_bit(Data: &mut u8, Position: u8, Value: bool) {
if Value {
*Data |= 1 << Position;
} else {
*Data &= !(1 << Position);
}
}

#[inline]
fn Get_bit(Data: &u8, Bit: u8) -> bool {
(Data & (1 << Bit)) != 0
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(transparent)]
pub struct Mode_type(u8);

impl Mode_type {
const Read_bit: u8 = 0;
const Write_bit: u8 = 1;

pub fn Read_only() -> Self {
Self(1 << Self::Read_bit)
}

pub fn Write_only() -> Self {
Self(1 << Self::Write_bit)
}

pub fn Read_write() -> Self {
Self((1 << Self::Read_bit) | (1 << Self::Write_bit))
}

pub fn Get_read(&self) -> bool {
Get_bit(&self.0, Self::Read_bit)
}

pub fn Get_write(&self) -> bool {
Get_bit(&self.0, Self::Write_bit)
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(transparent)]
pub struct Status_type(u8);

impl Default for Status_type {
fn default() -> Self {
Self(0).Set_append(false).Set_non_blocking(false)
}
}

impl Status_type {
const Append_bit: u8 = 0;
const Non_blocking_bit: u8 = 1;

pub fn Set_non_blocking(mut self, Value: bool) -> Self {
Set_bit(&mut self.0, Self::Non_blocking_bit, Value);
self
}

pub fn Get_non_blocking(&self) -> bool {
Get_bit(&self.0, Self::Non_blocking_bit)
}

pub fn Set_append(mut self, Value: bool) -> Self {
Set_bit(&mut self.0, Self::Append_bit, Value);
self
}

pub fn Get_append(&self) -> bool {
Get_bit(&self.0, Self::Append_bit)
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(transparent)]
pub struct Flags_type(u8);

impl Flags_type {
pub fn New(Mode: Mode_type, Status: Option<Status_type>) -> Self {
Self(Mode.0 << 4 | Status.unwrap_or_default().0)
}

pub fn Get_mode(&self) -> Mode_type {
Mode_type(self.0 >> 4)
}

pub fn Get_status(&self) -> Status_type {
Status_type(self.0 & 0b00001111)
}

pub fn Set_status(&mut self, Status: Status_type) {
self.0 = (self.0 & 0b11110000) | Status.0;
}

pub fn Is_permission_granted(&self, Permission: &Permission_type) -> bool {
let Mode = self.Get_mode();

(Permission.Get_read() && Mode.Get_read()) // Read permission
|| (Permission.Get_write() && (Mode.Get_write() || self.Get_status().Get_append()))
// Write permission
}
}

impl From<Mode_type> for Flags_type {
fn from(Mode: Mode_type) -> Self {
Self::New(Mode, None)
}
}
5 changes: 2 additions & 3 deletions Modules/File_system/src/Generics/Fundamentals/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{ops, u64};

pub mod Path;
mod Flags;
pub use Flags::*;
pub use Path::*;
use Shared::Discriminant_trait;

Expand Down

0 comments on commit ce25046

Please sign in to comment.