🩹 use tar binary

This commit is contained in:
JMARyA 2025-03-28 09:25:13 +01:00
parent 656df96792
commit 872ddf54dc
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -1,7 +1,8 @@
use std::{
fs::File,
io::{BufReader, Cursor, Read},
io::{BufReader, Cursor, Read, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
};
use based::get_pg;
@ -525,50 +526,63 @@ pub fn repo_add(db_file: &str, pkg_file: &str) {
}
pub fn read_file_tar(tar: &Path, file_path: &str) -> Option<String> {
let file = File::open(tar).ok()?;
let decoder = Decoder::new(BufReader::new(file)).ok()?;
let mut archive = Archive::new(decoder);
let output = Command::new("tar")
.arg("-xO") // Extract to stdout (-O)
.arg("-f")
.arg(tar)
.arg(file_path)
.output()
.ok()?;
for entry in archive.entries().ok()? {
let mut entry = entry.ok()?;
if entry.path().ok()?.to_str()? == file_path {
let mut file_content = String::new();
entry.read_to_string(&mut file_content).ok()?;
return Some(file_content);
}
if output.status.success() {
Some(String::from_utf8(output.stdout).ok()?.to_string())
} else {
None
}
None
}
pub fn read_file_tar_raw(tar_data: &[u8], file_path: &str) -> Option<String> {
let decoder = Decoder::new(Cursor::new(tar_data)).ok()?;
let mut archive = Archive::new(decoder);
let mut output = Command::new("tar")
.arg("-xO") // Extract to stdout (-O)
.arg("-f")
.arg("-") // Indicate that the file input comes from stdin
.arg(file_path)
.stdin(Stdio::piped()) // Open a pipe to provide the tar data
.spawn()
.ok()?;
for entry in archive.entries().ok()? {
let mut entry = entry.ok()?;
if entry.path().ok()?.to_str()? == file_path {
let mut file_content = String::new();
entry.read_to_string(&mut file_content).ok()?;
return Some(file_content);
}
if let Some(stdin) = output.stdin.as_mut() {
stdin.write_all(tar_data).ok()?;
stdin.flush().ok()?;
}
None
let output = output.wait_with_output().ok()?;
if output.status.success() {
Some(String::from_utf8(output.stdout).ok()?.to_string())
} else {
None
}
}
pub fn list_tar_file(tar: &Path) -> Option<Vec<String>> {
let file = File::open(tar).ok()?;
let decoder = Decoder::new(BufReader::new(file)).ok()?;
let mut archive = Archive::new(decoder);
let mut paths = Vec::new();
let output = Command::new("tar")
.arg("-tf") // List the contents of the tar file
.arg(tar)
.output()
.ok()?;
for entry in archive.entries().ok()? {
let entry = entry.ok()?;
paths.push(entry.path().ok()?.to_string_lossy().into_owned());
if output.status.success() {
let output_str = String::from_utf8(output.stdout).ok()?;
let paths = output_str
.lines()
.map(|line| line.to_string())
.collect::<Vec<String>>();
Some(paths)
} else {
None
}
Some(paths)
}
#[derive(Debug, Clone, FromRow)]