diff --git a/Makefile b/Makefile index 222226946ab9b7ab9763bc118a0b2b1ea48c6df0..443d50d19a01a439be62783c1c4dbf2375188881 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,8 @@ else endif image.bin: - dd if=/dev/zero of=image.bin bs=1048576 count=1024 cargo build --release --bin redoxfs-mkfs + dd if=/dev/zero of=image.bin bs=1048576 count=1024 target/release/redoxfs-mkfs image.bin mount: image.bin FORCE diff --git a/src/dir.rs b/src/dir.rs index 309ac0a3a54c53f84a97456d84cacf8713138073..cea613d1c8bd3266971a20479dd84b27568b3645 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -1,28 +1,24 @@ use alloc::{boxed::Box, vec}; use core::{mem, ops, slice, str}; -use crate::{BlockLevel, BlockTrait, Node, TreePtr, RECORD_LEVEL}; +use crate::{BlockLevel, BlockTrait, Node, TreePtr, RECORD_LEVEL, DIR_ENTRY_MAX_LENGTH}; #[repr(packed)] pub struct DirEntry { node_ptr: TreePtr<Node>, - name: [u8; 252], + name: [u8; DIR_ENTRY_MAX_LENGTH], } impl DirEntry { - pub fn new(node_ptr: TreePtr<Node>, name: &str) -> Option<DirEntry> { + pub fn new(node_ptr: TreePtr<Node>, name: &str) -> DirEntry { let mut entry = DirEntry { node_ptr, ..Default::default() }; - if name.len() > entry.name.len() { - return None; - } - entry.name[..name.len()].copy_from_slice(name.as_bytes()); - Some(entry) + entry } pub fn node_ptr(&self) -> TreePtr<Node> { @@ -54,7 +50,7 @@ impl Default for DirEntry { fn default() -> Self { Self { node_ptr: TreePtr::default(), - name: [0; 252], + name: [0; DIR_ENTRY_MAX_LENGTH], } } } diff --git a/src/lib.rs b/src/lib.rs index b6552c9e58b0a82bf7196f0d6c80795fcf92997d..d1cf367f8619e4e9e3ee2ccaeec85e8d0cef016b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,8 @@ pub const RECORD_LEVEL: usize = 5; pub const RECORD_SIZE: u64 = BLOCK_SIZE << RECORD_LEVEL; pub const SIGNATURE: &[u8; 8] = b"RedoxFS\0"; pub const VERSION: u64 = 6; +pub const DIR_ENTRY_MAX_LENGTH: usize = 252; + pub static IS_UMT: AtomicUsize = AtomicUsize::new(0); pub use self::allocator::{AllocEntry, AllocList, Allocator, ALLOC_LIST_ENTRIES}; diff --git a/src/transaction.rs b/src/transaction.rs index 36a2afa522f5ceac952b4cdc7ea0a3165b2897dd..487bf57ac2a0f1b211dbb0676ff71321c01ba610 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -12,11 +12,7 @@ use syscall::error::{ Error, Result, EEXIST, EINVAL, EIO, EISDIR, ENOENT, ENOSPC, ENOTDIR, ENOTEMPTY, ERANGE, }; -use crate::{ - AllocEntry, AllocList, Allocator, BlockAddr, BlockData, BlockLevel, BlockPtr, BlockTrait, - DirEntry, DirList, Disk, FileSystem, Header, Node, NodeLevel, RecordRaw, TreeData, TreePtr, - ALLOC_LIST_ENTRIES, HEADER_RING, -}; +use crate::{AllocEntry, AllocList, Allocator, BlockAddr, BlockData, BlockLevel, BlockPtr, BlockTrait, DirEntry, DirList, Disk, FileSystem, Header, Node, NodeLevel, RecordRaw, TreeData, TreePtr, ALLOC_LIST_ENTRIES, HEADER_RING, DIR_ENTRY_MAX_LENGTH}; pub struct Transaction<'a, D: Disk> { fs: &'a mut FileSystem<D>, @@ -569,12 +565,8 @@ impl<'a, D: Disk> Transaction<'a, D> { ctime: u64, ctime_nsec: u32, ) -> Result<TreeData<Node>> { - if name.contains(':') { - return Err(Error::new(EINVAL)); - } - - if self.find_node(parent_ptr, name).is_ok() { - return Err(Error::new(EEXIST)); + if let Err(err) = self.check_name(&parent_ptr, &name){ + return Err(err); } unsafe { @@ -605,13 +597,11 @@ impl<'a, D: Disk> Transaction<'a, D> { name: &str, node_ptr: TreePtr<Node>, ) -> Result<()> { - if name.contains(':') { - return Err(Error::new(EINVAL)); - } + if let Err(err) = self.check_name(&parent_ptr, &name){ + return Err(err); + } - if self.find_node(parent_ptr, name).is_ok() { - return Err(Error::new(EEXIST)); - } + let entry = DirEntry::new(node_ptr, name); let mut parent = self.read_tree(parent_ptr)?; @@ -619,8 +609,6 @@ impl<'a, D: Disk> Transaction<'a, D> { let links = node.data().links(); node.data_mut().set_links(links + 1); - let entry = DirEntry::new(node_ptr, name).ok_or(Error::new(EINVAL))?; - let record_level = parent.data().record_level(); let record_end = parent.data().size() / record_level.bytes(); for record_offset in 0..record_end { @@ -795,6 +783,24 @@ impl<'a, D: Disk> Transaction<'a, D> { Ok(()) } + fn check_name(&mut self, + parent_ptr: &TreePtr<Node>, + name: &str) -> Result<()> { + if name.contains(':') { + return Err(Error::new(EINVAL)); + } + + if name.len() > DIR_ENTRY_MAX_LENGTH { + return Err(Error::new(EINVAL)); + } + + if self.find_node(parent_ptr.clone(), name).is_ok() { + return Err(Error::new(EEXIST)); + } + + Ok(()) + } + fn node_record_ptr( &mut self, node: &TreeData<Node>,