-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpio.rs
More file actions
160 lines (142 loc) · 4.59 KB
/
Copy pathcpio.rs
File metadata and controls
160 lines (142 loc) · 4.59 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! cpio miniroot support.
use crate::io;
use crate::ramdisk;
use crate::result::{Error, Result};
use crate::{print, println};
use alloc::boxed::Box;
use core::slice;
pub(crate) struct FileSystem {
sd: io::Sd,
}
impl FileSystem {
pub(crate) fn try_new(bs: &[u8]) -> Result<FileSystem> {
if bs.starts_with(b"070707") {
let sd = io::Sd::from_slice(bs);
Ok(FileSystem { sd })
} else {
Err(Error::FsInvMagic)
}
}
}
pub(crate) struct File {
data: io::Sd,
}
impl File {
fn as_slice(&self) -> &[u8] {
let ptr = self.data.as_ptr();
let len = self.data.len();
unsafe { slice::from_raw_parts(ptr, len) }
}
}
impl ramdisk::File for File {
fn file_type(&self) -> ramdisk::FileType {
ramdisk::FileType::Regular
}
}
impl io::Read for File {
fn read(&self, offset: u64, dst: &mut [u8]) -> Result<usize> {
let s = self.as_slice();
s.read(offset, dst)
}
fn size(&self) -> usize {
self.data.len()
}
}
impl ramdisk::FileSystem for FileSystem {
fn open(&self, path: &str) -> Result<Box<dyn ramdisk::File>> {
let ptr: *const u8 = self.sd.as_ptr();
let len = self.sd.len();
let cpio = unsafe { core::slice::from_raw_parts(ptr, len) };
let key = path.strip_prefix("/").unwrap_or(path);
for file in cpio_reader::iter_files(cpio) {
if file.name() == key {
let data = io::Sd::from_slice(file.file());
return Ok(Box::new(File { data }));
}
}
Err(Error::FsNoFile)
}
fn list(&self, path: &str) -> Result<()> {
let ptr: *const u8 = self.sd.as_ptr();
let len = self.sd.len();
let cpio = unsafe { core::slice::from_raw_parts(ptr, len) };
let key = path.strip_prefix('/').unwrap_or(path);
for file in cpio_reader::iter_files(cpio) {
if file.name() == key {
lsfile(path, &file);
return Ok(());
}
}
let mut found = false;
for file in cpio_reader::iter_files(cpio) {
if file.name().starts_with(key) {
lsfile(file.name(), &file);
found = true;
}
}
if found { Ok(()) } else { Err(Error::FsNoFile) }
}
fn as_str(&self) -> &str {
"cpio"
}
fn with_addr(&self, addr: usize) -> *const u8 {
self.sd.as_ptr().with_addr(addr)
}
}
fn lsfile(path: &str, file: &cpio_reader::Entry) {
print!("#{ino:<4} ", ino = file.ino());
print_mode(file.mode());
println!(
" {nlink:<2} {uid:<3} {gid:<3} {size:>8} {path}",
nlink = file.nlink(),
uid = file.uid(),
gid = file.gid(),
size = file.file().len(),
);
}
fn first_char(mode: cpio_reader::Mode) -> char {
use cpio_reader::Mode;
match mode {
_ if mode.contains(Mode::DIRECTORY) => 'd',
_ if mode.contains(Mode::CHARACTER_SPECIAL_DEVICE) => 'c',
_ if mode.contains(Mode::BLOCK_SPECIAL_DEVICE) => 'b',
_ if mode.contains(Mode::SYMBOLIK_LINK) => 'l',
_ if mode.contains(Mode::SOCKET) => 's',
_ if mode.contains(Mode::NAMED_PIPE_FIFO) => 'f',
_ => '-',
}
}
fn print_mode(mode: cpio_reader::Mode) {
use cpio_reader::Mode;
print!("{}", first_char(mode));
let alt = |bit, t, f| {
if mode.contains(bit) { t } else { f }
};
// For some reason, the cpio reader library appears to have
// the meaning of these bits mirrored with respect to the owner
// bits.
print!("{}", alt(Mode::WORLD_READABLE, 'r', '-'));
print!("{}", alt(Mode::WORLD_WRITABLE, 'w', '-'));
if !mode.contains(Mode::SUID) {
print!("{}", alt(Mode::WORLD_EXECUTABLE, 'x', '-'));
} else {
print!("{}", alt(Mode::WORLD_EXECUTABLE, 's', 'S'));
}
print!("{}", alt(Mode::GROUP_READABLE, 'r', '-'));
print!("{}", alt(Mode::GROUP_WRITABLE, 'w', '-'));
if !mode.contains(Mode::SGID) {
print!("{}", alt(Mode::GROUP_EXECUTABLE, 'x', '-'));
} else {
print!("{}", alt(Mode::GROUP_EXECUTABLE, 's', 'S'));
}
print!("{}", alt(Mode::USER_READABLE, 'r', '-'));
print!("{}", alt(Mode::USER_WRITABLE, 'w', '-'));
if !mode.contains(Mode::STICKY) {
print!("{}", alt(Mode::USER_EXECUTABLE, 'x', '-'));
} else {
print!("{}", alt(Mode::USER_EXECUTABLE, 't', 'T'));
}
}