|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! cpio miniroot support. |
| 6 | +
|
| 7 | +use crate::io; |
| 8 | +use crate::ramdisk; |
| 9 | +use crate::result::{Error, Result}; |
| 10 | +use crate::{print, println}; |
| 11 | +use alloc::boxed::Box; |
| 12 | +use core::slice; |
| 13 | + |
| 14 | +pub(crate) struct FileSystem { |
| 15 | + sd: io::Sd, |
| 16 | +} |
| 17 | + |
| 18 | +impl FileSystem { |
| 19 | + pub(crate) fn try_new(bs: &[u8]) -> Result<FileSystem> { |
| 20 | + if bs.starts_with(b"070707") { |
| 21 | + let sd = io::Sd::from_slice(bs); |
| 22 | + Ok(FileSystem { sd }) |
| 23 | + } else { |
| 24 | + Err(Error::FsInvMagic) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub(crate) struct File { |
| 30 | + data: io::Sd, |
| 31 | +} |
| 32 | + |
| 33 | +impl File { |
| 34 | + fn as_slice(&self) -> &[u8] { |
| 35 | + let ptr = self.data.as_ptr(); |
| 36 | + let len = self.data.len(); |
| 37 | + unsafe { slice::from_raw_parts(ptr, len) } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl ramdisk::File for File { |
| 42 | + fn file_type(&self) -> ramdisk::FileType { |
| 43 | + ramdisk::FileType::Regular |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl io::Read for File { |
| 48 | + fn read(&self, offset: u64, dst: &mut [u8]) -> Result<usize> { |
| 49 | + let s = self.as_slice(); |
| 50 | + s.read(offset, dst) |
| 51 | + } |
| 52 | + |
| 53 | + fn size(&self) -> usize { |
| 54 | + self.data.len() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl ramdisk::FileSystem for FileSystem { |
| 59 | + fn open(&self, path: &str) -> Result<Box<dyn ramdisk::File>> { |
| 60 | + let ptr: *const u8 = self.sd.as_ptr(); |
| 61 | + let len = self.sd.len(); |
| 62 | + let cpio = unsafe { core::slice::from_raw_parts(ptr, len) }; |
| 63 | + let key = path.strip_prefix("/").unwrap_or(path); |
| 64 | + for file in cpio_reader::iter_files(cpio) { |
| 65 | + if file.name() == key { |
| 66 | + let data = io::Sd::from_slice(file.file()); |
| 67 | + return Ok(Box::new(File { data })); |
| 68 | + } |
| 69 | + } |
| 70 | + Err(Error::FsNoFile) |
| 71 | + } |
| 72 | + |
| 73 | + fn list(&self, path: &str) -> Result<()> { |
| 74 | + let ptr: *const u8 = self.sd.as_ptr(); |
| 75 | + let len = self.sd.len(); |
| 76 | + let cpio = unsafe { core::slice::from_raw_parts(ptr, len) }; |
| 77 | + let key = path.strip_prefix('/').unwrap_or(path); |
| 78 | + for file in cpio_reader::iter_files(cpio) { |
| 79 | + if file.name() == key { |
| 80 | + lsfile(path, &file); |
| 81 | + return Ok(()); |
| 82 | + } |
| 83 | + } |
| 84 | + let mut found = false; |
| 85 | + for file in cpio_reader::iter_files(cpio) { |
| 86 | + if file.name().starts_with(key) { |
| 87 | + lsfile(file.name(), &file); |
| 88 | + found = true; |
| 89 | + } |
| 90 | + } |
| 91 | + if found { Ok(()) } else { Err(Error::FsNoFile) } |
| 92 | + } |
| 93 | + |
| 94 | + fn as_str(&self) -> &str { |
| 95 | + "cpio" |
| 96 | + } |
| 97 | + |
| 98 | + fn with_addr(&self, addr: usize) -> *const u8 { |
| 99 | + self.sd.as_ptr().with_addr(addr) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn lsfile(path: &str, file: &cpio_reader::Entry) { |
| 104 | + print!("#{ino:<4} ", ino = file.ino()); |
| 105 | + print_mode(file.mode()); |
| 106 | + println!( |
| 107 | + " {nlink:<2} {uid:<3} {gid:<3} {size:>8} {path}", |
| 108 | + nlink = file.nlink(), |
| 109 | + uid = file.uid(), |
| 110 | + gid = file.gid(), |
| 111 | + size = file.file().len(), |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +fn first_char(mode: cpio_reader::Mode) -> char { |
| 116 | + use cpio_reader::Mode; |
| 117 | + match mode { |
| 118 | + _ if mode.contains(Mode::DIRECTORY) => 'd', |
| 119 | + _ if mode.contains(Mode::CHARACTER_SPECIAL_DEVICE) => 'c', |
| 120 | + _ if mode.contains(Mode::BLOCK_SPECIAL_DEVICE) => 'b', |
| 121 | + _ if mode.contains(Mode::SYMBOLIK_LINK) => 'l', |
| 122 | + _ if mode.contains(Mode::SOCKET) => 's', |
| 123 | + _ if mode.contains(Mode::NAMED_PIPE_FIFO) => 'f', |
| 124 | + _ => '-', |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fn print_mode(mode: cpio_reader::Mode) { |
| 129 | + use cpio_reader::Mode; |
| 130 | + print!("{}", first_char(mode)); |
| 131 | + let alt = |bit, t, f| { |
| 132 | + if mode.contains(bit) { t } else { f } |
| 133 | + }; |
| 134 | + // For some reason, the cpio reader library appears to have |
| 135 | + // the meaning of these bits mirrored with respect to the owner |
| 136 | + // bits. |
| 137 | + print!("{}", alt(Mode::WORLD_READABLE, 'r', '-')); |
| 138 | + print!("{}", alt(Mode::WORLD_WRITABLE, 'w', '-')); |
| 139 | + if !mode.contains(Mode::SUID) { |
| 140 | + print!("{}", alt(Mode::WORLD_EXECUTABLE, 'x', '-')); |
| 141 | + } else { |
| 142 | + print!("{}", alt(Mode::WORLD_EXECUTABLE, 's', 'S')); |
| 143 | + } |
| 144 | + |
| 145 | + print!("{}", alt(Mode::GROUP_READABLE, 'r', '-')); |
| 146 | + print!("{}", alt(Mode::GROUP_WRITABLE, 'w', '-')); |
| 147 | + if !mode.contains(Mode::SGID) { |
| 148 | + print!("{}", alt(Mode::GROUP_EXECUTABLE, 'x', '-')); |
| 149 | + } else { |
| 150 | + print!("{}", alt(Mode::GROUP_EXECUTABLE, 's', 'S')); |
| 151 | + } |
| 152 | + |
| 153 | + print!("{}", alt(Mode::USER_READABLE, 'r', '-')); |
| 154 | + print!("{}", alt(Mode::USER_WRITABLE, 'w', '-')); |
| 155 | + if !mode.contains(Mode::STICKY) { |
| 156 | + print!("{}", alt(Mode::USER_EXECUTABLE, 'x', '-')); |
| 157 | + } else { |
| 158 | + print!("{}", alt(Mode::USER_EXECUTABLE, 't', 'T')); |
| 159 | + } |
| 160 | +} |
0 commit comments