Skip to content

Commit 99716d0

Browse files
committed
check lookup
1 parent 1853ffa commit 99716d0

File tree

6 files changed

+109
-80
lines changed

6 files changed

+109
-80
lines changed

src/block.rs

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
mem,
1111
sync::Arc,
1212
time::{SystemTime, UNIX_EPOCH},
13-
vec,
13+
vec, f32::consts::E,
1414
};
1515

1616
pub const BLOCK_SIZE: usize = 1024; //每个数据块的大小为1kB
@@ -169,9 +169,7 @@ impl BlockGroup {
169169
}
170170

171171
pub fn bg_list(&self, parent_inode: usize) -> Vec<DirectoryEntry> {
172-
dbg!(self.inode_table.get(parent_inode as usize - 1));
173172
let mut all_dirs: Vec<DirectoryEntry> = vec![];
174-
dbg!(self.inode_table[parent_inode as usize - 1].direct_pointer);
175173
for index in self.inode_table[parent_inode as usize - 1].direct_pointer {
176174
if let Some(i_block) = index {
177175
all_dirs.append(&mut self.data_block[i_block as usize].get_all_dirs_name());
@@ -188,10 +186,13 @@ impl BlockGroup {
188186
}
189187
}
190188

191-
pub fn bg_mkdir(&mut self, name: String, parent_inode: usize) {
189+
pub fn bg_mkdir(&mut self, name: String, parent_inode: usize) -> Option<fuser::FileAttr> {
192190
let child_inode = self.add_entry_to_directory(name, parent_inode);
193-
self.add_entry_to_directory(".".to_string(), child_inode);
194-
self.add_entry_to_directory("..".to_string(), child_inode);
191+
let t = self.add_entry_to_directory(".".to_string(), child_inode);
192+
dbg!(t);
193+
let m = self.add_entry_to_directory("..".to_string(), child_inode);
194+
dbg!(m);
195+
Some(self.inode_table[child_inode - 1].get_file_attr(child_inode as u64))
195196
}
196197

197198
pub fn bg_lookup(&mut self, name: String, parent_inode: usize) -> Option<fuser::FileAttr> {
@@ -205,36 +206,9 @@ impl BlockGroup {
205206
let data_block = &self.data_block[index as usize];
206207
let dirs = data_block.get_all_dirs_name();
207208
for dir in dirs {
208-
if dir.name == name {
209+
if dbg!(dir.name) == name {
209210
let inode = &self.inode_table[index as usize - 1];
210-
return Some(fuser::FileAttr {
211-
ino: index as u64,
212-
size: inode.i_size as u64,
213-
// todo
214-
blocks: 0,
215-
atime: SystemTime::UNIX_EPOCH
216-
+ std::time::Duration::from_secs(inode.i_atime as u64),
217-
mtime: SystemTime::UNIX_EPOCH
218-
+ std::time::Duration::from_secs(inode.i_mtime as u64),
219-
ctime: SystemTime::UNIX_EPOCH
220-
+ std::time::Duration::from_secs(inode.i_ctime as u64),
221-
crtime: SystemTime::UNIX_EPOCH
222-
+ std::time::Duration::from_secs(inode.i_ctime as u64),
223-
kind: match inode.i_mode & 0xf000 {
224-
0x8000 => fuser::FileType::RegularFile,
225-
0x4000 => fuser::FileType::Directory,
226-
_ => fuser::FileType::RegularFile,
227-
},
228-
perm: inode.i_mode & 0x0fff,
229-
nlink: inode.i_links_count as u32,
230-
uid: inode.i_uid as u32,
231-
gid: inode.i_gid as u32,
232-
// 不熟这些 attr 先不实现
233-
rdev: 0,
234-
flags: 0,
235-
blksize: BLOCK_SIZE as u32,
236-
padding: 0,
237-
});
211+
return Some(inode.get_file_attr(index as u64));
238212
}
239213
}
240214
}
@@ -244,30 +218,7 @@ impl BlockGroup {
244218

245219
pub fn bg_getattr(&self, inode_index: usize) -> fuser::FileAttr {
246220
let inode = self.inode_table.get(inode_index - 1).unwrap();
247-
fuser::FileAttr {
248-
ino: inode_index as u64,
249-
size: inode.i_size as u64,
250-
// todo
251-
blocks: 0,
252-
atime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(inode.i_atime as u64),
253-
mtime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(inode.i_mtime as u64),
254-
ctime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(inode.i_ctime as u64),
255-
crtime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(inode.i_ctime as u64),
256-
kind: match inode.i_mode & 0xf000 {
257-
0x8000 => fuser::FileType::RegularFile,
258-
0x4000 => fuser::FileType::Directory,
259-
_ => fuser::FileType::RegularFile,
260-
},
261-
perm: inode.i_mode & 0x0fff,
262-
nlink: inode.i_links_count as u32,
263-
uid: inode.i_uid as u32,
264-
gid: inode.i_gid as u32,
265-
// 不熟这些 attr 先不实现
266-
rdev: 0,
267-
flags: 0,
268-
blksize: BLOCK_SIZE as u32,
269-
padding: 0,
270-
}
221+
inode.get_file_attr(inode_index as u64)
271222
}
272223

273224
pub fn bg_create(&mut self, name: String, parent_inode: usize) -> Option<fuser::FileAttr> {
@@ -279,8 +230,7 @@ impl BlockGroup {
279230
let inode_index = self.get_inode(); //分配一个inode
280231
let mut dir = DirectoryEntry::new(name, FileType::Directory, inode_index as u32, 0);
281232
let (dir_data, dir_size) = dir.to_bytes();
282-
283-
for block_index in self
233+
for &block_index in self
284234
.inode_table
285235
.get(parent_inode - 1)
286236
.unwrap()
@@ -289,7 +239,7 @@ impl BlockGroup {
289239
.take_while(|x: &&Option<u32>| x.is_some())
290240
{
291241
if let Some(index) = block_index {
292-
let data_block = &mut self.data_block[index.clone() as usize];
242+
let data_block = &mut self.data_block[index as usize];
293243
if data_block.count_free_bytes() >= dir_size as u16 {
294244
data_block.write(
295245
&dir_data,
@@ -420,6 +370,33 @@ impl Inode {
420370
}
421371
}
422372

373+
pub fn get_file_attr(&self, inode_index : u64) -> fuser::FileAttr {
374+
fuser::FileAttr {
375+
ino: inode_index as u64,
376+
size: self.i_size as u64,
377+
// todo
378+
blocks: 0,
379+
atime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(self.i_atime as u64),
380+
mtime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(self.i_mtime as u64),
381+
ctime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(self.i_ctime as u64),
382+
crtime: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(self.i_ctime as u64),
383+
kind: match self.i_mode & 0xf000 {
384+
0x8000 => fuser::FileType::RegularFile,
385+
0x4000 => fuser::FileType::Directory,
386+
_ => fuser::FileType::RegularFile,
387+
},
388+
perm: self.i_mode & 0x0fff,
389+
nlink: self.i_links_count as u32,
390+
uid: self.i_uid as u32,
391+
gid: self.i_gid as u32,
392+
// 不熟这些 attr 先不实现
393+
rdev: 0,
394+
flags: 0,
395+
blksize: BLOCK_SIZE as u32,
396+
padding: 0,
397+
}
398+
}
399+
423400
pub fn inode_update_size(&mut self, size: u32) {
424401
self.i_size += size + self.i_size;
425402
self.i_ctime = get_current_time();
@@ -466,14 +443,16 @@ impl DataBlock {
466443
let mut offset = 0;
467444
let mut dir_vec: Vec<DirectoryEntry> = vec![];
468445
while offset + 6 <= BLOCK_SIZE {
469-
dbg!(&self.data[..50]);
470-
let file_size: u16 =
471-
bincode::deserialize(dbg!(&self.data[4 + offset..6 + offset])).unwrap(); //从第四个字节开始解析2个字节为文件的大小
446+
// dbg!(&self.data[..50]);
447+
let file_size= self.data[offset +4];
448+
//dbg!(file_size);
449+
//bincode::deserialize(!(&self.data[4 + offset..6 + offset])).unwrap(); //从第四个字节开始解析2个字节为文件的大小
472450
if file_size == 0 {
473451
break;
474452
}
475453
let dir: DirectoryEntry =
476454
bincode::deserialize(&self.data[offset..offset + file_size as usize]).unwrap();
455+
//dbg!(&dir);
477456
dir_vec.push(dir);
478457
offset += file_size as usize;
479458
}

src/disk.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,21 @@ impl Filesystem for EXT2FS {
6464
fn mkdir(
6565
&mut self,
6666
_req: &fuser::Request<'_>,
67-
_parent: u64,
67+
parent: u64,
6868
name: &std::ffi::OsStr,
6969
_mode: u32,
7070
_umask: u32,
7171
reply: fuser::ReplyEntry,
7272
) {
7373
println!(
7474
"mkdir called for parent={}, name={}",
75-
_parent,
75+
parent,
7676
name.to_string_lossy()
7777
);
7878
let name = name.to_string_lossy().into_owned();
7979
//需要新建一个块
80-
self.block_groups.bg_mkdir(name, _parent as usize);
80+
let attr = self.block_groups.bg_mkdir(name, parent as usize).unwrap();
81+
reply.entry(&Duration::from_secs(1), &attr, 0);
8182
}
8283

8384
fn rmdir(
@@ -112,13 +113,18 @@ impl Filesystem for EXT2FS {
112113

113114
reply.ok();
114115
}
115-
116-
fn getattr(&mut self, _req: &fuser::Request<'_>, _ino: u64, reply: fuser::ReplyAttr) {
117-
println!("getattr called for ino={}", _ino);
118-
let attr = self.block_groups.bg_getattr(_ino as usize);
116+
//tested
117+
fn getattr(&mut self, _req: &fuser::Request<'_>, ino: u64, reply: fuser::ReplyAttr) {
118+
//println!("getattr called for ino={}", ino);
119+
let attr = self.block_groups.bg_getattr(ino as usize);
120+
//dbg!(attr);
119121
reply.attr(&Duration::new(0, 0), &attr);
120122
}
121123

124+
fn opendir(&mut self, _req: &fuser::Request<'_>, _ino: u64, _flags: i32, reply: fuser::ReplyOpen) {
125+
dbg!("opendir");
126+
}
127+
122128
fn lookup(
123129
&mut self,
124130
_req: &fuser::Request<'_>,
@@ -130,15 +136,19 @@ impl Filesystem for EXT2FS {
130136
"lookup called for parent={}, name={}",
131137
parent,
132138
name.to_string_lossy()
133-
);
139+
);
140+
dbg!(name);
134141
let dir_name = name.to_string_lossy().into_owned();
135-
let attr = self.block_groups.bg_lookup(dir_name, parent as usize);
142+
dbg!(&dir_name);
143+
let attr = self.block_groups.bg_lookup(dir_name.to_string(), parent as usize);
144+
dbg!(attr);
136145
if let Some(file) = attr {
137146
dbg!(file);
138147
reply.entry(&Duration::from_secs(1), &file, 0);
139148
}
140149
}
141150

151+
142152
fn open(&mut self, _req: &fuser::Request<'_>, _ino: u64, _flags: i32, reply: fuser::ReplyOpen) {
143153
}
144154

src/file.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::iter::StepBy;
2-
31
use serde::{Deserialize, Serialize};
42
#[derive(Serialize, Deserialize, Debug)]
53
pub enum FileType {
@@ -32,19 +30,26 @@ impl DirectoryEntry {
3230
}
3331
}
3432
pub fn to_bytes(&mut self) -> (Vec<u8>, u16) {
35-
let dir_data = bincode::serialize(&self).unwrap();
33+
let mut dir_data = bincode::serialize(&self).unwrap();
3634
let dir_size = dir_data.len() as u16;
35+
self.entry_update(dir_size);
36+
dir_data[4] = dir_size as u8;
3737
self.file_size = dir_size;
3838
(dir_data, dir_size)
3939
}
4040
pub fn get_type(&self) -> fuser::FileType {
4141
match self.file_type {
4242
FileType::Regular => fuser::FileType::RegularFile,
4343
FileType::Directory => fuser::FileType::Directory,
44-
// fuser 不存在 Unknown 这个 type
44+
// fuser 不存在 Unknown 这s个 type
4545
FileType::Unknow => fuser::FileType::RegularFile,
4646
}
4747
}
48+
49+
pub fn entry_update(&mut self,size : u16){
50+
self.file_size = size;
51+
}
52+
4853
pub fn get_name(&self) -> String {
4954
self.name.clone()
5055
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use fuser::{Filesystem, MountOption};
1010
use log::{info, warn};
1111

1212
fn main() {
13-
let mountpoint = "/test2";
13+
let mountpoint = "/Users/caofengyi/code/os/mount";
1414
let name = "123".to_string();
1515
let pw = "abc".to_string();
1616
let filesystem = EXT2FS::new(name, pw);

src/test.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[cfg(test)]
22
mod test {
3+
use std::{os::unix::process, mem::discriminant};
4+
35
#[test]
46
fn test() {
57
use crate::file::*;
@@ -21,4 +23,30 @@ mod test {
2123
bm.set(1, true);
2224
assert_eq!(bm.get(1), true);
2325
}
26+
27+
28+
#[test]
29+
fn test_bincode() {
30+
use crate::file::*;
31+
use bincode::deserialize;
32+
33+
let mut dir = DirectoryEntry::new("11".to_string(), FileType::Directory, 0, 100);
34+
let (dir_data, dir_size) = dir.to_bytes();
35+
// 打印 dir_data 的十六进制表示
36+
println!("dir_data: {:?},{},{}", dir_data,dir_data.len(),dir_size);
37+
let c = dir_data[4];
38+
println!("c = {}",c);
39+
let t : usize= bincode::deserialize(&dir_data[4..6]).unwrap();
40+
println!("t = {}",t);
41+
// 或者更易于阅读的格式
42+
println!("dir_data (hex): {:?}", dir_data.iter().map(|byte| format!("{:02x}", byte)).collect::<Vec<String>>().join(" "));
43+
44+
assert_eq!(200, t);
45+
}
46+
47+
48+
#[test]
49+
fn test_string(){
50+
let name: &std::ffi::OsStr = "11".to;
51+
}
2452
}

src/user.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
use crate::file::*;
2+
use bincode::deserialize;
13
struct User{
24
username : String,
35
password : String,
46
u_uid : u8,
57
u_gid : u8
68
}
7-
9+
#[debug]
10+
fn main(){
11+
let mut dir = DirectoryEntry::new("11".to_string(), FileType::Directory, 0, 2);
12+
let (dir_data, dir_size) = dir.to_bytes();
13+
dbg!(dir_data);
14+
}

0 commit comments

Comments
 (0)