No check rm -r

This commit is contained in:
LoveSy 2023-06-12 01:23:02 +08:00 committed by John Wu
parent 6b21091fe2
commit 607e6547a7
2 changed files with 11 additions and 20 deletions

View File

@ -222,32 +222,22 @@ impl Cpio {
Ok(()) Ok(())
} }
pub(crate) fn rm(&mut self, path: &str, recursive: bool) -> anyhow::Result<()> { pub(crate) fn rm(&mut self, path: &str, recursive: bool) {
let path = norm_path(path); let path = norm_path(path);
let entry = self if let Some(_) = self.entries.remove(&path) {
.entries
.get(&path)
.ok_or(anyhow!("no such entry {}", path))?;
if entry.mode & S_IFMT == S_IFDIR && !recursive {
return Err(anyhow!("cannot remove directory without -r"));
}
if entry.mode & S_IFMT != S_IFDIR && recursive {
return Err(anyhow!("cannot remove file with -r"));
}
self.entries.remove(&path);
eprintln!("Removed entry [{}]", path); eprintln!("Removed entry [{}]", path);
}
if recursive { if recursive {
let path = path + "/"; let path = path + "/";
self.entries.retain(|k, _| { self.entries.retain(|k, _| {
if k.starts_with(&path) { if k.starts_with(&path) {
eprintln!("Removed entry [{}]", k); eprintln!("Removed entry [{}]", k);
true
} else {
false false
} else {
true
} }
}) })
} }
Ok(())
} }
fn extract_entry(&self, path: &str, out: &Path) -> anyhow::Result<()> { fn extract_entry(&self, path: &str, out: &Path) -> anyhow::Result<()> {
@ -481,7 +471,7 @@ pub fn cpio_commands(argc: i32, argv: *const *const c_char) -> bool {
} }
} }
CpioCommands::Backup { origin } => cpio.backup(origin)?, CpioCommands::Backup { origin } => cpio.backup(origin)?,
CpioCommands::Rm { path, recursive } => cpio.rm(path, *recursive)?, CpioCommands::Rm { path, recursive } => cpio.rm(path, *recursive),
CpioCommands::Mv { from, to } => cpio.mv(from, to)?, CpioCommands::Mv { from, to } => cpio.mv(from, to)?,
CpioCommands::Extract { path, out } => { CpioCommands::Extract { path, out } => {
cpio.extract(path.as_deref(), out.as_deref())? cpio.extract(path.as_deref(), out.as_deref())?

View File

@ -102,14 +102,14 @@ impl MagiskCpio for Cpio {
backups.insert(name[8..].to_string(), entry); backups.insert(name[8..].to_string(), entry);
} }
}); });
self.rm(".backup", false).ok(); self.rm(".backup", false);
if rm_list.is_empty() && backups.is_empty() { if rm_list.is_empty() && backups.is_empty() {
self.entries.clear(); self.entries.clear();
return Ok(()); return Ok(());
} }
for rm in rm_list.split('\0') { for rm in rm_list.split('\0') {
if !rm.is_empty() { if !rm.is_empty() {
self.rm(rm, false)?; self.rm(rm, false);
} }
} }
self.entries.extend(backups); self.entries.extend(backups);
@ -132,8 +132,8 @@ impl MagiskCpio for Cpio {
}, },
); );
let mut o = Cpio::load_from_file(origin)?; let mut o = Cpio::load_from_file(origin)?;
o.rm(".backup", true).ok(); o.rm(".backup", true);
self.rm(".backup", true).ok(); self.rm(".backup", true);
let mut lhs = o.entries.drain_filter(|_, _| true).peekable(); let mut lhs = o.entries.drain_filter(|_, _| true).peekable();
let mut rhs = self.entries.iter().peekable(); let mut rhs = self.entries.iter().peekable();
@ -178,6 +178,7 @@ impl MagiskCpio for Cpio {
backups.insert(backup, entry); backups.insert(backup, entry);
} }
Action::Record(name) => { Action::Record(name) => {
eprintln!("Record new entry: [{}] -> [.backup/.rmlist]", name);
rm_list.push_str(&format!("{}\0", name)); rm_list.push_str(&format!("{}\0", name));
} }
Action::Noop => {} Action::Noop => {}