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

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()))
}
}