mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2025-01-18 21:17:14 +04:00
Write Hello, world! to a file and read it!
This commit is contained in:
parent
5ea07840ce
commit
dbaa7c8c6c
@ -55,5 +55,16 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||
for name in root_inode.ls() {
|
||||
println!("{}", name);
|
||||
}
|
||||
{
|
||||
let filea = root_inode.find("filea").unwrap();
|
||||
println!("writing filea!");
|
||||
filea.write_at(0, "Hello, world!".as_bytes());
|
||||
}
|
||||
{
|
||||
let filea = root_inode.find("filea").unwrap();
|
||||
let mut buffer = [0u8; 512];
|
||||
let len = filea.read_at(0, &mut buffer);
|
||||
println!("{}", core::str::from_utf8(&buffer[..len]).unwrap());
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -276,4 +276,7 @@ impl DirEntry {
|
||||
let len = (0usize..).find(|i| self.name[*i] == 0).unwrap();
|
||||
core::str::from_utf8(&self.name[..len]).unwrap()
|
||||
}
|
||||
pub fn inode_number(&self) -> u32 {
|
||||
self.inode_number
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ use alloc::sync::Arc;
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use spin::{Mutex, MutexGuard};
|
||||
use crate::layout::DiskInodeType::Directory;
|
||||
|
||||
pub struct Inode {
|
||||
inode_id: u32,
|
||||
@ -41,11 +42,13 @@ impl Inode {
|
||||
name: &str,
|
||||
inode: &Dirty<DiskInode>,
|
||||
) -> Option<u32> {
|
||||
println!("into find_inode_id");
|
||||
// assert it is a directory
|
||||
assert!(inode.read(|inode| inode.is_dir()));
|
||||
let file_count = inode.read(|inode| {
|
||||
inode.size as usize
|
||||
}) / DIRENT_SZ;
|
||||
println!("file_count = {}", file_count);
|
||||
let mut dirent_space: DirentBytes = Default::default();
|
||||
for i in 0..file_count {
|
||||
assert_eq!(
|
||||
@ -58,8 +61,9 @@ impl Inode {
|
||||
}),
|
||||
DIRENT_SZ,
|
||||
);
|
||||
if DirEntry::from_bytes(&dirent_space).name() == name {
|
||||
return Some(i as u32);
|
||||
let dirent = DirEntry::from_bytes(&dirent_space);
|
||||
if dirent.name() == name {
|
||||
return Some(dirent.inode_number() as u32);
|
||||
}
|
||||
}
|
||||
None
|
||||
@ -184,7 +188,7 @@ impl Inode {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn write_at(&self, offset: usize, buf: &mut [u8]) -> usize {
|
||||
pub fn write_at(&self, offset: usize, buf: &[u8]) -> usize {
|
||||
let mut fs = self.fs.lock();
|
||||
let mut inode = self.get_disk_inode(&mut fs);
|
||||
self.increase_size((offset + buf.len()) as u32, &mut inode, &mut fs);
|
||||
|
Loading…
Reference in New Issue
Block a user