🚑️ 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,20 +51,23 @@ async fn launch() -> _ {
..Default::default()
})
.mount_assets()
.mount("/", routes![
routes::index_page,
routes::pkg_route,
routes::push::upload_pkg,
routes::user::login,
routes::user::login_post,
routes::user::account_page,
routes::ui::pkg_ui,
routes::ui::repo_ui,
routes::user::new_api_key,
routes::user::end_session,
routes::user::change_password,
routes::user::change_password_post
])
.mount(
"/",
routes![
routes::index_page,
routes::pkg_route,
routes::push::upload_pkg,
routes::user::login,
routes::user::login_post,
routes::user::account_page,
routes::ui::pkg_ui,
routes::ui::repo_ui,
routes::user::new_api_key,
routes::user::end_session,
routes::user::change_password,
routes::user::change_password_post
],
)
.manage(config)
.manage(shell)
}

View file

@ -1,12 +1,13 @@
use std::{
fs::File,
io::{Cursor, Read},
io::{BufReader, Cursor, Read},
path::{Path, PathBuf},
};
use based::get_pg;
use sqlx::FromRow;
use tar::Archive;
use zstd::Decoder;
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]);
}
pub fn read_file_tar(tar: &PathBuf, file_path: &str) -> Option<String> {
let mut file = File::open(tar).ok()?;
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
read_file_tar_raw(&buf, file_path)
}
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);
pub fn read_file_tar_raw(tar: &[u8], file_path: &str) -> Option<String> {
let uncompressed = zstd::decode_all(Cursor::new(tar)).unwrap();
let content = Cursor::new(uncompressed);
let mut a = Archive::new(content);
for e in a.entries().ok()? {
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();
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);
}
}
None
}
pub fn list_tar_file(tar: &PathBuf) -> Option<Vec<String>> {
let mut file = File::open(tar).ok()?;
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
let uncompressed = zstd::decode_all(Cursor::new(buf)).unwrap();
let content = Cursor::new(uncompressed);
let mut a = Archive::new(content);
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);
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);
}
}
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();
for e in a.entries().ok()? {
let e = e.ok()?;
let path = e.path().ok()?;
let path = path.to_str()?;
paths.push(path.to_string());
for entry in archive.entries().ok()? {
let entry = entry.ok()?;
paths.push(entry.path().ok()?.to_string_lossy().into_owned());
}
Some(paths)