inprogress: handoff to quexeky

This commit is contained in:
DecDuck
2025-05-28 13:53:28 +10:00
parent 16b78bca17
commit 45a26c7156
9 changed files with 117 additions and 60 deletions

View File

@@ -2,12 +2,13 @@
use std::os::unix::fs::PermissionsExt;
use std::{
fs::{self, metadata, File},
io::BufReader,
io::{BufReader, Read},
path::{Path, PathBuf},
task::Poll,
};
const CHUNK_SIZE: usize = 1024 * 1024 * 64;
use napi::{bindgen_prelude::*, tokio_stream::{Stream, StreamExt}};
use tokio_util::{bytes::BytesMut, codec::{BytesCodec, FramedRead}};
fn _list_files(vec: &mut Vec<PathBuf>, path: &Path) {
if metadata(path).unwrap().is_dir() {
@@ -30,7 +31,7 @@ pub struct VersionFile {
pub trait VersionBackend: 'static {
fn list_files(&self, path: &Path) -> Vec<VersionFile>;
fn reader(&self, file: &VersionFile) -> BufReader<File>;
fn reader(&self, file: &VersionFile) -> Option<File>;
}
pub struct PathVersionBackend {
@@ -70,10 +71,10 @@ impl VersionBackend for PathVersionBackend {
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(CHUNK_SIZE, file);
return reader;
fn reader(&self, file: &VersionFile) -> Option<File> {
let file = File::open(self.base_dir.join(file.relative_filename.clone())).ok()?;
return Some(file);
}
}
@@ -85,7 +86,7 @@ impl VersionBackend for ArchiveVersionBackend {
todo!()
}
fn reader(&self, file: &VersionFile) -> BufReader<File> {
fn reader(&self, file: &VersionFile) -> Option<File> {
todo!()
}
}
@@ -120,4 +121,28 @@ pub fn list_files(path: String) -> Vec<String> {
let backend = create_backend_for_path(path).unwrap();
let files = backend.list_files(path);
files.into_iter().map(|e| e.relative_filename).collect()
}
}
#[napi]
pub fn read_file(
path: String,
sub_path: String,
env: &Env,
) -> Option<ReadableStream<'static, Vec<u8>>> {
let path = Path::new(&path);
let backend = create_backend_for_path(path).unwrap();
let version_file = VersionFile {
relative_filename: sub_path,
permission: 0, // Shouldn't matter
};
let reader = backend.reader(&version_file)?;
let reader = tokio::fs::File::from_std(reader);
let stream = FramedRead::new(reader, BytesCodec::new()).map(|e| {
if let Ok(bytes) = e {
Ok(bytes.to_vec())
} else {
Err(napi::Error::from_reason(e.unwrap_err().to_string()))
}
});
Some(ReadableStream::create_with_stream_bytes(env, stream).unwrap())
}

View File

@@ -1,23 +1,22 @@
use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader},
path::Path,
thread,
collections::HashMap, fs::File, io::{BufRead, BufReader}, path::Path, rc::Rc, sync::Arc, thread
};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use napi::{
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
Error, JsFunction,
bindgen_prelude::Function,
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
Env, Error, Result,
};
use serde_json::json;
use uuid::Uuid;
use crate::file_utils::create_backend_for_path;
const CHUNK_SIZE: usize = 1024 * 1024 * 64;
#[derive(serde::Serialize)]
struct ChunkData {
permissions: u32,
@@ -27,14 +26,10 @@ struct ChunkData {
}
#[napi]
pub fn call_alt_thread_func(callback: JsFunction) -> Result<(), Error> {
let tsfn: ThreadsafeFunction<u32, ErrorStrategy::CalleeHandled> = callback
.create_threadsafe_function(0, |ctx| {
ctx.env.create_uint32(ctx.value + 1).map(|v| vec![v])
})?;
let tsfn = tsfn.clone();
pub fn call_alt_thread_func(tsfn: Arc<ThreadsafeFunction<()>>) -> Result<(), String> {
let tsfn_cloned = tsfn.clone();
thread::spawn(move || {
tsfn.call(Ok(0), ThreadsafeFunctionCallMode::NonBlocking);
tsfn_cloned.call(Ok(()), ThreadsafeFunctionCallMode::Blocking);
});
Ok(())
}
@@ -42,24 +37,10 @@ pub fn call_alt_thread_func(callback: JsFunction) -> Result<(), Error> {
#[napi]
pub fn generate_manifest(
dir: String,
progress: JsFunction,
log: JsFunction,
callback: JsFunction,
) -> Result<(), Error> {
let progress_sfn: ThreadsafeFunction<i32, ErrorStrategy::CalleeHandled> = progress
.create_threadsafe_function(0, |ctx| ctx.env.create_int32(ctx.value).map(|v| vec![v]))
.unwrap();
let log_sfn: ThreadsafeFunction<String, ErrorStrategy::CalleeHandled> = log
.create_threadsafe_function(0, |ctx| {
ctx.env.create_string_from_std(ctx.value).map(|v| vec![v])
})
.unwrap();
let callback_sfn: ThreadsafeFunction<String, ErrorStrategy::CalleeHandled> = callback
.create_threadsafe_function(0, |ctx| {
ctx.env.create_string_from_std(ctx.value).map(|v| vec![v])
})
.unwrap();
progress_sfn: ThreadsafeFunction<i32>,
log_sfn: ThreadsafeFunction<String>,
callback_sfn: ThreadsafeFunction<String>,
) -> Result<(), String> {
thread::spawn(move || {
let base_dir = Path::new(&dir);
let backend = create_backend_for_path(base_dir).unwrap();
@@ -72,7 +53,8 @@ pub fn generate_manifest(
let mut i: i32 = 0;
for version_file in files {
let mut reader = backend.reader(&version_file);
let mut raw_reader= backend.reader(&version_file).unwrap();
let mut reader = BufReader::with_capacity(CHUNK_SIZE, raw_reader);
let mut chunk_data = ChunkData {
permissions: version_file.permission,
@@ -101,8 +83,7 @@ pub fn generate_manifest(
let log_str = format!(
"Processed chunk {} for {}",
chunk_index,
&version_file.relative_filename
chunk_index, &version_file.relative_filename
);
log_sfn.call(Ok(log_str), ThreadsafeFunctionCallMode::Blocking);