From dcee123194c2d91964645d6f9f618af7c736a037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 26 May 2025 20:31:22 +0000 Subject: [PATCH] rough impl for FileChunks --- src/main.rs | 11 +++--- src/sharry/file.rs | 91 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7c931fb..3bf93cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,13 +22,12 @@ fn main() { let share = Share::create(&agent, &alias, share).unwrap(); info!("share: {:?}", share); - let file = File::create( - &agent, - &share, - "/lib/x86_64-linux-gnu/liblldb-14.so.1".into(), - ) - .unwrap(); + let file = File::create(&agent, &share, "/lib/x86_64-linux-gnu/liblldb-14.so.1").unwrap(); info!("file: {:?}", file); + for chunk in file.chunks(10_485_760) { + println!("chunk len: {}", chunk.len()); + } + share.notify(&agent).unwrap(); } diff --git a/src/sharry/file.rs b/src/sharry/file.rs index 1fdda4b..baceaef 100644 --- a/src/sharry/file.rs +++ b/src/sharry/file.rs @@ -1,11 +1,13 @@ use std::{ ffi::OsStr, fs, - io::{self, Read}, + io::{self, Read, Seek}, + os::unix::fs::FileExt, path::{Path, PathBuf}, + str::FromStr, }; -use log::debug; +use log::{debug, error}; use ureq::http::StatusCode; use super::{ @@ -24,17 +26,18 @@ impl<'t> File<'t> { pub fn create( http: &ureq::Agent, share: &'t Share, - file_path: PathBuf, + file_path: impl Into, ) -> Result { - let endpoint = share - .alias - .get_endpoint(format!("alias/upload/{}/files/tus", share.id)); - + let file_path: PathBuf = file_path.into(); let filename = file_path .file_name() .and_then(OsStr::to_str) .unwrap_or("file.bin"); + let endpoint = share + .alias + .get_endpoint(format!("alias/upload/{}/files/tus", share.id)); + let res = http .post(endpoint) .sharry_header(share.alias) @@ -55,14 +58,6 @@ impl<'t> File<'t> { debug!("location: {}", location); - // let start = 10; - // let count = 10; - - // let mut f = File::open(path)?; - // f.seek(SeekFrom::Start(0)); - // let mut buf = vec![0; count]; - // f.read_exact(&mut buf)?; - Ok(Self { alias: share.alias, file_path, @@ -70,23 +65,71 @@ impl<'t> File<'t> { }) } - pub fn upload(&self, chunk_size: usize) -> UploadChunk { - UploadChunk::new(&self.file_path, chunk_size).unwrap() + pub fn chunks(&self, chunk_size: usize) -> FileChunks { + FileChunks::new(&self.file_path, chunk_size) } + + // pub fn upload(&mut self) -> io::Result<()> { + // let mut f = fs::File::open(&self.path)?; + // f.seek(io::SeekFrom::Start(0)); + + // let patch_size = f.read(&mut self.buffer)?; + // let patch_data = &self.buffer[0..patch_size]; + + // Ok(()) + // } } -pub struct UploadChunk { +pub struct FileChunks<'t> { num: u64, - f: fs::File, + path: &'t PathBuf, buffer: Vec, + data_len: Option, } -impl UploadChunk { - fn new(path: impl AsRef, chunk_size: usize) -> io::Result { - Ok(Self { +impl<'t> FileChunks<'t> { + fn new(path: &'t PathBuf, chunk_size: usize) -> Self { + Self { num: 0, - f: fs::File::open(path)?, + path, buffer: vec![0; chunk_size], - }) + data_len: None, + } + } + + fn get_offset(&self) -> Option { + let buffer_len: u64 = self + .buffer + .len() + .try_into() + .inspect_err(|e| error!("Error converting length: {}", e)) + .ok()?; + + Some(self.num * buffer_len) + } +} + +impl<'t> Iterator for FileChunks<'t> { + type Item = &'t [u8]; + + fn next(&mut self) -> Option { + let mut f = fs::File::open(&self.path) + .inspect_err(|e| error!("Error opening file: {}", e)) + .ok()?; + + f.seek(io::SeekFrom::Start(self.get_offset()?)).ok()?; + + self.data_len = { + let read_len = f + .read(&mut self.buffer) + .inspect_err(|e| error!("Error reading file: {}", e)) + .ok()?; + + Some(read_len).filter(|l| *l > 0) + }; + + self.num += 1; + + Some(unsafe { std::mem::transmute(&self.buffer[0..self.data_len?]) }) } }