use core::panic::RefUnwindSafe;
use crate::prelude::*;
use crate::ext4_defs::*;
use crate::return_errno;
use crate::return_errno_with_message;
use crate::utils::path_check;
pub use crate::ext4_defs::Ext4;
pub use crate::ext4_defs::BLOCK_SIZE;
pub use crate::ext4_defs::BlockDevice;
pub use crate::ext4_defs::InodeFileType;
impl Ext4 {
fn ext4_parse_flags(&self, flags: &str) -> Result<i32> {
match flags {
"r" | "rb" => Ok(O_RDONLY),
"w" | "wb" => Ok(O_WRONLY | O_CREAT | O_TRUNC),
"a" | "ab" => Ok(O_WRONLY | O_CREAT | O_APPEND),
"r+" | "rb+" | "r+b" => Ok(O_RDWR),
"w+" | "wb+" | "w+b" => Ok(O_RDWR | O_CREAT | O_TRUNC),
"a+" | "ab+" | "a+b" => Ok(O_RDWR | O_CREAT | O_APPEND),
_ => Err(Ext4Error::new(Errno::EINVAL)),
}
}
pub fn ext4_file_open(
&self,
path: &str,
flags: &str,
) -> Result<u32> {
let mut parent_inode_num = ROOT_INODE;
let filetype = InodeFileType::S_IFREG;
let iflags = self.ext4_parse_flags(flags).unwrap();
let filetype = InodeFileType::S_IFDIR;
let mut create = false;
if iflags & O_CREAT != 0 {
create = true;
}
self.generic_open(path, &mut parent_inode_num, create, filetype.bits(), &mut 0)
}
pub fn ext4_dir_mk(&self, path: &str) -> Result<u32> {
let mut search_result = Ext4DirSearchResult::new(Ext4DirEntry::default());
let r = self.dir_find_entry(ROOT_INODE, path, &mut search_result);
if r.is_ok() {
return_errno!(Errno::EEXIST);
}
let mut parent_inode_num = ROOT_INODE;
let filetype = InodeFileType::S_IFDIR;
self.generic_open(path, &mut parent_inode_num, true, filetype.bits(), &mut 0)
}
pub fn ext4_dir_open(
&self,
path: &str,
) -> Result<u32> {
let mut parent_inode_num = ROOT_INODE;
let filetype = InodeFileType::S_IFDIR;
self.generic_open(path, &mut parent_inode_num, false, filetype.bits(), &mut 0)
}
pub fn ext4_dir_get_entries(&self, inode: u32) -> Vec<Ext4DirEntry> {
let mut entries = self.dir_get_entries(inode);
entries
}
pub fn ext4_file_read(
&self,
ino: u64,
size: u32,
offset: i64,
) -> Result<Vec<u8>> {
let mut data = vec![0u8; size as usize];
let read_size = self.read_at(ino as u32, offset as usize, &mut data)?;
let r = data[..read_size].to_vec();
Ok(r)
}
pub fn ext4_file_write(
&self,
ino: u64,
offset: i64,
data: &[u8],
) -> Result<usize> {
let write_size = self.write_at(ino as u32, offset as usize, data)?;
Ok(write_size)
}
}