-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp
More file actions
243 lines (211 loc) · 7.52 KB
/
temp
File metadata and controls
243 lines (211 loc) · 7.52 KB
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
#![no_std]
extern crate alloc;
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::sync::{Arc, Weak};
use core::sync::atomic::{AtomicU64, Ordering};
use los_utils::Mutex;
use crate::fs::mode;
use crate::fs::vfs::error::{VfsError, VfsResult};
use crate::fs::vfs::inode::Inode;
use crate::fs::vfs::ops::{DirEntry, InodeOps, SetAttr};
use crate::fs::vfs::superblock::{StatFs, Superblock};
pub use los_utils::cpio::{CpioArchive, CpioEntry, CpioEntryType};
/// TEAM_205: Initramfs Inode Operations (Shared for all types)
pub struct InitramfsInodeOps;
static INITRAMFS_OPS: InitramfsInodeOps = InitramfsInodeOps;
impl InodeOps for InitramfsInodeOps {
fn lookup(&self, inode: &Inode, name: &str) -> VfsResult<Arc<Inode>> {
let path = inode.private::<String>().ok_or(VfsError::InternalError)?;
let full_path = if path.is_empty() || path == "/" {
name.to_string()
} else {
alloc::format!("{}/{}", path.trim_end_matches('/'), name)
};
let sb = inode.sb.upgrade().ok_or(VfsError::InternalError)?;
let initramfs_sb = sb
.as_any()
.downcast_ref::<InitramfsSuperblock>()
.ok_or(VfsError::InternalError)?;
// Find entry in CPIO
for entry in initramfs_sb.archive.iter() {
if entry.name == full_path {
return Ok(initramfs_sb.make_inode(entry, inode.sb.clone()));
}
}
// Special case: check if it's a directory (CPIO might not have explicit dir entries for all paths)
if initramfs_sb.archive.is_directory(&full_path) {
// Create a virtual directory inode
return Ok(initramfs_sb.make_virtual_dir_inode(&full_path, inode.sb.clone()));
}
Err(VfsError::NotFound)
}
fn read(&self, inode: &Inode, offset: u64, buf: &mut [u8]) -> VfsResult<usize> {
let path = inode.private::<String>().ok_or(VfsError::InternalError)?;
let sb = inode.sb.upgrade().ok_or(VfsError::InternalError)?;
let initramfs_sb = sb
.as_any()
.downcast_ref::<InitramfsSuperblock>()
.ok_or(VfsError::InternalError)?;
let data = initramfs_sb
.archive
.get_file(&path)
.ok_or(VfsError::NotFound)?;
let offset = offset as usize;
if offset >= data.len() {
return Ok(0);
}
let available = data.len() - offset;
let to_read = buf.len().min(available);
buf[..to_read].copy_from_slice(&data[offset..offset + to_read]);
Ok(to_read)
}
fn readdir(&self, inode: &Inode, offset: usize) -> VfsResult<Option<DirEntry>> {
let path = inode.private::<String>().ok_or(VfsError::InternalError)?;
let sb = inode.sb.upgrade().ok_or(VfsError::InternalError)?;
let initramfs_sb = sb
.as_any()
.downcast_ref::<InitramfsSuperblock>()
.ok_or(VfsError::InternalError)?;
// offset 0 = ., 1 = ..
if offset == 0 {
return Ok(Some(DirEntry {
ino: inode.ino,
name: ".".to_string(),
file_type: mode::S_IFDIR,
}));
}
if offset == 1 {
return Ok(Some(DirEntry {
ino: 1, // Assume root is 1
name: "..".to_string(),
file_type: mode::S_IFDIR,
}));
}
// List directory entries from CPIO
let mut idx = 2usize;
for entry in initramfs_sb.archive.list_directory(&path) {
if idx == offset {
return Ok(Some(DirEntry {
ino: entry.ino,
name: entry
.name
.split('/')
.last()
.unwrap_or(entry.name)
.to_string(),
file_type: match entry.entry_type {
CpioEntryType::File => mode::S_IFREG,
CpioEntryType::Directory => mode::S_IFDIR,
CpioEntryType::Symlink => mode::S_IFLNK,
_ => mode::S_IFREG,
},
}));
}
idx += 1;
}
Ok(None)
}
fn readlink(&self, inode: &Inode) -> VfsResult<String> {
let path = inode.private::<String>().ok_or(VfsError::InternalError)?;
let sb = inode.sb.upgrade().ok_or(VfsError::InternalError)?;
let initramfs_sb = sb
.as_any()
.downcast_ref::<InitramfsSuperblock>()
.ok_or(VfsError::InternalError)?;
// In CPIO, symlink target is stored in data
let target_data = initramfs_sb
.archive
.get_file(&path)
.ok_or(VfsError::NotASymlink)?;
core::str::from_utf8(target_data)
.map(|s| s.to_string())
.map_err(|_| VfsError::IoError)
}
fn setattr(&self, _inode: &Inode, _attr: &SetAttr) -> VfsResult<()> {
Err(VfsError::ReadOnlyFs)
}
}
/// TEAM_205: Initramfs Superblock
pub struct InitramfsSuperblock {
pub archive: CpioArchive<'static>,
root_inode: Mutex<Option<Arc<Inode>>>,
next_ino: AtomicU64,
}
impl InitramfsSuperblock {
pub fn new(data: &'static [u8]) -> Self {
Self {
archive: CpioArchive::new(data),
root_inode: Mutex::new(None),
next_ino: AtomicU64::new(1000), // Start above CPIO inos
}
}
pub fn make_inode(&self, entry: CpioEntry<'static>, sb: Weak<dyn Superblock>) -> Arc<Inode> {
let mode = match entry.entry_type {
CpioEntryType::File => mode::S_IFREG | 0o444,
CpioEntryType::Directory => mode::S_IFDIR | 0o555,
CpioEntryType::Symlink => mode::S_IFLNK | 0o777,
_ => mode::S_IFREG | 0o444,
};
Arc::new(Inode::new(
entry.ino,
0,
mode,
&INITRAMFS_OPS,
sb,
Box::new(entry.name.to_string()),
))
}
pub fn make_virtual_dir_inode(&self, path: &str, sb: Weak<dyn Superblock>) -> Arc<Inode> {
Arc::new(Inode::new(
self.next_ino.fetch_add(1, Ordering::SeqCst),
0,
mode::S_IFDIR | 0o555,
&INITRAMFS_OPS,
sb,
Box::new(path.to_string()),
))
}
}
impl Superblock for InitramfsSuperblock {
fn root(&self) -> Arc<Inode> {
self.root_inode.lock().as_ref().unwrap().clone()
}
fn statfs(&self) -> VfsResult<StatFs> {
Ok(StatFs {
f_type: 0x01234567, // Random
f_bsize: 4096,
f_blocks: (self.archive.iter().count() as u64),
f_bfree: 0,
f_bavail: 0,
f_files: (self.archive.iter().count() as u64),
f_ffree: 0,
f_namelen: 255,
..Default::default()
})
}
fn fs_type(&self) -> &'static str {
"initramfs"
}
fn alloc_ino(&self) -> u64 {
self.next_ino.fetch_add(1, Ordering::SeqCst)
}
fn as_any(&self) -> &dyn core::any::Any {
self
}
}
/// TEAM_205: Initialize initramfs as VFS root
pub fn init_vfs(data: &'static [u8]) -> Arc<InitramfsSuperblock> {
let sb = Arc::new(InitramfsSuperblock::new(data));
// Create root inode
let root = Arc::new(Inode::new(
1,
0,
mode::S_IFDIR | 0o555,
&INITRAMFS_OPS,
Arc::downgrade(&(Arc::clone(&sb) as Arc<dyn Superblock>)),
Box::new("".to_string()),
));
*sb.root_inode.lock() = Some(root);
sb
}