Compare commits

...

2 commits

Author SHA1 Message Date
f05e112040 [wip] retry chunks
- make `Chunk` lifetime agnostic by using a raw pointer
- add `fn Uploading::rewind`
- add `fn AppState::{next_chunk, is_done}`
2025-06-13 17:03:25 +00:00
5556a658f5 minor refactoring
- var names
- logging
2025-06-13 16:07:25 +00:00
5 changed files with 79 additions and 35 deletions

View file

@ -8,11 +8,13 @@ use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use log::{debug, warn};
use crate::file::Chunk;
use super::{
cachefile::CacheFile,
cli::Cli,
file::{self, FileTrait},
sharry::{self, Client, ClientError},
sharry::{self, Client},
};
pub struct AppState {
@ -104,7 +106,7 @@ impl AppState {
})
}
fn finish_bar(&self) {
fn finish_progressbar(&self) {
let mut slot = self.progress.borrow_mut();
if let Some(bar) = slot.as_ref() {
bar.finish();
@ -112,7 +114,7 @@ impl AppState {
}
}
pub fn upload_chunk(&mut self) -> sharry::Result<Option<()>> {
fn next_chunk(&mut self) -> io::Result<Option<Chunk>> {
let Some(mut uploading) = self.inner.pop_file(&self.http) else {
self.inner
.share_notify(&self.http)
@ -121,20 +123,23 @@ impl AppState {
return Ok(None);
};
debug!("{uploading:?}");
self.get_or_create_progressbar(&uploading);
debug!("{uploading} chunk {}", self.buffer.len());
let chunk = uploading.read(&mut self.buffer);
self.inner.push_file(uploading);
let chunk = chunk?;
let chunk = uploading
.read(&mut self.buffer)
.map_err(ClientError::from)?;
debug!("{chunk:?}");
self.http.file_patch(
chunk.get_patch_uri(),
self.inner.alias_id(),
chunk.get_offset(),
chunk.get_data(),
)?;
Ok(Some(chunk))
}
fn is_done(&mut self) -> bool {
let Some(uploading) = self.inner.pop_file(&self.http) else {
return true;
};
match uploading.check_eof() {
Ok(uploading) => {
@ -146,15 +151,29 @@ impl AppState {
drop(bar);
self.inner.push_file(uploading);
Ok(Some(()))
}
Err(path) => {
debug!("Finished {:?}!", path.display());
self.finish_bar();
Ok(self.inner.has_file().then_some(()))
self.finish_progressbar();
}
}
self.inner.is_empty()
}
pub fn upload_chunk(&mut self) -> sharry::Result<bool> {
let Some(chunk) = self.next_chunk()? else {
return Ok(true);
};
self.http.file_patch(
chunk.get_patch_uri(),
self.inner.alias_id(),
chunk.get_offset(),
unsafe { chunk.get_data() },
)?;
Ok(self.is_done())
}
pub fn file_names(&self) -> Vec<&str> {

View file

@ -99,8 +99,8 @@ impl CacheFile {
self.files.iter().map(FileState::file_name).collect()
}
pub fn has_file(&self) -> bool {
!self.files.is_empty()
pub fn is_empty(&self) -> bool {
self.files.is_empty()
}
pub fn pop_file(&mut self, http: &impl Client) -> Option<file::Uploading> {

View file

@ -1,11 +1,25 @@
pub struct Chunk<'t> {
data: &'t [u8],
patch_uri: &'t str,
use std::fmt;
pub struct Chunk {
data: *const [u8],
patch_uri: String,
offset: u64,
}
impl<'t> Chunk<'t> {
pub fn new(data: &'t [u8], patch_uri: &'t str, offset: u64) -> Self {
impl fmt::Debug for Chunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Chunk")
.field("patch_uri", &self.patch_uri)
.field("offset", &self.offset)
.field("data_len", &self.data.len())
.finish_non_exhaustive()
}
}
impl Chunk {
pub fn new(data: &[u8], patch_uri: String, offset: u64) -> Self {
let data: *const [u8] = data as *const [u8];
Self {
data,
patch_uri,
@ -13,8 +27,8 @@ impl<'t> Chunk<'t> {
}
}
pub fn get_data(&self) -> &[u8] {
self.data
pub unsafe fn get_data(&self) -> &[u8] {
unsafe { &*self.data }
}
pub fn get_length(&self) -> u64 {
@ -27,7 +41,7 @@ impl<'t> Chunk<'t> {
}
pub fn get_patch_uri(&self) -> &str {
self.patch_uri
&self.patch_uri
}
pub fn get_offset(&self) -> u64 {

View file

@ -8,15 +8,16 @@ use serde::{Deserialize, Serialize};
use super::{Chunk, FileTrait};
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
pub struct Uploading {
path: PathBuf,
size: u64,
patch_uri: String,
last_offset: Option<u64>,
offset: u64,
}
impl fmt::Display for Uploading {
impl fmt::Debug for Uploading {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
@ -34,6 +35,7 @@ impl Uploading {
path,
size,
patch_uri,
last_offset: None,
offset: 0,
}
}
@ -42,7 +44,15 @@ impl Uploading {
self.offset
}
pub fn read<'t>(&'t mut self, buf: &'t mut [u8]) -> io::Result<Chunk<'t>> {
pub fn rewind(self) -> Option<Self> {
self.last_offset.map(|last_offset| Self {
last_offset: None,
offset: last_offset,
..self
})
}
pub fn read(&mut self, buf: &mut [u8]) -> io::Result<Chunk> {
let mut f = fs::File::open(&self.path)?;
f.seek(SeekFrom::Start(self.offset))?;
@ -55,7 +65,8 @@ impl Uploading {
));
}
let chunk = Chunk::new(&buf[..read_len], &self.patch_uri, self.offset);
let chunk = Chunk::new(&buf[..read_len], self.patch_uri.clone(), self.offset);
self.last_offset = Some(self.offset);
self.offset += chunk.get_length();
Ok(chunk)

View file

@ -94,7 +94,7 @@ fn main() {
"{} Failed to save {} state: {e}",
style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(),
)
);
});
state
}
@ -116,14 +116,14 @@ fn main() {
loop {
match state.upload_chunk() {
Err(e) => error!("error: {e:?}"), // HACK handle errors better
Ok(None) => {
Ok(true) => {
info!("all uploads done");
state.clear().unwrap_or_else(|e| {
eprintln!(
"{} Failed to remove {} state: {e}",
style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(),
)
);
});
break;
}
@ -135,7 +135,7 @@ fn main() {
"{} Failed to save {} state: {e}",
style("Warning:").red().bold(),
style("ShrUpl").yellow().bold(),
)
);
});
check_ctrlc();
}