🩹 use tar binary
This commit is contained in:
parent
656df96792
commit
872ddf54dc
1 changed files with 46 additions and 32 deletions
|
@ -1,7 +1,8 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{BufReader, Cursor, Read},
|
io::{BufReader, Cursor, Read, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
use based::get_pg;
|
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> {
|
pub fn read_file_tar(tar: &Path, file_path: &str) -> Option<String> {
|
||||||
let file = File::open(tar).ok()?;
|
let output = Command::new("tar")
|
||||||
let decoder = Decoder::new(BufReader::new(file)).ok()?;
|
.arg("-xO") // Extract to stdout (-O)
|
||||||
let mut archive = Archive::new(decoder);
|
.arg("-f")
|
||||||
|
.arg(tar)
|
||||||
|
.arg(file_path)
|
||||||
|
.output()
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
for entry in archive.entries().ok()? {
|
if output.status.success() {
|
||||||
let mut entry = entry.ok()?;
|
Some(String::from_utf8(output.stdout).ok()?.to_string())
|
||||||
if entry.path().ok()?.to_str()? == file_path {
|
} else {
|
||||||
let mut file_content = String::new();
|
None
|
||||||
entry.read_to_string(&mut file_content).ok()?;
|
|
||||||
return Some(file_content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_file_tar_raw(tar_data: &[u8], file_path: &str) -> Option<String> {
|
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 output = Command::new("tar")
|
||||||
let mut archive = Archive::new(decoder);
|
.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()? {
|
if let Some(stdin) = output.stdin.as_mut() {
|
||||||
let mut entry = entry.ok()?;
|
stdin.write_all(tar_data).ok()?;
|
||||||
if entry.path().ok()?.to_str()? == file_path {
|
stdin.flush().ok()?;
|
||||||
let mut file_content = String::new();
|
|
||||||
entry.read_to_string(&mut file_content).ok()?;
|
|
||||||
return Some(file_content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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>> {
|
pub fn list_tar_file(tar: &Path) -> Option<Vec<String>> {
|
||||||
let file = File::open(tar).ok()?;
|
let output = Command::new("tar")
|
||||||
let decoder = Decoder::new(BufReader::new(file)).ok()?;
|
.arg("-tf") // List the contents of the tar file
|
||||||
let mut archive = Archive::new(decoder);
|
.arg(tar)
|
||||||
let mut paths = Vec::new();
|
.output()
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
for entry in archive.entries().ok()? {
|
if output.status.success() {
|
||||||
let entry = entry.ok()?;
|
let output_str = String::from_utf8(output.stdout).ok()?;
|
||||||
paths.push(entry.path().ok()?.to_string_lossy().into_owned());
|
let paths = output_str
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.to_string())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
Some(paths)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(paths)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, FromRow)]
|
#[derive(Debug, Clone, FromRow)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue