Box CpioEntry

This commit is contained in:
LoveSy 2023-06-13 17:47:01 +08:00 committed by John Wu
parent 7e8e013832
commit c874391be4
2 changed files with 16 additions and 16 deletions

View File

@ -94,7 +94,7 @@ struct CpioHeader {
} }
pub(crate) struct Cpio { pub(crate) struct Cpio {
pub(crate) entries: BTreeMap<String, CpioEntry>, pub(crate) entries: BTreeMap<String, Box<CpioEntry>>,
} }
pub(crate) struct CpioEntry { pub(crate) struct CpioEntry {
@ -138,14 +138,14 @@ impl Cpio {
continue; continue;
} }
let file_size = x8u::<usize>(&hdr.filesize)?; let file_size = x8u::<usize>(&hdr.filesize)?;
let entry = CpioEntry { let entry = Box::new(CpioEntry {
mode: x8u(&hdr.mode)?, mode: x8u(&hdr.mode)?,
uid: x8u(&hdr.uid)?, uid: x8u(&hdr.uid)?,
gid: x8u(&hdr.gid)?, gid: x8u(&hdr.gid)?,
rdevmajor: x8u(&hdr.rdevmajor)?, rdevmajor: x8u(&hdr.rdevmajor)?,
rdevminor: x8u(&hdr.rdevminor)?, rdevminor: x8u(&hdr.rdevminor)?,
data: data[pos..pos + file_size].to_vec(), data: data[pos..pos + file_size].to_vec(),
}; });
pos += file_size; pos += file_size;
cpio.entries.insert(name, entry); cpio.entries.insert(name, entry);
pos = align_4(pos); pos = align_4(pos);
@ -303,14 +303,14 @@ impl Cpio {
}; };
self.entries.insert( self.entries.insert(
norm_path(path), norm_path(path),
CpioEntry { Box::new(CpioEntry {
mode, mode,
uid: 0, uid: 0,
gid: 0, gid: 0,
rdevmajor, rdevmajor,
rdevminor, rdevminor,
data: content, data: content,
}, }),
); );
eprintln!("Add file [{}] ({:04o})", path, mode); eprintln!("Add file [{}] ({:04o})", path, mode);
Ok(()) Ok(())
@ -319,14 +319,14 @@ impl Cpio {
fn mkdir(&mut self, mode: &mode_t, dir: &str) { fn mkdir(&mut self, mode: &mode_t, dir: &str) {
self.entries.insert( self.entries.insert(
norm_path(dir), norm_path(dir),
CpioEntry { Box::new(CpioEntry {
mode: *mode | S_IFDIR, mode: *mode | S_IFDIR,
uid: 0, uid: 0,
gid: 0, gid: 0,
rdevmajor: 0, rdevmajor: 0,
rdevminor: 0, rdevminor: 0,
data: vec![], data: vec![],
}, }),
); );
eprintln!("Create directory [{}] ({:04o})", dir, mode); eprintln!("Create directory [{}] ({:04o})", dir, mode);
} }
@ -334,14 +334,14 @@ impl Cpio {
fn ln(&mut self, src: &str, dst: &str) { fn ln(&mut self, src: &str, dst: &str) {
self.entries.insert( self.entries.insert(
norm_path(dst), norm_path(dst),
CpioEntry { Box::new(CpioEntry {
mode: S_IFLNK, mode: S_IFLNK,
uid: 0, uid: 0,
gid: 0, gid: 0,
rdevmajor: 0, rdevmajor: 0,
rdevminor: 0, rdevminor: 0,
data: norm_path(src).as_bytes().to_vec(), data: norm_path(src).as_bytes().to_vec(),
}, }),
); );
eprintln!("Create symlink [{}] -> [{}]", dst, src); eprintln!("Create symlink [{}] -> [{}]", dst, src);
} }

View File

@ -90,7 +90,7 @@ impl MagiskCpio for Cpio {
} }
fn restore(&mut self) -> anyhow::Result<()> { fn restore(&mut self) -> anyhow::Result<()> {
let mut backups = HashMap::<String, CpioEntry>::new(); let mut backups = HashMap::<String, Box<CpioEntry>>::new();
let mut rm_list = String::new(); let mut rm_list = String::new();
self.entries self.entries
.drain_filter(|name, _| name.starts_with(".backup/")) .drain_filter(|name, _| name.starts_with(".backup/"))
@ -121,18 +121,18 @@ impl MagiskCpio for Cpio {
} }
fn backup(&mut self, origin: &Utf8CStr) -> anyhow::Result<()> { fn backup(&mut self, origin: &Utf8CStr) -> anyhow::Result<()> {
let mut backups = HashMap::<String, CpioEntry>::new(); let mut backups = HashMap::<String, Box<CpioEntry>>::new();
let mut rm_list = String::new(); let mut rm_list = String::new();
backups.insert( backups.insert(
".backup".to_string(), ".backup".to_string(),
CpioEntry { Box::new(CpioEntry {
mode: S_IFDIR, mode: S_IFDIR,
uid: 0, uid: 0,
gid: 0, gid: 0,
rdevmajor: 0, rdevmajor: 0,
rdevminor: 0, rdevminor: 0,
data: vec![], data: vec![],
}, }),
); );
let mut o = Cpio::load_from_file(origin)?; let mut o = Cpio::load_from_file(origin)?;
o.rm(".backup", true); o.rm(".backup", true);
@ -143,7 +143,7 @@ impl MagiskCpio for Cpio {
loop { loop {
enum Action<'a> { enum Action<'a> {
Backup(String, CpioEntry), Backup(String, Box<CpioEntry>),
Record(&'a String), Record(&'a String),
Noop, Noop,
} }
@ -190,14 +190,14 @@ impl MagiskCpio for Cpio {
if !rm_list.is_empty() { if !rm_list.is_empty() {
backups.insert( backups.insert(
".backup/.rmlist".to_string(), ".backup/.rmlist".to_string(),
CpioEntry { Box::new(CpioEntry {
mode: S_IFREG, mode: S_IFREG,
uid: 0, uid: 0,
gid: 0, gid: 0,
rdevmajor: 0, rdevmajor: 0,
rdevminor: 0, rdevminor: 0,
data: rm_list.as_bytes().to_vec(), data: rm_list.as_bytes().to_vec(),
}, }),
); );
} }
self.entries.extend(backups); self.entries.extend(backups);