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
//! Types and functions to compute diffs.
use indexmap::IndexMap;
use sos_core::{
commit::Comparison,
events::{patch::FolderDiff, EventLog},
VaultId,
};
use sos_sync::{
MaybeDiff, StorageEventLogs, SyncDiff, SyncStatus, SyncStorage,
};
#[cfg(feature = "files")]
use sos_core::events::patch::FileDiff;
/// Comparison between local and remote status.
#[derive(Debug)]
pub struct SyncComparison {
/// Local sync status.
pub local_status: SyncStatus,
/// Remote sync status.
pub remote_status: SyncStatus,
/// Comparison of the identity event log.
pub identity: Comparison,
/// Comparison of the account event log.
pub account: Comparison,
/// Comparison of the device event log.
pub device: Comparison,
/// Comparison of the files event log.
#[cfg(feature = "files")]
pub files: Option<Comparison>,
/// Comparison for each folder in the account.
pub folders: IndexMap<VaultId, Comparison>,
}
impl SyncComparison {
/// Create a new sync comparison.
pub async fn new<S, E>(
storage: &S,
remote_status: SyncStatus,
) -> std::result::Result<SyncComparison, E>
where
S: SyncStorage,
E: From<<S as StorageEventLogs>::Error> + From<sos_core::Error>,
{
let local_status = storage.sync_status().await?;
let identity = {
let identity = storage.identity_log().await?;
let reader = identity.read().await;
reader.tree().compare(&remote_status.identity.1)?
};
let account = {
let account = storage.account_log().await?;
let reader = account.read().await;
reader.tree().compare(&remote_status.account.1)?
};
let device = {
let device = storage.device_log().await?;
let reader = device.read().await;
reader.tree().compare(&remote_status.device.1)?
};
#[cfg(feature = "files")]
let files = {
let files = storage.file_log().await?;
let reader = files.read().await;
if let Some(files) = &remote_status.files {
if reader.tree().is_empty() {
None
} else {
Some(reader.tree().compare(&files.1)?)
}
} else if reader.tree().is_empty() {
None
} else {
Some(Comparison::Unknown)
}
};
let folders = {
let mut folders = IndexMap::new();
for (id, folder) in &remote_status.folders {
// Folder may exist on remote but not locally
// if we have just deleted a folder
if let Ok(event_log) = storage.folder_log(id).await {
let event_log = event_log.read().await;
folders.insert(*id, event_log.tree().compare(&folder.1)?);
}
}
folders
};
Ok(SyncComparison {
local_status,
remote_status,
identity,
account,
device,
#[cfg(feature = "files")]
files,
folders,
})
}
/// Determine if synchronization is required.
pub fn needs_sync(&self) -> bool {
self.local_status != self.remote_status
}
/// Build a diff from this comparison.
///
/// The diff includes changes on local that are not yet
/// present on the remote or information that will allow
/// a comparison on the remote.
pub async fn diff<S, E>(
&self,
storage: &S,
) -> std::result::Result<SyncDiff, E>
where
S: SyncStorage,
E: std::error::Error
+ std::fmt::Debug
+ From<<S as StorageEventLogs>::Error>
+ From<sos_backend::Error>
+ From<sos_backend::StorageError>
+ From<sos_core::Error>,
{
let mut diff: SyncDiff = Default::default();
match self.identity {
Comparison::Equal => {}
Comparison::Contains(_) => {
// Need to push changes to remote
let log = storage.identity_log().await?;
let reader = log.read().await;
let is_last_commit = Some(&self.remote_status.identity.0)
== reader.tree().last_commit().as_ref();
// Avoid empty patches when commit is already the last
if !is_last_commit {
let identity = reader
.diff_checked(
Some(self.remote_status.identity.0),
self.remote_status.identity.1.clone(),
)
.await?;
diff.identity = Some(MaybeDiff::Diff(identity));
}
}
Comparison::Unknown => {
tracing::info!(
local = ?self.local_status.identity,
remote = ?self.remote_status.identity,
"identity folder divergence"
);
diff.identity = Some(MaybeDiff::Compare(Some(
self.local_status.identity.clone(),
)));
}
}
match self.account {
Comparison::Equal => {}
Comparison::Contains(_) => {
// Need to push changes to remote
let log = storage.account_log().await?;
let reader = log.read().await;
let is_last_commit = Some(&self.remote_status.account.0)
== reader.tree().last_commit().as_ref();
// Avoid empty patches when commit is already the last
if !is_last_commit {
let account = reader
.diff_checked(
Some(self.remote_status.account.0),
self.remote_status.account.1.clone(),
)
.await?;
diff.account = Some(MaybeDiff::Diff(account));
}
}
Comparison::Unknown => {
tracing::info!(
local = ?self.local_status.account,
remote = ?self.remote_status.account,
"account events divergence"
);
diff.account = Some(MaybeDiff::Compare(Some(
self.local_status.account.clone(),
)));
}
}
match self.device {
Comparison::Equal => {}
Comparison::Contains(_) => {
// Need to push changes to remote
let log = storage.device_log().await?;
let reader = log.read().await;
let is_last_commit = Some(&self.remote_status.device.0)
== reader.tree().last_commit().as_ref();
// Avoid empty patches when commit is already the last
if !is_last_commit {
let device = reader
.diff_checked(
Some(self.remote_status.device.0),
self.remote_status.device.1.clone(),
)
.await?;
diff.device = Some(MaybeDiff::Diff(device));
}
}
Comparison::Unknown => {
tracing::info!(
local = ?self.local_status.device,
remote = ?self.remote_status.device,
"device events divergence"
);
// NOTE: this will break the device revoke test spec!
/*
diff.device = Some(MaybeDiff::Compare(Some(
self.local_status.device.clone(),
)));
*/
}
}
#[cfg(feature = "files")]
match (&self.files, &self.remote_status.files) {
(Some(files), Some(remote_files)) => {
match files {
Comparison::Equal => {}
Comparison::Contains(_) => {
// Need to push changes to remote
let log = storage.file_log().await?;
let reader = log.read().await;
let is_last_commit = Some(&remote_files.0)
== reader.tree().last_commit().as_ref();
// Avoid empty patches when commit is already the last
if !is_last_commit {
let files = reader
.diff_checked(
Some(remote_files.0),
remote_files.1.clone(),
)
.await?;
diff.files = Some(MaybeDiff::Diff(files));
}
}
Comparison::Unknown => {
tracing::info!(
local = ?files,
remote = ?remote_files,
"file events divergence"
);
diff.files = Some(MaybeDiff::Compare(
self.local_status.files.clone(),
));
}
}
}
// Remote does not have any files yet so we need
// to send the entire file event log
(Some(Comparison::Unknown), None) => {
// Need to push changes to remote
let log = storage.file_log().await?;
let reader = log.read().await;
if !reader.tree().is_empty() {
let files = FileDiff {
last_commit: None,
patch: reader.diff_events(None).await?,
checkpoint: Default::default(),
};
diff.files = Some(MaybeDiff::Diff(files));
}
}
_ => {}
}
for (id, folder) in &self.folders {
let commit_state = self
.remote_status
.folders
.get(id)
.ok_or(sos_backend::StorageError::FolderNotFound(*id))?;
match folder {
Comparison::Equal => {}
Comparison::Contains(_) => {
// Need to push changes to remote
let log = storage.folder_log(id).await?;
let log = log.read().await;
let folder = log
.diff_checked(
Some(commit_state.0),
commit_state.1.clone(),
)
.await?;
if !folder.patch.is_empty() {
diff.folders.insert(*id, MaybeDiff::Diff(folder));
}
}
Comparison::Unknown => {
tracing::info!(
id = %id,
local = ?self.local_status.folders.get(id),
remote = ?commit_state,
"folder events divergence"
);
diff.folders.insert(
*id,
MaybeDiff::Compare(
self.local_status.folders.get(id).cloned(),
),
);
}
}
}
// Handle events for new folders on local that
// don't exist on remote yet
for (id, _) in &self.local_status.folders {
if self.remote_status.folders.get(id).is_none() {
let log = storage.folder_log(id).await?;
let log = log.read().await;
let first_commit = log.tree().first_commit()?;
let folder = FolderDiff {
last_commit: Some(first_commit.0),
patch: log.diff_events(Some(&first_commit.0)).await?,
checkpoint: first_commit.1,
};
if !folder.patch.is_empty() {
diff.folders.insert(*id, MaybeDiff::Diff(folder));
}
}
}
Ok(diff)
}
}
/// Difference between a local sync status and a remote
/// sync status.
pub async fn diff<S, E>(
storage: &S,
remote_status: SyncStatus,
) -> std::result::Result<(bool, SyncStatus, SyncDiff), E>
where
S: SyncStorage,
E: std::error::Error
+ std::fmt::Debug
+ From<<S as StorageEventLogs>::Error>
+ From<sos_core::Error>
+ From<sos_backend::Error>
+ From<sos_backend::StorageError>
+ Send
+ Sync
+ 'static,
{
let comparison = {
// Compare local status to the remote
SyncComparison::new::<_, E>(storage, remote_status).await?
};
let needs_sync = comparison.needs_sync();
let mut diff = comparison.diff::<_, E>(storage).await?;
let is_server = !storage.is_client_storage();
if is_server {
let storage_folders = storage.folder_details().await?;
diff.folders.retain(|k, _| {
if let Some(folder) = storage_folders.iter().find(|s| s.id() == k)
{
!folder.flags().is_sync_disabled()
} else {
true
}
});
}
Ok((needs_sync, comparison.local_status, diff))
}