Skip to content

Commit

Permalink
ext4 file to read and to write examples
Browse files Browse the repository at this point in the history
  • Loading branch information
elliott10 committed Apr 19, 2024
1 parent c35c334 commit 64c8da3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
11 changes: 4 additions & 7 deletions examples/src/ext4fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::vfs_ops::{VfsNodeOps, VfsOps};
use alloc::sync::Arc;
use log::*;
use lwext4_rust::bindings::{
O_CREAT, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, SEEK_CUR, SEEK_END, SEEK_SET,
O_APPEND, O_CREAT, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, SEEK_CUR, SEEK_END, SEEK_SET,
};
use lwext4_rust::{Ext4BlockWrapper, Ext4File, InodeTypes, KernelDevOp};
use virtio_drivers::transport::Transport;
Expand Down Expand Up @@ -93,10 +93,6 @@ impl FileWrapper {

/// The [`VfsNodeOps`] trait provides operations on a file or a directory.
impl VfsNodeOps for FileWrapper {
#[inline]
fn as_any(&self) -> &dyn core::any::Any {
self
}

/*
fn get_attr(&self) -> Result<usize, i32> {
Expand Down Expand Up @@ -281,7 +277,8 @@ impl VfsNodeOps for FileWrapper {
let mut file = self.0.borrow_mut();
let path = file.get_path();
let path = path.to_str().unwrap();
file.file_open(path, O_RDWR)?;
//file.file_open(path, O_RDWR)?;
file.file_open(path, O_RDWR | O_CREAT | O_APPEND)?;

file.file_seek(offset as i64, SEEK_SET)?;
let r = file.file_write(buf);
Expand Down Expand Up @@ -317,4 +314,4 @@ impl Drop for FileWrapper {
file.file_close().expect("failed to close fd");
drop(file); // todo
}
}
}
33 changes: 11 additions & 22 deletions examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,42 +148,31 @@ fn virtio_blk<T: Transport>(transport: T) {

init_rootfs(blk);

/*
let mut input = vec![0xffu8; 512];
let mut output = vec![0; 512];
for i in 0..32 {
for x in input.iter_mut() {
*x = i as u8;
}
blk.write_blocks(i, &input).expect("failed to write");
blk.read_blocks(i, &mut output).expect("failed to read");
assert_eq!(input, output);
}
*/
info!("virtio-blk test finished");
}

pub fn init_rootfs<T: Transport>(dev: VirtIOBlk<HalImpl, T>) {
let disk = Disk::new(dev);
let ext4_fs: Box<dyn VfsOps> = Box::new(Ext4FileSystem::new(disk));
let root = ext4_fs.root_dir();
/*
let mut write_buf: [u8; 20] = [b'H'; 20];
let mut read_buf: [u8; 20] = [0; 20];

let new_file = "/ext4_test.txt";
let mut new_fd: Box<dyn VfsNodeOps> = Box::new(FileWrapper::new(new_file, InodeTypes::EXT4_INODE_MODE_FILE));
//let new_fd = root;
//new_fd.create(new_file, InodeTypes::EXT4_INODE_MODE_FILE);
root.create(new_file, InodeTypes::EXT4_DE_REG_FILE);

let mut new_fd: Box<dyn VfsNodeOps> =
Box::new(FileWrapper::new(new_file, InodeTypes::EXT4_INODE_MODE_FILE));

let mut write_buf: [u8; 20] = [0xFFu8; 20];
let mut read_buf: [u8; 20] = [0; 20];

new_fd.write_at(0, &write_buf);

new_fd.read_at(0, &mut read_buf);

//new_fd.remove(new_file);
root.remove(new_file);

println!("read file = {:#x?}", read_buf);
assert_eq!(write_buf, read_buf);
*/

drop(ext4_fs);
}
Expand Down
1 change: 1 addition & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Ext4File {
"r+"
}
};
debug!("flags_to_cstring: {}", cstr);
CString::new(cstr).expect("CString::new OpenFlags failed")
}

Expand Down

0 comments on commit 64c8da3

Please sign in to comment.