1use crate::{CloudProvider, ObjectPath, StorageConfig, StorageError, StorageFailure};
2use coven_crypto::SecretBytes;
3use coven_crypto::SecretText;
4use coven_foundation::id_source::DeviceId;
5use serde::{Deserialize, Serialize};
6use std::ops::Range;
7
8pub(crate) const CLOUDKIT_PART_SIZE: usize = 8 * 1024 * 1024;
9pub(crate) const DROPBOX_PART_SIZE: usize = 8 * 1024 * 1024;
10pub(crate) const ONEDRIVE_PART_SIZE: usize = 24 * 320 * 1024;
11pub(crate) const S3_MAX_PARTS: usize = 10_000;
13pub(crate) const S3_REQUEST_LIMIT: u64 = 5 * 1024 * 1024 * 1024;
16const S3_PART_UNIT: u64 = 8 * 1024 * 1024;
18
19#[derive(Clone, Serialize, Deserialize)]
43#[serde(try_from = "RecordedUploadSession")]
44pub struct UploadSession {
45 pub(crate) device: DeviceId,
48 pub(crate) location: StorageConfig,
49 pub(crate) path: ObjectPath,
50 pub(crate) total: u64,
51 pub(crate) confirmed: u64,
52 pub(crate) part_size: usize,
53 pub(crate) state: SessionState,
54}
55
56#[derive(Deserialize)]
59#[serde(deny_unknown_fields)]
60struct RecordedUploadSession {
61 device: DeviceId,
62 location: StorageConfig,
63 path: ObjectPath,
64 total: u64,
65 confirmed: u64,
66 part_size: usize,
67 state: SessionState,
68}
69
70impl TryFrom<RecordedUploadSession> for UploadSession {
71 type Error = StorageError;
72 fn try_from(recorded: RecordedUploadSession) -> Result<Self, Self::Error> {
73 let session = Self {
74 device: recorded.device,
75 location: recorded.location,
76 path: recorded.path,
77 total: recorded.total,
78 confirmed: recorded.confirmed,
79 part_size: recorded.part_size,
80 state: recorded.state,
81 };
82 session.validate()?;
83 Ok(session)
84 }
85}
86
87#[derive(Clone, Serialize, Deserialize)]
88#[serde(deny_unknown_fields)]
89pub(crate) enum SessionState {
90 S3 {
91 id: SecretText,
92 token: SecretText,
93 parts: Vec<S3Part>,
94 },
95 S3Restart {
96 id: SecretText,
97 token: SecretText,
98 },
99 S3Planned {
103 token: SecretText,
104 started: PlanStart,
105 },
106 Dropbox {
107 id: SecretText,
108 },
109 OneDrive {
110 temporary: uuid::Uuid,
111 upload: OneDriveUpload,
112 },
113 CloudKit {
114 id: SecretText,
115 },
116 VerifyPublished,
117 Complete,
118 #[cfg(any(test, feature = "test-utils"))]
121 MemoryPlanned {
122 started: PlanStart,
123 },
124 #[cfg(any(test, feature = "test-utils"))]
125 Memory {
126 id: u64,
127 complete: bool,
128 },
129 #[cfg(any(test, feature = "test-utils"))]
133 MemoryParts {
134 id: u64,
135 parts: Vec<u64>,
136 complete: bool,
137 },
138}
139
140#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
144#[serde(from = "bool", into = "bool")]
145pub(crate) enum PlanStart {
146 Unstarted,
149 Starting,
152 MayHaveStarted,
155}
156
157impl From<bool> for PlanStart {
158 fn from(marked: bool) -> Self {
159 if marked {
160 Self::MayHaveStarted
161 } else {
162 Self::Unstarted
163 }
164 }
165}
166
167impl From<PlanStart> for bool {
168 fn from(start: PlanStart) -> Self {
169 start != PlanStart::Unstarted
170 }
171}
172
173#[derive(Clone, Default)]
179pub(crate) struct StartSent(std::sync::Arc<std::sync::atomic::AtomicBool>);
180
181impl StartSent {
182 pub(crate) fn going(&self) {
184 self.0.store(true, std::sync::atomic::Ordering::SeqCst);
185 }
186 fn went(&self) -> bool {
187 self.0.load(std::sync::atomic::Ordering::SeqCst)
188 }
189}
190
191#[derive(Clone, Serialize, Deserialize)]
192#[serde(deny_unknown_fields)]
193pub(crate) struct S3Part {
194 pub(crate) number: i32,
195 pub(crate) size: u64,
196 pub(crate) etag: String,
197}
198
199#[derive(Clone, Serialize, Deserialize)]
202#[serde(deny_unknown_fields)]
203pub(crate) enum OneDriveUpload {
204 Planned {
206 #[serde(skip)]
208 unstarted: bool,
209 },
210 Receiving {
211 url: SecretText,
212 },
213 Uploaded {
214 item: String,
215 },
216 Copying {
217 monitor: SecretText,
218 },
219 Refused,
221 VerifyPublished,
222 Published,
223}
224
225impl S3Part {
226 pub(crate) fn fits(&self, total: u64, part_size: usize) -> bool {
230 u64::try_from(self.number)
231 .ok()
232 .and_then(|number| number.checked_sub(1))
233 .and_then(|index| part_span(total, part_size, index))
234 .is_some_and(|span| span.end - span.start == self.size)
235 && !self.etag.is_empty()
236 }
237}
238
239pub(crate) fn part_span(total: u64, part_size: usize, index: u64) -> Option<Range<u64>> {
242 let start = index.checked_mul(part_size as u64)?;
243 (start < total).then(|| start..start.saturating_add(part_size as u64).min(total))
244}
245
246pub struct ConfirmedPart(pub(crate) Confirmation);
249
250impl std::fmt::Debug for ConfirmedPart {
251 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
252 let index = match &self.0 {
253 Confirmation::S3(part) => i64::from(part.number) - 1,
254 #[cfg(any(test, feature = "test-utils"))]
255 Confirmation::Memory { index } => *index as i64,
256 };
257 f.debug_struct("ConfirmedPart").field("index", &index).finish()
258 }
259}
260
261pub(crate) enum Confirmation {
262 S3(S3Part),
263 #[cfg(any(test, feature = "test-utils"))]
265 Memory {
266 index: u64,
267 },
268}
269
270impl UploadSession {
271 pub(crate) fn mark_start(&mut self) -> bool {
275 let started = match &mut self.state {
276 SessionState::S3Planned { started, .. } => started,
277 #[cfg(any(test, feature = "test-utils"))]
278 SessionState::MemoryPlanned { started } => started,
279 _ => return false,
280 };
281 if *started != PlanStart::Unstarted {
282 return false;
283 }
284 *started = PlanStart::Starting;
285 true
286 }
287 pub(crate) fn start_failed(&mut self, start: &StartSent) {
293 let started = match &mut self.state {
294 SessionState::S3Planned { started, .. } => started,
295 #[cfg(any(test, feature = "test-utils"))]
296 SessionState::MemoryPlanned { started } => started,
297 _ => return,
298 };
299 if !start.went() {
300 *started = PlanStart::Unstarted;
301 }
302 }
303 pub fn needs_initiation(&self) -> bool {
306 matches!(self.state, SessionState::OneDrive { upload: OneDriveUpload::Planned { .. }, .. })
307 }
308 pub fn device(&self) -> DeviceId {
310 self.device
311 }
312 pub fn path(&self) -> &ObjectPath {
314 &self.path
315 }
316 pub fn confirmed_bytes(&self) -> u64 {
318 self.confirmed
319 }
320 pub fn total_bytes(&self) -> u64 {
322 self.total
323 }
324 pub fn part_size(&self) -> usize {
327 self.part_size
328 }
329 pub fn is_complete(&self) -> bool {
331 match self.state {
332 SessionState::Complete => true,
333 #[cfg(any(test, feature = "test-utils"))]
334 SessionState::Memory { complete, .. } | SessionState::MemoryParts { complete, .. } => {
335 complete
336 }
337 _ => false,
338 }
339 }
340 pub fn parts_independent(&self) -> bool {
345 match self.state {
346 SessionState::S3 { .. } => true,
347 #[cfg(any(test, feature = "test-utils"))]
348 SessionState::MemoryParts { complete, .. } => !complete,
349 _ => false,
350 }
351 }
352 pub fn parts_to_send(&self) -> Vec<Range<u64>> {
357 if self.is_complete() || self.confirmed >= self.total {
358 return Vec::new();
359 }
360 if let Some(confirmed) = self.confirmed_parts() {
361 return (0..self.total.div_ceil(self.part_size as u64))
362 .filter(|index| confirmed.binary_search(index).is_err())
363 .filter_map(|index| part_span(self.total, self.part_size, index))
364 .collect();
365 }
366 let next = (self.confirmed / self.part_size as u64 + 1) * self.part_size as u64;
367 std::iter::once(self.confirmed..next.min(self.total)).collect()
368 }
369 fn confirmed_parts(&self) -> Option<Vec<u64>> {
372 match &self.state {
373 SessionState::S3 { parts, .. } => Some(
374 parts
375 .iter()
376 .filter_map(|part| u64::try_from(part.number).ok()?.checked_sub(1))
377 .collect(),
378 ),
379 #[cfg(any(test, feature = "test-utils"))]
380 SessionState::MemoryParts { parts, complete: false, .. } => Some(parts.clone()),
381 _ => None,
382 }
383 }
384 pub fn confirm(&mut self, part: ConfirmedPart) -> Result<(), StorageError> {
388 let (total, part_size) = (self.total, self.part_size);
389 let size = match (&mut self.state, part.0) {
390 (SessionState::S3 { parts, .. }, Confirmation::S3(part)) => {
391 if !part.fits(total, part_size) {
392 return Err(StorageFailure::Protocol
393 .with_source("S3 confirmed a part the session doesn't have"));
394 }
395 let at = parts
396 .binary_search_by_key(&part.number, |held| held.number)
397 .err()
398 .ok_or(StorageFailure::InvalidPart)?;
399 let size = part.size;
400 parts.insert(at, part);
401 size
402 }
403 #[cfg(any(test, feature = "test-utils"))]
404 (
405 SessionState::MemoryParts { parts, complete: false, .. },
406 Confirmation::Memory { index },
407 ) => {
408 let span = part_span(total, part_size, index).ok_or(StorageFailure::Protocol)?;
409 let at = parts.binary_search(&index).err().ok_or(StorageFailure::InvalidPart)?;
410 parts.insert(at, index);
411 span.end - span.start
412 }
413 _ => return Err(StorageFailure::SessionMismatch.into()),
414 };
415 self.confirmed += size;
416 Ok(())
417 }
418 pub fn encode(&self) -> Result<SecretBytes, StorageError> {
420 self.validate()?;
421 crate::secret_json::encode(self)
422 }
423 pub fn decode(bytes: &[u8]) -> Result<Self, StorageError> {
428 let value: RecordedUploadSession = crate::secret_json::decode(bytes)?;
429 value.try_into()
430 }
431 fn validate(&self) -> Result<(), StorageError> {
432 self.location.validate()?;
433 if self.path.is_replaceable() {
436 return Err(StorageFailure::InvalidPath.into());
437 }
438 if self.part_size == 0
439 || self.total == 0
440 || self.confirmed > self.total
441 || (self.is_complete() && self.confirmed != self.total)
442 {
443 return Err(StorageFailure::InvalidPart.into());
444 }
445 let provider = self.location.provider();
446 match &self.state {
447 SessionState::S3 { id, token, parts } if provider == CloudProvider::S3 => {
448 nonempty(id)?;
449 nonempty(token)?;
450 if parts.len() > S3_MAX_PARTS {
451 return Err(StorageFailure::InvalidPart.into());
452 }
453 let mut confirmed = 0u64;
456 let mut previous = 0;
457 for part in parts {
458 if part.number <= previous || !part.fits(self.total, self.part_size) {
459 return Err(StorageFailure::InvalidPart.into());
460 }
461 previous = part.number;
462 confirmed += part.size;
463 }
464 if confirmed != self.confirmed {
465 return Err(StorageFailure::InvalidPart.into());
466 }
467 }
468 SessionState::S3Restart { id, token } if provider == CloudProvider::S3 => {
469 nonempty(id)?;
470 nonempty(token)?;
471 if self.confirmed != 0 {
472 return Err(StorageFailure::InvalidPart.into());
473 }
474 }
475 SessionState::S3Planned { token, .. } if provider == CloudProvider::S3 => {
476 nonempty(token)?;
477 if self.confirmed != 0 {
478 return Err(StorageFailure::InvalidPart.into());
479 }
480 }
481 SessionState::Dropbox { id } if provider == CloudProvider::Dropbox => nonempty(id)?,
482 SessionState::OneDrive { upload, .. } if provider == CloudProvider::OneDrive => {
483 match upload {
484 OneDriveUpload::Planned { .. } => {
485 if self.confirmed != 0 {
486 return Err(StorageFailure::InvalidPart.into());
487 }
488 }
489 OneDriveUpload::Receiving { url } => {
490 transfer_url(url)?;
491 if self.confirmed == self.total {
492 return Err(StorageFailure::InvalidPart.into());
493 }
494 }
495 OneDriveUpload::Uploaded { item } => {
496 if item.is_empty() || self.confirmed != self.total {
497 return Err(StorageFailure::InvalidPart.into());
498 }
499 }
500 OneDriveUpload::Copying { monitor } => {
501 transfer_url(monitor)?;
502 if self.confirmed != self.total {
503 return Err(StorageFailure::InvalidPart.into());
504 }
505 }
506 OneDriveUpload::Published | OneDriveUpload::Refused
507 if self.confirmed != self.total =>
508 {
509 return Err(StorageFailure::InvalidPart.into());
510 }
511 OneDriveUpload::VerifyPublished if self.confirmed == self.total => {
512 return Err(StorageFailure::InvalidPart.into());
513 }
514 OneDriveUpload::VerifyPublished
515 | OneDriveUpload::Published
516 | OneDriveUpload::Refused => {}
517 }
518 }
519 SessionState::CloudKit { id } if provider == CloudProvider::CloudKit => {
520 check_cloudkit_id(id.as_str())?
521 }
522 SessionState::VerifyPublished if provider == CloudProvider::Dropbox => {
523 if self.confirmed == self.total {
524 return Err(StorageFailure::InvalidPart.into());
525 }
526 }
527 SessionState::Complete => {}
528 #[cfg(any(test, feature = "test-utils"))]
529 SessionState::MemoryPlanned { .. } => {
530 return if self.confirmed == 0 {
531 Ok(())
532 } else {
533 Err(StorageFailure::InvalidPart.into())
534 };
535 }
536 #[cfg(any(test, feature = "test-utils"))]
537 SessionState::Memory { id, .. } => {
538 return if *id != 0 { Ok(()) } else { Err(StorageFailure::InvalidPart.into()) };
539 }
540 #[cfg(any(test, feature = "test-utils"))]
541 SessionState::MemoryParts { id, parts, complete } => {
542 let mut confirmed = 0u64;
543 for (at, index) in parts.iter().enumerate() {
544 let span = part_span(self.total, self.part_size, *index)
545 .ok_or(StorageFailure::InvalidPart)?;
546 if at > 0 && parts[at - 1] >= *index {
547 return Err(StorageFailure::InvalidPart.into());
548 }
549 confirmed += span.end - span.start;
550 }
551 let held = if *complete { self.total } else { confirmed };
552 return if *id != 0 && held == self.confirmed {
553 Ok(())
554 } else {
555 Err(StorageFailure::InvalidPart.into())
556 };
557 }
558 _ => return Err(StorageFailure::SessionMismatch.into()),
559 }
560 let valid_size = match provider {
561 CloudProvider::S3 => self.part_size == s3_part_size(self.total)?,
562 CloudProvider::Dropbox => self.part_size == DROPBOX_PART_SIZE,
563 CloudProvider::OneDrive => self.part_size == ONEDRIVE_PART_SIZE,
564 CloudProvider::CloudKit => self.part_size == CLOUDKIT_PART_SIZE,
565 };
566 if !valid_size {
567 return Err(StorageFailure::InvalidPart.into());
568 }
569 Ok(())
570 }
571 pub(crate) fn check(&self, location: &StorageConfig) -> Result<(), StorageError> {
572 self.validate()?;
573 if !self.location.same_location(location) {
574 return Err(StorageFailure::SessionMismatch.into());
575 }
576 Ok(())
577 }
578 pub(crate) fn part_end(&self, offset: u64, len: usize) -> Result<u64, StorageError> {
583 let end = offset.checked_add(len as u64).ok_or(StorageFailure::InvalidPart)?;
584 if self.is_complete() || len == 0 {
585 return Err(StorageFailure::InvalidPart.into());
586 }
587 if let Some(confirmed) = self.confirmed_parts() {
588 let index = offset / self.part_size as u64;
589 let whole = offset.is_multiple_of(self.part_size as u64)
590 && part_span(self.total, self.part_size, index).is_some_and(|span| span.end == end);
591 if !whole || confirmed.binary_search(&index).is_ok() {
592 return Err(StorageFailure::InvalidPart.into());
593 }
594 return Ok(end);
595 }
596 if offset != self.confirmed
597 || len > self.part_size
598 || end > self.total
599 || (end != self.total && end % self.part_size as u64 != 0)
600 {
601 return Err(StorageFailure::InvalidPart.into());
602 }
603 Ok(end)
604 }
605 pub(crate) fn next_part(&self, len: usize) -> Result<Range<u64>, StorageError> {
608 let offset = self
609 .parts_to_send()
610 .first()
611 .map(|part| part.start)
612 .ok_or(StorageFailure::InvalidPart)?;
613 Ok(offset..self.part_end(offset, len)?)
614 }
615}
616
617pub(crate) fn s3_part_size(total: u64) -> Result<usize, StorageError> {
618 let parts = S3_MAX_PARTS as u64;
619 if total == 0 || total > parts * S3_REQUEST_LIMIT {
620 return Err(StorageFailure::InvalidPart.into());
621 }
622 usize::try_from(total.div_ceil(parts).div_ceil(S3_PART_UNIT) * S3_PART_UNIT)
623 .map_err(|error| StorageFailure::InvalidPart.with_source(error))
624}
625pub(crate) fn check_cloudkit_id(value: &str) -> Result<(), StorageError> {
626 coven_foundation::id_source::canonical_uuid(value)
627 .map_err(|error| StorageFailure::SessionMismatch.with_source(error))?;
628 Ok(())
629}
630fn nonempty(id: &SecretText) -> Result<(), StorageError> {
631 if id.as_str().is_empty() {
632 return Err(StorageFailure::SessionMismatch.into());
633 }
634 Ok(())
635}
636fn transfer_url(value: &SecretText) -> Result<(), StorageError> {
637 let url = url::Url::parse(value.as_str())
638 .map_err(|error| StorageFailure::SessionMismatch.with_source(error))?;
639 if !crate::web_url::is_web_url(&url) || url.fragment().is_some() {
640 return Err(StorageFailure::SessionMismatch.into());
641 }
642 Ok(())
643}
644
645#[cfg(test)]
646#[path = "session_tests.rs"]
647mod tests;