4 Commits

5 changed files with 116 additions and 31 deletions

2
index.d.ts vendored
View File

@ -3,6 +3,8 @@
/* auto-generated by NAPI-RS */ /* auto-generated by NAPI-RS */
export declare function hasBackendForPath(path: string): boolean
export declare function listFiles(path: string): Array<string>
export declare function callAltThreadFunc(callback: (...args: any[]) => any): void export declare function callAltThreadFunc(callback: (...args: any[]) => any): void
export declare function generateManifest(dir: string, progress: (...args: any[]) => any, log: (...args: any[]) => any, callback: (...args: any[]) => any): void export declare function generateManifest(dir: string, progress: (...args: any[]) => any, log: (...args: any[]) => any, callback: (...args: any[]) => any): void
export declare function generateRootCa(): Array<string> export declare function generateRootCa(): Array<string>

View File

@ -310,8 +310,10 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`) throw new Error(`Failed to load native binding`)
} }
const { callAltThreadFunc, generateManifest, generateRootCa, generateClientCertificate, verifyClientCertificate, signNonce, verifyNonce } = nativeBinding const { hasBackendForPath, listFiles, callAltThreadFunc, generateManifest, generateRootCa, generateClientCertificate, verifyClientCertificate, signNonce, verifyNonce } = nativeBinding
module.exports.hasBackendForPath = hasBackendForPath
module.exports.listFiles = listFiles
module.exports.callAltThreadFunc = callAltThreadFunc module.exports.callAltThreadFunc = callAltThreadFunc
module.exports.generateManifest = generateManifest module.exports.generateManifest = generateManifest
module.exports.generateRootCa = generateRootCa module.exports.generateRootCa = generateRootCa

View File

@ -1,6 +1,6 @@
{ {
"name": "@drop-oss/droplet", "name": "@drop-oss/droplet",
"version": "0.7.2", "version": "1.1.1",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",
"napi": { "napi": {
@ -42,6 +42,6 @@
}, },
"packageManager": "yarn@4.7.0", "packageManager": "yarn@4.7.0",
"repository": { "repository": {
"url": "https://github.com/Drop-OSS/droplet" "url": "git+https://github.com/Drop-OSS/droplet.git"
} }
} }

View File

@ -1,5 +1,8 @@
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::{ use std::{
fs::{self, metadata}, fs::{self, metadata, File},
io::BufReader,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -17,8 +20,101 @@ fn _list_files(vec: &mut Vec<PathBuf>, path: &Path) {
} }
} }
pub fn list_files(path: &Path) -> Vec<PathBuf> { pub struct VersionFile {
let mut vec = Vec::new(); pub relative_filename: String,
_list_files(&mut vec, path); pub permission: u32,
vec
} }
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
};
results.push(VersionFile {
relative_filename: relative.to_string_lossy().to_string(),
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
}
#[napi]
pub fn list_files(path: String) -> Vec<String> {
let path = Path::new(&path);
let backend = create_backend_for_path(path).unwrap();
let files = backend.list_files(path);
files.into_iter().map(|e| e.relative_filename).collect()
}

View File

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