浏览代码

refactor: remove file module

liujingx 9 月之前
父节点
当前提交
da7c561b04
共有 5 个文件被更改,包括 19 次插入140 次删除
  1. 18 39
      ext4_test/src/main.rs
  2. 0 28
      src/ext4/high_level.rs
  3. 0 67
      src/ext4_defs/file.rs
  4. 0 2
      src/ext4_defs/mod.rs
  5. 1 4
      src/lib.rs

+ 18 - 39
ext4_test/src/main.rs

@@ -1,5 +1,5 @@
+use another_ext4::{Ext4, InodeMode, EXT4_ROOT_INO};
 use block_file::BlockFile;
-use another_ext4::{Ext4, InodeMode, OpenFlags, EXT4_ROOT_INO};
 use simple_logger::SimpleLogger;
 use std::sync::Arc;
 
@@ -29,14 +29,6 @@ fn open_ext4() -> Ext4 {
 
 fn mkdir_test(ext4: &mut Ext4) {
     let dir_mode: InodeMode = InodeMode::DIRECTORY | InodeMode::ALL_RWX;
-    for i in 0..1000 {
-        ext4.generic_create(ROOT_INO, &format!("d{}", i), dir_mode)
-            .expect("mkdir failed");
-    }
-    for i in 0..1000 {
-        ext4.generic_lookup(ROOT_INO, &format!("d{}", i))
-            .expect("lookup failed");
-    }
     ext4.generic_create(ROOT_INO, "d1", dir_mode)
         .expect("mkdir failed");
     ext4.generic_create(ROOT_INO, "d1/d2", dir_mode)
@@ -69,37 +61,19 @@ fn create_test(ext4: &mut Ext4) {
 
 fn read_write_test(ext4: &mut Ext4) {
     let wbuffer = "hello world".as_bytes();
-    let wfile = ext4
-        .generic_open(ROOT_INO, "d3/f0", OpenFlags::O_WRONLY)
-        .expect("open failed");
-    ext4.write(wfile.inode, 0, wbuffer).expect("write failed");
-
+    let file = ext4.generic_lookup(ROOT_INO, "d3/f0").expect("open failed");
+    ext4.write(file, 0, wbuffer).expect("write failed");
     let mut rbuffer = vec![0u8; wbuffer.len() + 100]; // Test end of file
-    let rfile = ext4
-        .generic_open(ROOT_INO, "d3/f0", OpenFlags::O_RDONLY)
-        .expect("open failed");
-    let rcount = ext4
-        .read(rfile.inode, 0, &mut rbuffer)
-        .expect("read failed");
-
+    let rcount = ext4.read(file, 0, &mut rbuffer).expect("read failed");
     assert_eq!(wbuffer, &rbuffer[..rcount]);
 }
 
 fn large_read_write_test(ext4: &mut Ext4) {
     let wbuffer = vec![99u8; 1024 * 1024 * 16];
-    let wfile = ext4
-        .generic_open(ROOT_INO, "d3/f1", OpenFlags::O_WRONLY)
-        .expect("open failed");
-    ext4.write(wfile.inode, 0, &wbuffer).expect("write failed");
-
-    let rfile = ext4
-        .generic_open(ROOT_INO, "d3/f1", OpenFlags::O_RDONLY)
-        .expect("open failed");
+    let file = ext4.generic_lookup(ROOT_INO, "d3/f1").expect("open failed");
+    ext4.write(file, 0, &wbuffer).expect("write failed");
     let mut rbuffer = vec![0u8; wbuffer.len()];
-    let rcount = ext4
-        .read(rfile.inode, 0, &mut rbuffer)
-        .expect("read failed");
-
+    let rcount = ext4.read(file, 0, &mut rbuffer).expect("read failed");
     assert_eq!(wbuffer, &rbuffer[..rcount]);
 }
 
@@ -132,16 +106,21 @@ fn xattr_test(ext4: &mut Ext4) {
 
     let names = ext4.listxattr(file).expect("listxattr failed");
     assert_eq!(names, vec!["user.testone", "user.testtwo"]);
-    
-    let value = ext4.getxattr(file, "user.testone").expect("getxattr failed");
+
+    let value = ext4
+        .getxattr(file, "user.testone")
+        .expect("getxattr failed");
     assert_eq!(value, "hello world".as_bytes());
-    let value = ext4.getxattr(file, "user.testtwo").expect("getxattr failed");
+    let value = ext4
+        .getxattr(file, "user.testtwo")
+        .expect("getxattr failed");
     assert_eq!(value, "world hello".as_bytes());
-    
+
     let names = ext4.listxattr(file).expect("listxattr failed");
     assert_eq!(names, vec!["user.testone", "user.testtwo"]);
-    
-    ext4.removexattr(file, "user.testone").expect("removexattr failed");
+
+    ext4.removexattr(file, "user.testone")
+        .expect("removexattr failed");
     ext4.getxattr(file, "user.testone")
         .expect_err("getxattr failed");
     let names = ext4.listxattr(file).expect("listxattr failed");

+ 0 - 28
src/ext4/high_level.rs

@@ -41,34 +41,6 @@ impl Ext4 {
         Ok(cur)
     }
 
-    /// (DEPRECATED) Open a file in the filesystem.
-    ///
-    /// # Params
-    ///
-    /// * `root` - The inode id of the root directory for search.
-    /// * `path` - The path of the object to be opened.
-    /// * `flags` - The open flags. Creation (O_CREAT, O_EXCL, O_NOCTTY) flags
-    ///             will be ignored.
-    ///
-    /// # Return
-    ///
-    /// `Ok(fh)` - File handler
-    ///
-    /// # Error
-    ///
-    /// * `ENOENT` - The file does not exist.
-    /// * `EISDIR` - The file is a directory.
-    #[deprecated]
-    pub fn generic_open(&self, root: InodeId, path: &str, flags: OpenFlags) -> Result<FileHandler> {
-        let inode_id = self.generic_lookup(root, path)?;
-        let inode = self.read_inode(inode_id);
-        // Check file type
-        if !inode.inode.is_file() {
-            return_error!(ErrCode::EISDIR, "File {} is not a regular file", path);
-        }
-        Ok(FileHandler::new(inode.id, flags, inode.inode.size()))
-    }
-
     /// Create an object in the filesystem.
     ///
     /// This function will perform recursive-creation i.e. if the parent

+ 0 - 67
src/ext4_defs/file.rs

@@ -1,67 +0,0 @@
-use crate::prelude::*;
-
-#[derive(Debug)]
-pub struct FileHandler {
-    /// The inode number of the file
-    pub inode: InodeId,
-    /// The open flags for the file
-    pub flags: OpenFlags,
-    /// The size of the file
-    pub fsize: u64,
-    /// The current position in the file
-    pub fpos: usize,
-}
-
-impl FileHandler {
-    pub fn new(inode: InodeId, flags: OpenFlags, fsize: u64) -> Self {
-        FileHandler {
-            inode,
-            flags,
-            fsize,
-            fpos: 0,
-        }
-    }
-}
-
-bitflags! {
-    #[derive(Debug, Clone, Copy)]
-    pub struct OpenFlags: u32 {
-        const O_ACCMODE = 0o0003;
-        const O_RDONLY = 0o00;
-        const O_WRONLY = 0o01;
-        const O_RDWR = 0o02;
-        const O_CREAT = 0o0100;
-        const O_EXCL = 0o0200;
-        const O_NOCTTY = 0o0400;
-        const O_TRUNC = 0o01000;
-        const O_APPEND = 0o02000;
-        const O_NONBLOCK = 0o04000;
-        const O_NDELAY = Self::O_NONBLOCK.bits();
-        const O_SYNC = 0o4010000;
-        const O_FSYNC = Self::O_SYNC.bits();
-        const O_ASYNC = 0o020000;
-        const O_LARGEFILE = 0o0100000;
-        const O_DIRECTORY = 0o0200000;
-        const O_NOFOLLOW = 0o0400000;
-        const O_CLOEXEC = 0o2000000;
-        const O_DIRECT = 0o040000;
-        const O_NOATIME = 0o1000000;
-        const O_PATH = 0o10000000;
-        const O_DSYNC = 0o010000;
-        const O_TMPFILE = 0o20000000 | Self::O_DIRECTORY.bits();
-    }
-}
-
-impl OpenFlags {
-    pub fn from_str(flags: &str) -> Result<Self> {
-        match flags {
-            "r" | "rb" => Ok(Self::O_RDONLY),
-            "w" | "wb" => Ok(Self::O_WRONLY | Self::O_CREAT | Self::O_TRUNC),
-            "a" | "ab" => Ok(Self::O_WRONLY | Self::O_CREAT | Self::O_APPEND),
-            "r+" | "rb+" | "r+b" => Ok(Self::O_RDWR),
-            "w+" | "wb+" | "w+b" => Ok(Self::O_RDWR | Self::O_CREAT | Self::O_TRUNC),
-            "a+" | "ab+" | "a+b" => Ok(Self::O_RDWR | Self::O_CREAT | Self::O_APPEND),
-            _ => Err(Ext4Error::new(ErrCode::EINVAL)),
-        }
-    }
-}

+ 0 - 2
src/ext4_defs/mod.rs

@@ -20,7 +20,6 @@ mod block_group;
 mod crc;
 mod dir;
 mod extent;
-mod file;
 mod inode;
 mod mount_point;
 mod super_block;
@@ -34,7 +33,6 @@ pub use block::*;
 pub use block_group::*;
 pub use dir::*;
 pub use extent::*;
-pub use file::*;
 pub use inode::*;
 pub use super_block::*;
 pub use xattr::*;

+ 1 - 4
src/lib.rs

@@ -11,7 +11,4 @@ mod prelude;
 pub use constants::{BLOCK_SIZE, EXT4_ROOT_INO, INODE_BLOCK_SIZE};
 pub use error::{ErrCode, Ext4Error};
 pub use ext4::Ext4;
-pub use ext4_defs::{
-    Block, BlockDevice, DirEntry, FileAttr, FileHandler, FileType, Inode, InodeMode, InodeRef,
-    OpenFlags,
-};
+pub use ext4_defs::{Block, BlockDevice, DirEntry, FileAttr, FileType, Inode, InodeMode, InodeRef};