use std::{fs, path::Path};
use anyhow::{Context, Result};
use chrono::DateTime;
use crate::lib::{job, logic};
pub trait AsScannedNode {
fn as_scanned_node(&self) -> logic::ScannedNode;
}
impl AsScannedNode for logic::ScannedFileNode {
fn as_scanned_node(&self) -> logic::ScannedNode {
logic::ScannedNode {
name: self.name.clone(),
path: self.path.clone(),
node: logic::NodeIs::File,
tier: self.tier,
id_self: self.id_self, id_path: self.id_path.clone(), dir_has_subdirs: None,
dir_has_files_binary: None,
dir_has_files_text: None,
dir_has_symlinks: None,
file_is_binary: Some(self.is_binary),
file_is_empty: Some(self.is_empty),
}
}
}
impl AsScannedNode for logic::ScannedDirNode {
fn as_scanned_node(&self) -> logic::ScannedNode {
logic::ScannedNode {
name: self.name.clone(),
path: self.path.clone(),
node: logic::NodeIs::Dir,
tier: self.tier,
id_self: self.id_self, id_path: self.id_path.clone(), dir_has_subdirs: Some(self.has_subdirs),
dir_has_files_binary: Some(self.has_files_binary),
dir_has_files_text: Some(self.has_files_text),
dir_has_symlinks: Some(self.has_symlinks),
file_is_binary: None,
file_is_empty: None,
}
}
}
fn piece_gather<L>(j: &job::ValidPreparedJobConfig, p: &logic::Partition<L>) -> Vec<job::ValidResultMainRow>
where
L: logic::MatchLabel,
L::Node: AsScannedNode, {
p.nodes
.iter()
.filter_map(|n| {
let unified_node = n.as_scanned_node();
data_inspect(&j, unified_node).ok() })
.collect()
}
fn data_inspect(j: &job::ValidPreparedJobConfig, scanned_node: logic::ScannedNode) -> Result<job::ValidResultMainRow> {
let clean_rel = scanned_node.path.str.strip_prefix("./").unwrap_or(&scanned_node.path.str);
let absolute_path = j.work.execution_dir.join(clean_rel);
let metadata = fs::metadata(&absolute_path).context("Nie można odczytać metadanych")?;
let dt_modified = DateTime::from(metadata.modified()?);
let name_with_ext = absolute_path.file_name().unwrap_or_default().to_string_lossy().to_string();
let size_real = if metadata.is_dir() { get_dir_size(&absolute_path) } else { metadata.len() };
Ok(job::ValidResultMainRow { dt_modified, name_with_ext, size_real, node: scanned_node })
}
fn get_dir_size(path: &Path) -> u64 {
walkdir::WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter_map(|e| e.metadata().ok())
.map(|m| m.len())
.sum()
}
pub fn data_gather(j: &job::ValidPreparedJobConfig, b: &logic::PartitionScanned) -> job::ValidResultMainTab {
let mut final_rows = Vec::new();
let mut t_max = 0;
let mut n_max = 0;
let mut p_max = 0;
if j.part.md {
final_rows.extend(piece_gather(&j, &b.m_d));
t_max = t_max.max(b.m_d.tier_max);
n_max = n_max.max(b.m_d.name_len_max);
p_max = p_max.max(b.m_d.path_len_max);
}
if j.part.mf {
final_rows.extend(piece_gather(&j, &b.m_f));
t_max = t_max.max(b.m_f.tier_max);
n_max = n_max.max(b.m_f.name_len_max);
p_max = p_max.max(b.m_f.path_len_max);
}
if j.part.xd {
final_rows.extend(piece_gather(&j, &b.x_d));
t_max = t_max.max(b.x_d.tier_max);
n_max = n_max.max(b.x_d.name_len_max);
p_max = p_max.max(b.x_d.path_len_max);
}
if j.part.xf {
final_rows.extend(piece_gather(&j, &b.x_f));
t_max = t_max.max(b.x_f.tier_max);
n_max = n_max.max(b.x_f.name_len_max);
p_max = p_max.max(b.x_f.path_len_max);
}
final_rows.sort_unstable_by(|row_a, row_b| row_a.node.path.str.cmp(&row_b.node.path.str));
job::ValidResultMainTab { rows: final_rows, tier_max: t_max, name_len_max: n_max, path_len_max: p_max }
}
pub fn engine_step1_scanner(j: &job::ValidPreparedJobConfig) -> job::ValidResultMainTab {
let partition_scanned = logic::PartitionScanned::scan(&j);
data_gather(&j, &partition_scanned)
}