summaryrefslogtreecommitdiff
path: root/src/disk
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-25 02:13:22 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-25 02:13:22 -0800
commit76ac602c3d79bb39c133c81a38425a77bc0b8b1f (patch)
treea1de9a03d16bd38fc6ab6b9568d2df562a913d96 /src/disk
parentb8afa7cfb02b32278e268924e189170496f81c1b (diff)
downloadmyfs-76ac602c3d79bb39c133c81a38425a77bc0b8b1f.tar.gz
myfs-76ac602c3d79bb39c133c81a38425a77bc0b8b1f.zip
Some FUSE callbacks, some POSIX interface implementation
Diffstat (limited to 'src/disk')
-rw-r--r--src/disk/allocation.rs2
-rw-r--r--src/disk/block.rs119
-rw-r--r--src/disk/inode.rs41
3 files changed, 95 insertions, 67 deletions
diff --git a/src/disk/allocation.rs b/src/disk/allocation.rs
index 5643dd3..bde2014 100644
--- a/src/disk/allocation.rs
+++ b/src/disk/allocation.rs
@@ -1,6 +1,6 @@
use crate::disk::block::{DataBlock, DoubleIndirectBlock, IndirectBlock, TripleIndirectBlock};
use crate::disk::inode::Inode;
-use crate::memory::cached_block::{convert, convert_mut};
+use crate::memory::cached_block::convert;
use crate::AyaFS;
impl AyaFS {
diff --git a/src/disk/block.rs b/src/disk/block.rs
index d287ee6..0058b3e 100644
--- a/src/disk/block.rs
+++ b/src/disk/block.rs
@@ -1,7 +1,10 @@
use crate::disk::inode::Inode;
+use std::ffi::{OsStr, OsString};
+use std::os::unix::ffi::OsStrExt;
pub trait Block: Default + Clone {}
+#[repr(C)]
#[derive(Clone)]
pub struct DataBlock(pub(crate) [u8; 4096]);
@@ -13,6 +16,7 @@ impl Default for DataBlock {
impl Block for DataBlock {}
+#[repr(C)]
#[derive(Clone)]
pub struct InodeBlock {
pub(crate) inodes: [Inode; 16],
@@ -47,53 +51,70 @@ impl Block for InodeBlock {}
const FULL_MAP: u32 = 0b111_111_111_111_111;
+#[repr(C)]
#[derive(Clone)]
-pub struct DirectoryBlock {
- entries: [[u8; 256]; 15],
- inode_ids: [usize; 15],
- occupancy_map: u32,
- reserved: [u8; 132],
+pub struct DirectoryEntry {
+ pub inode: u32,
+ pub record_len: u16,
+ pub name_len: u8,
+ pub file_type: u8,
+ pub name: [u8; 256],
}
-impl Default for DirectoryBlock {
+impl DirectoryEntry {
+ pub(crate) fn name(&self) -> OsString {
+ let name = &self.name[0..self.name_len as usize];
+ OsStr::from_bytes(name).to_os_string()
+ }
+}
+
+impl Default for DirectoryEntry {
fn default() -> Self {
Self {
- entries: [[0; 256]; 15],
- inode_ids: [0; 15],
- occupancy_map: 0,
- reserved: [0xFF; 132],
+ inode: 0,
+ record_len: 0,
+ name_len: 0,
+ file_type: 0x0,
+ name: [0; 256],
}
}
}
-impl Block for DirectoryBlock {}
-
-impl DirectoryBlock {
- fn vacant(&self) -> bool {
- self.occupancy_map & FULL_MAP != FULL_MAP
- }
-
- fn first_free(&self) -> Option<usize> {
- todo!()
- }
-
- fn mark_busy(&mut self, entry_id: usize) {
- todo!()
- }
+#[repr(C)]
+#[derive(Clone)]
+pub struct DirectoryBlock {
+ pub entries: [DirectoryEntry; 15],
+ reserved: [u8; 136],
+}
- /// 需要判断 entry_name.len() <= 255
- pub fn write_entry(&mut self, entry_name: &[u8], entry_inode_id: usize) -> Option<usize> {
- if let Some(entry_id) = self.first_free() {
- self.mark_busy(entry_id);
- self.entries[entry_id].copy_from_slice(entry_name);
- self.inode_ids[entry_id] = entry_inode_id;
- Some(entry_id)
- } else {
- None
+impl Default for DirectoryBlock {
+ fn default() -> Self {
+ Self {
+ entries: [
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ DirectoryEntry::default(),
+ ],
+ reserved: [0xFF; 136],
}
}
}
+impl Block for DirectoryBlock {}
+
+#[repr(C)]
#[derive(Clone)]
pub struct IndirectBlock {
pub entries: [u32; 1024],
@@ -107,16 +128,7 @@ impl Default for IndirectBlock {
impl Block for IndirectBlock {}
-impl IndirectBlock {
- pub fn full(&self) -> bool {
- todo!()
- }
-
- pub fn allocate(&mut self) -> Option<usize> {
- todo!()
- }
-}
-
+#[repr(C)]
#[derive(Clone)]
pub struct DoubleIndirectBlock {
pub indirect: [u32; 1024],
@@ -132,16 +144,7 @@ impl Default for DoubleIndirectBlock {
impl Block for DoubleIndirectBlock {}
-impl DoubleIndirectBlock {
- pub fn full(&self) -> bool {
- todo!()
- }
-
- pub fn allocate(&mut self) -> Option<usize> {
- todo!()
- }
-}
-
+#[repr(C)]
#[derive(Clone)]
pub struct TripleIndirectBlock {
pub double_indirect: [u32; 1024],
@@ -156,13 +159,3 @@ impl Default for TripleIndirectBlock {
}
impl Block for TripleIndirectBlock {}
-
-impl TripleIndirectBlock {
- pub fn full(&self) -> bool {
- todo!()
- }
-
- pub fn allocate(&mut self) -> Option<usize> {
- todo!()
- }
-}
diff --git a/src/disk/inode.rs b/src/disk/inode.rs
index 1095aca..256d2f5 100644
--- a/src/disk/inode.rs
+++ b/src/disk/inode.rs
@@ -1,11 +1,11 @@
use bitflags::bitflags;
use fuser::{FileAttr, FileType};
-use std::fs::File;
+use crate::utils;
-const DIRECT_NUMBER: usize = 15;
+pub const DIRECT_NUMBER: usize = 15;
#[derive(Debug, Clone, Copy)]
-pub struct InodeMode(u16);
+pub struct InodeMode(pub u16);
bitflags! {
impl InodeMode: u16 {
@@ -33,6 +33,34 @@ bitflags! {
}
impl InodeMode {
+ pub(crate) fn exec_other(&self) -> bool {
+ self.0 & Self::IXOTH.0 != 0
+ }
+ pub(crate) fn write_other(&self) -> bool {
+ self.0 & Self::IWOTH.0 != 0
+ }
+ pub(crate) fn read_other(&self) -> bool {
+ self.0 & Self::IROTH.0 != 0
+ }
+ pub(crate) fn exec_group(&self) -> bool {
+ self.0 & Self::IXGRP.0 != 0
+ }
+ pub(crate) fn write_group(&self) -> bool {
+ self.0 & Self::IWGRP.0 != 0
+ }
+ pub(crate) fn read_group(&self) -> bool {
+ self.0 & Self::IRGRP.0 != 0
+ }
+ pub(crate) fn exec_user(&self) -> bool {
+ self.0 & Self::IXUSR.0 != 0
+ }
+ pub(crate) fn write_user(&self) -> bool {
+ self.0 & Self::IWUSR.0 != 0
+ }
+ pub(crate) fn read_user(&self) -> bool {
+ self.0 & Self::IRUSR.0 != 0
+ }
+
pub(crate) fn perm(&self) -> u16 {
self.0 & 0x0FFF
}
@@ -69,6 +97,13 @@ impl From<InodeMode> for FileType {
}
}
+impl From<InodeMode> for u8 {
+ fn from(value: InodeMode) -> Self {
+ utils::from_filetype(value.into())
+ }
+}
+
+
/// Pretty much the same with ext2, with minor changes:
/// - removed OS dependent attributes (osd1 & osd2)
/// - removed i_faddr since fragmentation is not supported