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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
mod fs;
mod gcs;
use std::ops::Not;
use std::path::{Path, PathBuf};
use bytes::Bytes;
use futures::future::Either;
use futures::{Future, Stream, StreamExt, TryStreamExt};
use fs::FsClient;
use gcs::GcsClient;
use crate::oauth2::token::TokenGenerator;
pub struct ReaderWriter {
inner: ReaderWriterInternal,
}
pub type Source = ReaderWriter;
impl ReaderWriter {
fn new(inner: ReaderWriterInternal) -> Self {
Self { inner }
}
pub async fn gcs(
token_generator: Box<dyn TokenGenerator>,
bucket: &str,
prefix: &str,
) -> RSyncResult<Self> {
let client = GcsClient::new(token_generator, bucket, prefix).await?;
Ok(Self::new(ReaderWriterInternal::Gcs(Box::new(client))))
}
pub fn fs(base_path: &Path) -> Self {
let client = FsClient::new(base_path);
Self::new(ReaderWriterInternal::Fs(Box::new(client)))
}
}
enum ReaderWriterInternal {
Gcs(Box<GcsClient>),
Fs(Box<FsClient>),
}
type Size = u64;
impl ReaderWriterInternal {
async fn list(
&self,
) -> Either<
impl Stream<Item = RSyncResult<RelativePath>> + '_,
impl Stream<Item = RSyncResult<RelativePath>> + '_,
> {
match self {
ReaderWriterInternal::Gcs(client) => Either::Left(client.list().await),
ReaderWriterInternal::Fs(client) => Either::Right(client.list().await),
}
}
async fn read(
&self,
path: &RelativePath,
) -> Either<impl Stream<Item = RSyncResult<Bytes>>, impl Stream<Item = RSyncResult<Bytes>>>
{
match self {
ReaderWriterInternal::Gcs(client) => Either::Left(client.read(path).await),
ReaderWriterInternal::Fs(client) => Either::Right(client.read(path).await),
}
}
async fn get_crc32c(&self, path: &RelativePath) -> RSyncResult<Option<Entry>> {
match self {
ReaderWriterInternal::Gcs(client) => client.get_crc32c(path).await,
ReaderWriterInternal::Fs(client) => client.get_crc32c(path).await,
}
}
async fn write<S>(
&self,
mtime: Option<chrono::DateTime<chrono::Utc>>,
set_fs_mtime: bool,
path: &RelativePath,
stream: S,
) -> RSyncResult<()>
where
S: futures::TryStream<Ok = bytes::Bytes, Error = RSyncError> + Send + Sync + 'static,
{
async {
match self {
ReaderWriterInternal::Gcs(client) => match mtime {
Some(mtime) => client.write_mtime(mtime, path, stream).await,
None => client.write(path, stream).await,
},
ReaderWriterInternal::Fs(client) => match (mtime, set_fs_mtime) {
(Some(mtime), true) => client.write_mtime(mtime, path, stream).await,
_ => client.write(path, stream).await,
},
}
}
.await
}
async fn delete(&self, path: &RelativePath) -> RSyncResult<()> {
match self {
ReaderWriterInternal::Gcs(client) => client.delete(path).await,
ReaderWriterInternal::Fs(client) => client.delete(path).await,
}
}
async fn exists(&self, path: &RelativePath) -> RSyncResult<bool> {
match self {
ReaderWriterInternal::Gcs(client) => client.exists(path).await,
ReaderWriterInternal::Fs(client) => client.exists(path).await,
}
}
async fn size_and_mt(
&self,
path: &RelativePath,
) -> RSyncResult<(Option<chrono::DateTime<chrono::Utc>>, Option<Size>)> {
match self {
ReaderWriterInternal::Gcs(client) => client.size_and_mt(path).await,
ReaderWriterInternal::Fs(client) => client.size_and_mt(path).await,
}
}
}
pub struct RSync {
source: ReaderWriterInternal,
dest: ReaderWriterInternal,
restore_fs_mtime: bool,
}
impl RSync {
pub fn new(source: ReaderWriter, dest: ReaderWriter) -> Self {
Self {
source: source.inner,
dest: dest.inner,
restore_fs_mtime: false,
}
}
pub fn with_restore_fs_mtime(mut self, restore_fs_mtime: bool) -> Self {
self.restore_fs_mtime = restore_fs_mtime;
self
}
async fn write_entry(
&self,
mtime: Option<chrono::DateTime<chrono::Utc>>,
path: &RelativePath,
) -> RSyncResult<()> {
let source = self.source.read(path).await;
self.dest
.write(mtime, self.restore_fs_mtime, path, source)
.await?;
Ok(())
}
async fn sync_entry_crc32c(&self, path: &RelativePath) -> RSyncResult<RSyncStatus> {
Ok(match self.dest.get_crc32c(path).await? {
None => {
self.write_entry(None, path).await?;
RSyncStatus::updated("no dest crc32c", path)
}
Some(crc32c_dest) => {
let crc32c_source = self.source.get_crc32c(path).await?;
if Some(crc32c_dest) == crc32c_source {
RSyncStatus::already_synced("same crc32c", path)
} else {
self.write_entry(None, path).await?;
RSyncStatus::updated("different crc32c", path)
}
}
})
}
async fn sync_entry(&self, path: &RelativePath) -> RSyncResult<RSyncStatus> {
Ok(match self.dest.size_and_mt(path).await? {
(Some(dest_dt), Some(dest_size)) => match self.source.size_and_mt(path).await? {
(Some(source_dt), Some(source_size)) => {
let dest_ts = dest_dt.timestamp();
let source_ts = source_dt.timestamp();
if dest_ts == source_ts && dest_size == source_size {
RSyncStatus::already_synced("same mtime and size", path)
} else {
self.write_entry(Some(source_dt), path).await?;
RSyncStatus::updated("different size or mtime", path)
}
}
_ => self.sync_entry_crc32c(path).await?,
},
(None, None) => {
let (mtime, _) = self.source.size_and_mt(path).await?;
self.write_entry(mtime, path).await?;
RSyncStatus::Created(path.to_owned())
}
_ => self.sync_entry_crc32c(path).await?,
})
}
pub async fn sync(
&self,
) -> impl Stream<Item = RSyncResult<impl Future<Output = RSyncResult<RSyncStatus>> + '_>> + '_
{
self.source
.list()
.await
.map_ok(move |path| async move { self.sync_entry(&path).await })
}
async fn delete_extras(
&self,
) -> impl Stream<Item = RSyncResult<impl Future<Output = RSyncResult<RMirrorStatus>> + '_>> + '_
{
self.dest.list().await.map(move |result| {
result.map(|path| async move {
if self.source.exists(&path).await?.not() {
self.dest.delete(&path).await?;
Ok(RMirrorStatus::Deleted(path))
} else {
Ok(RMirrorStatus::NotDeleted(path))
}
})
})
}
pub async fn mirror(
&self,
) -> impl Stream<Item = RSyncResult<impl Future<Output = RSyncResult<RMirrorStatus>> + '_>> + '_
{
let synced = self
.sync()
.await
.map_ok(|fut| async { fut.await.map(RMirrorStatus::Synced) })
.map_ok(futures::future::Either::Left);
let deleted = self
.delete_extras()
.await
.map_ok(futures::future::Either::Right);
synced.chain(deleted)
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct RelativePath {
path: String,
}
impl std::fmt::Debug for RelativePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.path)
}
}
impl RelativePath {
pub fn new(path: &str) -> RSyncResult<Self> {
let path = path.strip_prefix('/').unwrap_or(path).to_owned();
if path.is_empty() {
Err(RSyncError::EmptyRelativePathError)
} else {
Ok(Self { path })
}
}
}
#[derive(Debug, PartialEq, Clone)]
struct Entry {
path: RelativePath,
crc32c: u32,
}
impl Entry {
pub(self) fn new(path: &RelativePath, crc32c: u32) -> Self {
Self {
path: path.to_owned(),
crc32c,
}
}
}
#[derive(Debug)]
pub enum RSyncError {
MissingFieldsInGcsResponse(String),
StorageError(super::storage::Error),
FsIoError {
path: PathBuf,
message: String,
error: std::io::Error,
},
EmptyRelativePathError,
}
impl RSyncError {
fn fs_io_error<T, U>(message: U, path: T, error: std::io::Error) -> RSyncError
where
T: AsRef<Path>,
U: AsRef<str>,
{
RSyncError::FsIoError {
path: path.as_ref().to_path_buf(),
message: message.as_ref().to_owned(),
error,
}
}
}
impl std::fmt::Display for RSyncError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for RSyncError {}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum RSyncStatus {
Created(RelativePath),
Updated { reason: String, path: RelativePath },
AlreadySynced { reason: String, path: RelativePath },
}
impl RSyncStatus {
fn updated(reason: &str, path: &RelativePath) -> Self {
let reason = reason.to_owned();
let path = path.to_owned();
Self::Updated { reason, path }
}
fn already_synced(reason: &str, path: &RelativePath) -> Self {
let reason = reason.to_owned();
let path = path.to_owned();
Self::AlreadySynced { reason, path }
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum RMirrorStatus {
Synced(RSyncStatus),
Deleted(RelativePath),
NotDeleted(RelativePath),
}
pub type RSyncResult<T> = Result<T, RSyncError>;
#[cfg(test)]
mod tests {
use crate::{gcp::sync::RelativePath, sync::RSyncError};
#[test]
fn test_relative_path() {
fn is_empty_err(x: RSyncError) -> bool {
matches!(x, RSyncError::EmptyRelativePathError)
}
assert!(
is_empty_err(RelativePath::new("").unwrap_err()),
"empty path is not allowed"
);
assert!(is_empty_err(RelativePath::new("/").unwrap_err()));
assert_eq!(
"hello/world",
RelativePath::new("/hello/world").unwrap().path
);
assert_eq!(
"hello/world",
RelativePath::new("hello/world").unwrap().path
);
}
}