mirror of
https://github.com/Drop-OSS/droplet.git
synced 2026-06-22 04:11:40 +10:00
feat: move to backend-based manifest
This commit is contained in:
Vendored
+1
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
/* auto-generated by NAPI-RS */
|
/* auto-generated by NAPI-RS */
|
||||||
|
|
||||||
|
export declare function hasBackendForPath(path: string): boolean
|
||||||
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>
|
||||||
|
|||||||
@@ -310,8 +310,9 @@ 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, callAltThreadFunc, generateManifest, generateRootCa, generateClientCertificate, verifyClientCertificate, signNonce, verifyNonce } = nativeBinding
|
||||||
|
|
||||||
|
module.exports.hasBackendForPath = hasBackendForPath
|
||||||
module.exports.callAltThreadFunc = callAltThreadFunc
|
module.exports.callAltThreadFunc = callAltThreadFunc
|
||||||
module.exports.generateManifest = generateManifest
|
module.exports.generateManifest = generateManifest
|
||||||
module.exports.generateRootCa = generateRootCa
|
module.exports.generateRootCa = generateRootCa
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@drop-oss/droplet",
|
"name": "@drop-oss/droplet",
|
||||||
"version": "0.7.2",
|
"version": "1.0.0",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
"napi": {
|
"napi": {
|
||||||
|
|||||||
+97
-5
@@ -1,5 +1,9 @@
|
|||||||
|
#[cfg(unix)]
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
use std::{
|
use std::{
|
||||||
fs::{self, metadata},
|
fs::{self, metadata, File},
|
||||||
|
io::{BufReader, Read},
|
||||||
|
os::unix::fs::MetadataExt,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -17,8 +21,96 @@ 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 size: u64,
|
||||||
vec
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-23
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user