feat: move to backend-based manifest

This commit is contained in:
DecDuck
2025-05-26 13:56:24 +10:00
parent 921eb02132
commit 7ede73e87c
5 changed files with 109 additions and 30 deletions

View File

@@ -1,5 +1,9 @@
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::{
fs::{self, metadata},
fs::{self, metadata, File},
io::{BufReader, Read},
os::unix::fs::MetadataExt,
path::{Path, PathBuf},
};
@@ -17,8 +21,96 @@ fn _list_files(vec: &mut Vec<PathBuf>, path: &Path) {
}
}
pub fn list_files(path: &Path) -> Vec<PathBuf> {
let mut vec = Vec::new();
_list_files(&mut vec, path);
vec
pub struct VersionFile {
pub relative_filename: String,
pub size: u64,
pub permission: u32,
}
pub trait VersionBackend: 'static {
fn list_files(&self, path: &Path) -> Vec<VersionFile>;
fn reader(&self, file: &VersionFile) -> BufReader<File>;
}
pub struct PathVersionBackend {
pub base_dir: PathBuf,
}
impl VersionBackend for PathVersionBackend {
fn list_files(&self, path: &Path) -> Vec<VersionFile> {
let mut vec = Vec::new();
_list_files(&mut vec, path);
let mut results = Vec::new();
for pathbuf in vec.iter() {
let file = File::open(pathbuf.clone()).unwrap();
let relative = pathbuf.strip_prefix(path).unwrap();
let metadata = file.try_clone().unwrap().metadata().unwrap();
let permission_object = metadata.permissions();
let permissions = {
let perm: u32;
#[cfg(target_family = "unix")]
{
perm = permission_object.mode();
}
#[cfg(not(target_family = "unix"))]
{
perm = 0
}
perm
};
let size = metadata.size();
results.push(VersionFile {
relative_filename: relative.to_string_lossy().to_string(),
size: size,
permission: permissions,
});
}
results
}
fn reader(&self, file: &VersionFile) -> BufReader<File> {
let file = File::open(self.base_dir.join(file.relative_filename.clone())).unwrap();
let reader = BufReader::with_capacity(4096, file);
return reader;
}
}
// Todo implementation for archives
// Split into a separate impl for each type of archive
pub struct ArchiveVersionBackend {}
impl VersionBackend for ArchiveVersionBackend {
fn list_files(&self, path: &Path) -> Vec<VersionFile> {
todo!()
}
fn reader(&self, file: &VersionFile) -> BufReader<File> {
todo!()
}
}
pub fn create_backend_for_path(path: &Path) -> Option<Box<(dyn VersionBackend)>> {
let is_directory = path.is_dir();
if is_directory {
return Some(Box::new(PathVersionBackend {
base_dir: path.to_path_buf(),
}));
};
/*
Insert checks for whatever backend you like
*/
None
}
#[napi]
pub fn has_backend_for_path(path: String) -> bool {
let path = Path::new(&path);
let has_backend = create_backend_for_path(path).is_some();
has_backend
}

View File

@@ -16,7 +16,7 @@ use napi::{
use serde_json::json;
use uuid::Uuid;
use crate::file_utils::list_files;
use crate::file_utils::create_backend_for_path;
const CHUNK_SIZE: usize = 1024 * 1024 * 64;
@@ -64,7 +64,8 @@ pub fn generate_manifest(
thread::spawn(move || {
let base_dir = Path::new(&dir);
let files = list_files(base_dir);
let backend = create_backend_for_path(base_dir).unwrap();
let files = backend.list_files(base_dir);
// Filepath to chunk data
let mut chunks: HashMap<String, ChunkData> = HashMap::new();
@@ -72,27 +73,11 @@ pub fn generate_manifest(
let total: i32 = files.len() as i32;
let mut i: i32 = 0;
for file_path in files {
let file = File::open(file_path.clone()).unwrap();
let relative = file_path.strip_prefix(base_dir).unwrap();
let permission_object = file.try_clone().unwrap().metadata().unwrap().permissions();
let permissions = {
let perm: u32;
#[cfg(target_family = "unix")]
{
perm = permission_object.mode();
}
#[cfg(not(target_family = "unix"))]
{
perm = 0
}
perm
};
let mut reader = BufReader::with_capacity(CHUNK_SIZE, file);
for version_file in files {
let mut reader = backend.reader(&version_file);
let mut chunk_data = ChunkData {
permissions,
permissions: version_file.permission,
ids: Vec::new(),
checksums: Vec::new(),
lengths: Vec::new(),
@@ -119,7 +104,7 @@ pub fn generate_manifest(
let log_str = format!(
"Processed chunk {} for {}",
chunk_index,
relative.to_str().unwrap()
&version_file.relative_filename
);
log_sfn.call(Ok(log_str), ThreadsafeFunctionCallMode::Blocking);
@@ -127,7 +112,7 @@ pub fn generate_manifest(
chunk_index += 1;
}
chunks.insert(relative.to_str().unwrap().to_string(), chunk_data);
chunks.insert(version_file.relative_filename, chunk_data);
i += 1;
let progress = i * 100 / total;