🚑️ more efficient tar reading
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2025-03-15 00:39:27 +01:00
parent 74b8c7571d
commit 788de4da7d
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 52 additions and 46 deletions

View file

@ -51,7 +51,9 @@ async fn launch() -> _ {
..Default::default() ..Default::default()
}) })
.mount_assets() .mount_assets()
.mount("/", routes![ .mount(
"/",
routes![
routes::index_page, routes::index_page,
routes::pkg_route, routes::pkg_route,
routes::push::upload_pkg, routes::push::upload_pkg,
@ -64,7 +66,8 @@ async fn launch() -> _ {
routes::user::end_session, routes::user::end_session,
routes::user::change_password, routes::user::change_password,
routes::user::change_password_post routes::user::change_password_post
]) ],
)
.manage(config) .manage(config)
.manage(shell) .manage(shell)
} }

View file

@ -1,12 +1,13 @@
use std::{ use std::{
fs::File, fs::File,
io::{Cursor, Read}, io::{BufReader, Cursor, Read},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use based::get_pg; use based::get_pg;
use sqlx::FromRow; use sqlx::FromRow;
use tar::Archive; use tar::Archive;
use zstd::Decoder;
use super::{Repository, arch::Architecture}; use super::{Repository, arch::Architecture};
@ -476,46 +477,48 @@ pub fn repo_add(db_file: &str, pkg_file: &str) {
run_command(vec!["repo-add", db_file, pkg_file]); run_command(vec!["repo-add", db_file, pkg_file]);
} }
pub fn read_file_tar(tar: &PathBuf, file_path: &str) -> Option<String> { pub fn read_file_tar(tar: &Path, file_path: &str) -> Option<String> {
let mut file = File::open(tar).ok()?; let file = File::open(tar).ok()?;
let mut buf = Vec::new(); let decoder = Decoder::new(BufReader::new(file)).ok()?;
file.read_to_end(&mut buf).unwrap(); let mut archive = Archive::new(decoder);
read_file_tar_raw(&buf, file_path)
}
pub fn read_file_tar_raw(tar: &[u8], file_path: &str) -> Option<String> { for entry in archive.entries().ok()? {
let uncompressed = zstd::decode_all(Cursor::new(tar)).unwrap(); let mut entry = entry.ok()?;
let content = Cursor::new(uncompressed); if entry.path().ok()?.to_str()? == file_path {
let mut a = Archive::new(content); let mut file_content = String::new();
entry.read_to_string(&mut file_content).ok()?;
for e in a.entries().ok()? { return Some(file_content);
let mut e = e.ok()?;
let path = e.path().unwrap();
let path = path.to_str().unwrap();
if path == file_path {
let mut file_content = Vec::new();
e.read_to_end(&mut file_content).unwrap();
return String::from_utf8(file_content).ok();
} }
} }
None None
} }
pub fn list_tar_file(tar: &PathBuf) -> Option<Vec<String>> { pub fn read_file_tar_raw(tar_data: &[u8], file_path: &str) -> Option<String> {
let mut file = File::open(tar).ok()?; let decoder = Decoder::new(Cursor::new(tar_data)).ok()?;
let mut buf = Vec::new(); let mut archive = Archive::new(decoder);
file.read_to_end(&mut buf).unwrap();
let uncompressed = zstd::decode_all(Cursor::new(buf)).unwrap(); for entry in archive.entries().ok()? {
let content = Cursor::new(uncompressed); let mut entry = entry.ok()?;
let mut a = Archive::new(content); 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);
}
}
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 mut paths = Vec::new();
for e in a.entries().ok()? { for entry in archive.entries().ok()? {
let e = e.ok()?; let entry = entry.ok()?;
let path = e.path().ok()?; paths.push(entry.path().ok()?.to_string_lossy().into_owned());
let path = path.to_str()?;
paths.push(path.to_string());
} }
Some(paths) Some(paths)