Simplify the unzipping ramdisk file to reduce the heap allocation

This commit is contained in:
LI Qing
2023-07-05 16:22:58 +08:00
committed by Tate, Hongliang Tian
parent 4c83ff9411
commit 4b3cf8daeb
4 changed files with 10 additions and 21 deletions

2
Cargo.lock generated
View File

@ -175,6 +175,7 @@ dependencies = [
name = "cpio-decoder"
version = "0.1.0"
dependencies = [
"core2",
"int-to-c-enum",
]
@ -574,7 +575,6 @@ dependencies = [
"ascii",
"bitflags",
"controlled",
"core2",
"cpio-decoder",
"getrandom",
"int-to-c-enum",

View File

@ -36,7 +36,6 @@ ringbuf = { version = "0.3.2", default-features = false, features = ["alloc"] }
keyable-arc = { path = "../keyable-arc" }
# unzip initramfs
libflate = { git = "https://github.com/jinzhao-dev/libflate", rev = "b781da6", features = ["no_std"] }
core2 = { version = "0.4", default_features = false, features = ["alloc"] }
spin = "0.9.4"
vte = "0.10"

View File

@ -1,18 +1,18 @@
use crate::prelude::*;
use super::fs_resolver::{FsPath, FsResolver};
use super::utils::{InodeMode, InodeType};
use core2::io::Read;
use crate::prelude::*;
use cpio_decoder::{CpioDecoder, FileType};
use libflate::gzip::Decoder as GZipDecoder;
/// Unpack and prepare the fs from the ramdisk CPIO buffer.
pub fn init(ramdisk_buf: &[u8]) -> Result<()> {
pub fn init(gzip_ramdisk_buf: &[u8]) -> Result<()> {
println!("[kernel] unzipping ramdisk.cpio.gz ...");
let unzipped_ramdisk_buf = unzip(ramdisk_buf)?;
println!("[kernel] unzip ramdisk.cpio.gz done");
let decoder = CpioDecoder::new(&unzipped_ramdisk_buf);
let fs = FsResolver::new();
let mut decoder = CpioDecoder::new(
GZipDecoder::new(gzip_ramdisk_buf)
.map_err(|_| Error::with_message(Errno::EINVAL, "invalid gzip buffer"))?,
);
for entry_result in decoder.decode_entries() {
let entry = entry_result?;
@ -59,13 +59,3 @@ pub fn init(ramdisk_buf: &[u8]) -> Result<()> {
Ok(())
}
fn unzip(buf: &[u8]) -> Result<Vec<u8>> {
let mut decoder = GZipDecoder::new(buf)
.map_err(|_| Error::with_message(Errno::EINVAL, "invalid gzip buffer"))?;
let mut unzipped_buf = Vec::new();
decoder
.read_to_end(&mut unzipped_buf)
.map_err(|_| Error::with_message(Errno::EINVAL, "invalid gzip buffer"))?;
Ok(unzipped_buf)
}

View File

@ -139,8 +139,8 @@ impl<R: TRights> Vmo<TRightSet<R>> {
/// Strict the access rights.
#[require(R > R1)]
pub fn restrict<R1: TRights>(self) -> Vmo<R1> {
Vmo(self.0, R1::new())
pub fn restrict<R1: TRights>(self) -> Vmo<TRightSet<R1>> {
Vmo(self.0, TRightSet(R1::new()))
}
}