Skip to content

Commit 234c026

Browse files
committed
updata
1 parent 491a3c2 commit 234c026

File tree

4 files changed

+99
-76
lines changed

4 files changed

+99
-76
lines changed

src/block.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ impl BlockGroup {
8080
}
8181

8282

83-
pub fn write_file(&mut self,parent_inode : usize,) {
84-
83+
pub fn write_file(&mut self,parent_inode : usize,data :&[u8]) {
84+
let inode_index = self.inode_table[parent_inode].get_index();
85+
let block_index = self.get_block_for_file();
86+
self.data_block[block_index].write(data, 0);
8587
}
88+
8689
pub fn bg_list(&self,parent_inode : usize) {
8790
let mut all_dirs:Vec<DirectoryEntry> = vec![];
8891
for index in self.inode_table[parent_inode].direct_pointer{
@@ -150,13 +153,13 @@ impl BlockGroup {
150153
}
151154
}
152155
//找到空的块
153-
pub fn get_block_for_file(&mut self) -> u32 {
156+
pub fn get_block_for_file(&mut self) -> usize {
154157
if let Some(index) = self.block_bitmap.free_index() {
155-
return index as u32;
158+
return index;
156159
}
157160
self.block_bitmap.set(self.data_block.len(), true);
158161
self.data_block.push(DataBlock::new());
159-
return self.data_block.len() as u32 ;
162+
return self.data_block.len();
160163
}
161164

162165

@@ -288,7 +291,7 @@ impl DataBlock {
288291
}
289292
pub fn write(&mut self,data: &[u8],offset : usize) {
290293
for (i, &byte) in data.iter().enumerate().take(BLOCK_SIZE) {
291-
self.data[i] = byte;
294+
self.data[i + offset] = byte;
292295
}
293296
}
294297
pub fn count_free_bytes(&self) -> u16{

src/disk.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,30 @@ impl Filesystem for EXT2FS {
1919
Ok(())
2020
}
2121

22-
fn write(
22+
fn getxattr(
2323
&mut self,
2424
_req: &fuser::Request<'_>,
2525
_ino: u64,
26+
_name: &std::ffi::OsStr,
27+
_size: u32,
28+
reply: fuser::ReplyXattr,
29+
) {
30+
31+
32+
}
33+
fn write(
34+
&mut self,
35+
_req: &fuser::Request<'_>,
36+
ino: u64,
2637
_fh: u64,
2738
_offset: i64,
28-
_data: &[u8],
39+
data: &[u8],
2940
_write_flags: u32,
3041
_flags: i32,
3142
_lock_owner: Option<u64>,
3243
reply: fuser::ReplyWrite,
3344
) {
34-
45+
self.block_groups[0].write_file(ino as usize, data)
3546

3647
}
3748

@@ -66,18 +77,29 @@ impl Filesystem for EXT2FS {
6677
Some(index) => index,
6778
None => {
6879
let block_group = BlockGroup::new();
69-
self.blocks.push(block_group);
70-
self.blocks.len() - 1
80+
self.block_groups.push(block_group);
81+
self.block_groups.len() - 1
7182
},
7283
};
7384
//需要新建一个块
74-
self.blocks[block_group_index].bg_mkdir(name, _parent as usize);
85+
self.block_groups[block_group_index].bg_mkdir(name, _parent as usize);
7586

7687

7788
}
7889
fn rmdir(&mut self, _req: &fuser::Request<'_>, _parent: u64, _name: &std::ffi::OsStr, reply: fuser::ReplyEmpty) {
7990
let dir_name = _name.to_string_lossy().into_owned();
80-
self.blocks[0].bg_rmdir(_parent as usize, dir_name)
91+
self.block_groups[0].bg_rmdir(_parent as usize, dir_name)
92+
}
93+
94+
fn readdir(
95+
&mut self,
96+
_req: &fuser::Request<'_>,
97+
ino: u64,
98+
_fh: u64,
99+
_offset: i64,
100+
reply: fuser::ReplyDirectory,
101+
) {
102+
self.block_groups[0].bg_list(ino as usize);
81103
}
82104

83105
fn open(&mut self, _req: &fuser::Request<'_>, _ino: u64, _flags: i32, reply: fuser::ReplyOpen) {
@@ -94,7 +116,7 @@ impl EXT2FS {
94116
//将root文件夹放入第一个大块中
95117
EXT2FS{
96118
//boot_block : boot,
97-
blocks : vec![root_block],
119+
block_groups : vec![root_block],
98120
path : "root".to_string(),
99121
user_name : name,
100122
password : pwd,
@@ -103,11 +125,10 @@ impl EXT2FS {
103125

104126
}
105127
pub fn ls(&self,block_group_index : usize) {
106-
self.blocks[block_group_index].bg_list(self.current_inode_index);
107128
}
108129

109130
pub fn get_block_group(&self) -> Option<usize>{
110-
self.blocks
131+
self.block_groups
111132
.iter()
112133
.position(|x| !x.full())
113134
}

0 commit comments

Comments
 (0)