1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![allow(clippy::used_underscore_binding)]
use crate::manifest::StoredArchive;
use crate::repository::{Chunk, ChunkID, ChunkSettings, EncryptedKey};
use async_trait::async_trait;
use chrono::prelude::*;
use semver::Version;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
use std::collections::HashSet;
pub mod common;
pub mod flatfile;
pub mod mem;
pub mod multifile;
#[cfg(feature = "sftp")]
pub mod sftp;
#[cfg(not(tarpaulin_include))]
pub mod object_wrappers;
pub use object_wrappers::{backend_to_object, BackendObject};
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum BackendError {
#[error("I/O Error: {0}")]
IOError(#[from] std::io::Error),
#[error("Data not found")]
DataNotFound,
#[error("Segment Error: {0}")]
SegmentError(String),
#[error("Manifest Error: {0}")]
ManifestError(String),
#[error("Index Error: {0}")]
IndexError(String),
#[error("CBOR Encode/Decode Error")]
MsgPackEncodeError(#[from] serde_cbor::error::Error),
#[error("Failed to lock file")]
FileLockError,
#[error("Cancelled oneshot")]
CancelledOneshotError(#[from] futures::channel::oneshot::Canceled),
#[error("Chunk Unpacking Error: {0}")]
ChunkUnpackError(#[from] asuran_core::repository::chunk::ChunkError),
#[error("Repository has an existing global lock: {0}")]
RepositoryGloballyLocked(String),
#[error("Task Communication Error, likely trying to talk to a closed backend")]
ChannelDroppedSend(#[from] futures::channel::mpsc::SendError),
#[error("Error connecting to backend: {0}")]
ConnectionError(String),
#[error("FlatFile Format Error: {0}")]
FlatFile(#[from] asuran_core::repository::backend::flatfile::FlatFileError),
#[error("Unknown Error: {0}")]
Unknown(String),
}
pub type Result<T> = std::result::Result<T, BackendError>;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct SegmentDescriptor {
pub segment_id: u64,
pub start: u64,
}
#[async_trait]
pub trait Manifest: Send + Sync + std::fmt::Debug + 'static {
type Iterator: Iterator<Item = StoredArchive> + 'static;
async fn last_modification(&mut self) -> Result<DateTime<FixedOffset>>;
async fn chunk_settings(&mut self) -> ChunkSettings;
async fn archive_iterator(&mut self) -> Self::Iterator;
async fn write_chunk_settings(&mut self, settings: ChunkSettings) -> Result<()>;
async fn write_archive(&mut self, archive: StoredArchive) -> Result<()>;
async fn touch(&mut self) -> Result<()>;
async fn seen_versions(&mut self) -> HashSet<(Version, Uuid)>;
}
#[async_trait]
pub trait Index: Send + Sync + std::fmt::Debug + 'static {
async fn lookup_chunk(&mut self, id: ChunkID) -> Option<SegmentDescriptor>;
async fn set_chunk(&mut self, id: ChunkID, location: SegmentDescriptor) -> Result<()>;
async fn known_chunks(&mut self) -> HashSet<ChunkID>;
async fn commit_index(&mut self) -> Result<()>;
async fn count_chunk(&mut self) -> usize;
}
#[async_trait]
pub trait Backend: 'static + Send + Sync + std::fmt::Debug + 'static {
type Manifest: Manifest + 'static;
type Index: Index + 'static;
fn get_index(&self) -> Self::Index;
async fn write_key(&self, key: &EncryptedKey) -> Result<()>;
async fn read_key(&self) -> Result<EncryptedKey>;
fn get_manifest(&self) -> Self::Manifest;
async fn read_chunk(&mut self, location: SegmentDescriptor) -> Result<Chunk>;
async fn write_chunk(&mut self, chunk: Chunk) -> Result<SegmentDescriptor>;
async fn close(&mut self);
fn get_object_handle(&self) -> BackendObject;
}
pub trait BackendClone: Backend + Clone {}
impl<T: ?Sized> BackendClone for T where T: Backend + Clone {}
#[derive(Copy, PartialEq, Eq, Clone, Serialize, Deserialize, Debug)]
pub enum TransactionType {
Insert,
Delete,
}