1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| unsafe impl Send for Inode {} unsafe impl Sync for Inode {} impl Inode { pub fn new(path: &str, types: InodeTypes) -> Self { info!("Inode new {:?} {}", types, path);
Self(RefCell::new(Ext4File::new(path, types))) }
fn path_deal_with(&self, path: &str) -> String { if path.starts_with('/') { warn!("path_deal_with: {}", path); } let p = path.trim_matches('/'); if p.is_empty() || p == "." { return String::new(); }
if let Some(rest) = p.strip_prefix("./") { return self.path_deal_with(rest); } let rest_p = p.replace("//", "/"); if p != rest_p { return self.path_deal_with(&rest_p); }
let file = self.0.borrow_mut(); let path = file.get_path(); let fpath = String::from(path.to_str().unwrap().trim_end_matches('/')) + "/" + p; info!("dealt with full path: {}", fpath.as_str()); fpath }
#[allow(unused)] pub fn find(&self, name: &str) -> Option<Arc<Inode>> { let file = self.0.borrow_mut(); info!("find name: {} in {}", name, file.get_path().to_str().unwrap()); let (names, inode_type) = file.lwext4_dir_entries().unwrap(); info!("out lwext4_dir_entries"); let mut name_iter = names.iter(); let mut inode_type_iter = inode_type.iter();
info!("into while"); while let Some(iname) = name_iter.next() { let itypes = inode_type_iter.next(); info!("iname: {}", core::str::from_utf8(iname).unwrap()); if core::str::from_utf8(iname).unwrap().trim_end_matches('\0') == name { info!("find {} success", name);
let full_path = String::from(file.get_path().to_str().unwrap().trim_end_matches('/')) + "/" + name; return Some(Arc::new(Inode::new(full_path.as_str(), itypes.unwrap().clone()))); } }
info!("find {} failed", name); None }
pub fn lookup(&self, name: &str) -> Option<Arc<Inode>> { let mut file = self.0.borrow_mut(); let full_path = String::from(file.get_path().to_str().unwrap().trim_end_matches('/')) + "/" + name; if file.check_inode_exist(full_path.as_str(), InodeTypes::EXT4_DE_REG_FILE) { info!("lookup {} success", name); return Some(Arc::new(Inode::new(full_path.as_str(), InodeTypes::EXT4_DE_REG_FILE))); }
info!("lookup {} failed", name); None }
#[allow(unused)] pub fn ls(&self) -> Vec<String> { let file = self.0.borrow_mut();
if file.get_type() != InodeTypes::EXT4_DE_DIR { info!("not a directory"); }
let (name, inode_type) = match file.lwext4_dir_entries() { Ok((name, inode_type)) => (name, inode_type), Err(e) => { panic!("error when ls: {}", e); } };
let mut name_iter = name.iter(); let _inode_type_iter = inode_type.iter();
let mut names = Vec::new(); while let Some(iname) = name_iter.next() { names.push(String::from(core::str::from_utf8(iname).unwrap())); } names }
pub fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize, i32> { debug!("To read_at {}, buf len={}", offset, buf.len()); let mut file = self.0.borrow_mut(); let path = file.get_path(); let path = path.to_str().unwrap(); file.file_open(path, O_RDONLY)?;
file.file_seek(offset as i64, SEEK_SET)?; let r = file.file_read(buf);
let _ = file.file_close(); r }
pub fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize, i32> { debug!("To write_at {}, buf len={}", offset, buf.len()); 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_seek(offset as i64, SEEK_SET)?; let r = file.file_write(buf);
let _ = file.file_close(); r }
pub fn truncate(&self, size: u64) -> Result<usize, i32> { info!("truncate file to size={}", size); let mut file = self.0.borrow_mut(); let path = file.get_path(); let path = path.to_str().unwrap(); file.file_open(path, O_RDWR)?;
let t = file.file_truncate(size);
let _ = file.file_close(); t }
pub fn create(&self, path: &str, ty: InodeTypes) -> Option<Arc<Inode>> { info!("create {:?} on Ext4fs: {}", ty, path); let fpath = self.path_deal_with(path); let fpath = fpath.as_str(); if fpath.is_empty() { info!("given path is empty"); return None; }
let types = ty;
let mut file = self.0.borrow_mut();
let result = if file.check_inode_exist(fpath, types.clone()) { info!("inode already exists"); Ok(0) } else { if types == InodeTypes::EXT4_DE_DIR { file.dir_mk(fpath) } else { file.file_open(fpath, O_WRONLY | O_CREAT | O_TRUNC) .expect("create file failed"); file.file_close() } };
match result { Err(e) => { error!("create inode failed: {}", e); None } Ok(_) => { info!("create inode success"); Some(Arc::new(Inode::new(fpath, types))) } } }
#[allow(unused)] fn remove(&self, path: &str) -> Result<usize, i32> { info!("remove ext4fs: {}", path); let fpath = self.path_deal_with(path); let fpath = fpath.as_str();
assert!(!fpath.is_empty());
let mut file = self.0.borrow_mut(); if file.check_inode_exist(fpath, InodeTypes::EXT4_DE_DIR) { file.dir_rm(fpath) } else { file.file_remove(fpath) } }
#[allow(unused)] fn parent(&self) -> Option<Arc<Inode>> { let file = self.0.borrow_mut(); if file.get_type() == InodeTypes::EXT4_DE_DIR { let path = file.get_path(); let path = path.to_str().unwrap(); info!("Get the parent dir of {}", path); let path = path.trim_end_matches('/').trim_end_matches(|c| c != '/'); if !path.is_empty() { return Some(Arc::new(Self::new(path, InodeTypes::EXT4_DE_DIR))); } } None }
#[allow(unused)] fn rename(&self, src_path: &str, dst_path: &str) -> Result<usize, i32> { info!("rename from {} to {}", src_path, dst_path); let mut file = self.0.borrow_mut(); file.file_rename(src_path, dst_path) } }
impl Drop for Inode { fn drop(&mut self) { let mut file = self.0.borrow_mut(); info!("Drop struct Inode {:?}", file.get_path()); file.file_close().expect("failed to close fd"); drop(file); } }
|