libtbr/src/firmware/xbuilder.rs

80 lines
2.3 KiB
Rust
Raw Normal View History

2025-09-08 10:45:36 +07:00
// Simplify build
// -
use std::fs::File;
use chrono::{DateTime, Utc};
use rayon::iter::{ParallelBridge, ParallelIterator};
use tar::Archive;
use crate::recipe_functions::common;
pub trait Peekable {
fn peek(&self) -> Result<Vec<String>, Box<dyn std::error::Error>>;
}
/// Is expected to get information about last build
pub struct LastBuildFirmware {
pub path: String,
pub date: DateTime<Utc>,
pub is_full: bool,
/// Should be added later by calling implemented functions
pub entries: Vec<String>,
pub tags: Option<Vec<String>>,
}
impl Peekable for LastBuildFirmware {
fn peek(&self) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let mut peeked = Vec::new();
2025-09-08 11:07:13 +07:00
let file = File::open(self.path.clone()).unwrap();
2025-09-08 11:25:46 +07:00
let decompressed = flate2::read::GzDecoder::new(file);
let mut ar = Archive::new(decompressed);
2025-09-08 10:45:36 +07:00
for file2 in ar.entries().unwrap() {
2025-09-08 11:25:46 +07:00
let f = file2.unwrap();
2025-09-08 12:05:08 +07:00
peeked.push(f.path().unwrap().as_os_str().to_string_lossy().to_string());
2025-09-08 10:45:36 +07:00
}
Ok(peeked)
}
}
pub fn read_last_build_firmwares() -> Result<Vec<LastBuildFirmware>, Box<dyn std::error::Error>> {
// get config
let ucfg = common::get_config();
let firmware_dir = ucfg
.get("FIRMWARE_DIR")
.expect("Unknown firmware directory, check .tbcfg");
// parallel walk to dir
2025-09-08 11:07:13 +07:00
let result = walkdir::WalkDir::new(firmware_dir)
2025-09-08 10:45:36 +07:00
.max_depth(2)
.into_iter()
.par_bridge()
.filter(|w| {
2025-09-08 11:07:13 +07:00
let filename = &w.as_ref().unwrap().clone();
let file_clone = filename.file_name().to_str().unwrap();
file_clone.ends_with(".tar") || file_clone.ends_with(".zip")
2025-09-08 10:45:36 +07:00
})
2025-09-08 11:07:13 +07:00
.map(|entry| {
let dir = entry.unwrap();
let meta = dir.clone().metadata().unwrap();
2025-09-08 10:45:36 +07:00
2025-09-08 11:10:36 +07:00
let filename = dir.clone().path().to_str().unwrap().to_string();
2025-09-08 11:07:13 +07:00
let mod_time: DateTime<Utc> = meta.modified().unwrap().into();
2025-09-08 10:45:36 +07:00
let full_firmware = filename.clone().contains("full");
2025-09-08 11:07:13 +07:00
LastBuildFirmware {
path: filename,
2025-09-08 10:45:36 +07:00
date: mod_time,
is_full: full_firmware,
entries: Vec::new(),
tags: None,
2025-09-08 11:07:13 +07:00
}
})
.collect();
2025-09-08 10:45:36 +07:00
Ok(result)
}