vomit-sync 0.10.1

A library for IMAP to maildir synchronization
Documentation
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use chrono::prelude::*;
use maildir::Maildir;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum StateError {
    #[error("IO error: {0}")]
    IOError(#[from] io::Error),
    #[error("error parsing TOML: {0}")]
    LoadTOMLError(#[from] toml::de::Error),
    #[error("error writing TOML: {0}")]
    SaveTOMLError(#[from] toml::ser::Error),
    #[error("conflicting changes on both sides for UID {0}")]
    ConflictError(u32),
    #[error("error: {0}")]
    Error(&'static str),
}

#[derive(Debug)]
pub(crate) struct LocalAddition {
    id: String,
    flags: String,
}

impl LocalAddition {
    pub fn new(id: String, flags: String) -> Self {
        LocalAddition { id, flags }
    }

    pub fn id(&self) -> &str {
        &self.id
    }
}

type LocalAdditions = Vec<LocalAddition>;

#[derive(Debug)]
pub(crate) struct LocalDeletion {}

type LocalDeletions = BTreeMap<u32, LocalDeletion>;

#[derive(Debug)]
pub(crate) struct LocalModification {
    id: String,
    new_flags: String,
}

impl LocalModification {
    pub fn new_flags(&self) -> &str {
        &self.new_flags
    }
}

type LocalModifications = BTreeMap<u32, LocalModification>;

pub(crate) enum LocalChange {
    Addition(LocalAddition),
    Deletion(LocalDeletion),
    Modification(LocalModification),
}

/// Changes made to the local maildir since last sync
#[derive(Debug)]
pub(crate) struct LocalChanges {
    /// List of file names of local mails added since last sync
    pub added: LocalAdditions,
    /// Set of remote UIDs deleted locally since last sync
    pub deleted: LocalDeletions,
    /// Set of remote UIDs whose local status changed since last sync
    pub modified: LocalModifications,
}

/// Representation of the `last_seen` section of the [SyncState]
///
/// These values can be used (and updated) to keep track of which remote mails
/// have been seen before, even if they were e.g. deleted.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) struct LastSeen {
    /// The highest UID seen in this mailbox
    pub uid: u32,
    /// The UID validity value of this mailbox
    ///
    /// If this value changes, known UIDs may have changed and a full refresh
    /// is needed.
    pub uid_validity: u32,
    /// The highest mod sequence seen for this mailbox
    pub highest_mod_seq: u64,
    pub localtime: Option<i64>,
}

// maildir id, flags
type MailState = (String, String);

/// A cached representation of the local maildir state
///
/// Such a cache must be kept to detect e.g. that mails were deleted or
/// modified locally since the last sync.
#[derive(Debug)]
pub(crate) struct SyncState {
    /// The directory from which the state was loaded
    dir: PathBuf,
    uids: BTreeMap<u32, MailState>,
    /// The `last_seen` section of the state file
    pub last_seen: LastSeen,
    /// Representation of all detected local changes to the maildir
    pub local_changes: LocalChanges,
}

/// A state needed in the root of a maildir hierarchy
///
/// Keeps track of subfolders.
#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct RootState {
    #[serde(skip)]
    dir: PathBuf,
    pub subdirs: BTreeSet<String>,
}

// UID, flags
type MailStateOnDisk = (u32, String);

/// The on-disk representation of [SyncState]
///
/// It differs from the representation used in memory for performance reasons.
#[derive(Debug, Deserialize, Serialize)]
struct SyncStateOnDisk {
    last_seen: LastSeen,
    uids: BTreeMap<String, MailStateOnDisk>,
}

impl SyncStateOnDisk {
    const FNAME: &str = ".vmtsyncstate";

    /// Create a new SyncStateOnDisk structure for when no state was present on-disk
    fn new() -> SyncStateOnDisk {
        SyncStateOnDisk {
            uids: BTreeMap::new(),
            last_seen: LastSeen {
                uid: 1,
                uid_validity: 0,
                highest_mod_seq: 0,
                localtime: Some(0),
            },
        }
    }

    /// Load the state from the given directory
    fn load(dir: &impl AsRef<Path>) -> Result<Self, StateError> {
        let dir: &Path = dir.as_ref();
        let name = Path::new(SyncStateOnDisk::FNAME);
        let path: PathBuf = [dir, name].iter().collect();
        match fs::read_to_string(&path) {
            Ok(s) => Ok(toml::from_str(&s)?),
            Err(e) => {
                if e.kind() != io::ErrorKind::NotFound {
                    eprintln!("error accessing state file {:?}", &path);
                    return Err(StateError::IOError(e));
                }
                // If state file didn't exist, return empty state
                Ok(SyncStateOnDisk::new())
            }
        }
    }

    /// Write the state to the given directory
    fn save(&self, dir: &impl AsRef<Path>) -> Result<(), StateError> {
        let dir: &Path = dir.as_ref();
        let name = Path::new(SyncStateOnDisk::FNAME);
        let path: PathBuf = [dir, name].iter().collect();

        let toml = toml::to_string(self)?;
        fs::write(path, toml)?;
        Ok(())
    }
}

impl RootState {
    const FNAME: &str = ".vmtdirstate";

    /// Load [RootState] from the given directory
    pub(crate) fn load(dir: &impl AsRef<Path>) -> Result<Self, StateError> {
        let dir: &Path = dir.as_ref();
        let name = Path::new(RootState::FNAME);
        let path: PathBuf = [dir, name].iter().collect();
        match fs::read_to_string(&path) {
            Ok(s) => {
                let mut r: Self = toml::from_str(&s)?;
                r.dir = path;
                Ok(r)
            }
            Err(e) => {
                if e.kind() != io::ErrorKind::NotFound {
                    eprintln!("error accessing state file {:?}", &path);
                    return Err(StateError::IOError(e));
                }
                // If state file didn't exist, return empty state
                Ok(RootState {
                    dir: path,
                    subdirs: BTreeSet::new(),
                })
            }
        }
    }

    /// Save the state back to disk
    pub fn save(&mut self) -> Result<(), StateError> {
        let toml = toml::to_string(self)?;
        fs::write(&self.dir, toml)?;
        Ok(())
    }
}

impl SyncState {
    pub fn is_empty(&self) -> bool {
        // Currently, the best indicator is probably this:
        self.last_seen.highest_mod_seq == 0 && self.uids.len() == 0
    }

    pub fn clear(&mut self) {
        self.uids.clear()
    }

    pub fn get(&mut self, uid: &u32) -> Option<&(String, String)> {
        self.uids.get(uid)
    }

    pub fn insert(&mut self, uid: u32, value: (String, String)) -> Option<(String, String)> {
        self.uids.insert(uid, value)
    }

    pub fn remove(&mut self, uid: &u32) -> Option<(String, String)> {
        self.uids.remove(uid)
    }

    /// Returns a list of all UIDs present in the state
    pub fn uids(&self) -> BTreeSet<u32> {
        // TODO references?
        self.uids.keys().cloned().collect()
    }

    pub fn load(dir: &impl AsRef<Path>) -> Result<SyncState, StateError> {
        let mut ssod = SyncStateOnDisk::load(dir)?;

        let changes = SyncState::local_changes(dir.as_ref(), &ssod.uids);
        ssod.last_seen.localtime = Some(Utc::now().timestamp_millis());
        Ok(SyncState {
            dir: PathBuf::from(dir.as_ref()),
            uids: SyncState::invert_state_on_disk(ssod.uids),
            last_seen: ssod.last_seen,
            local_changes: changes,
        })
    }

    pub fn save(&self) -> Result<(), StateError> {
        let ssod = SyncStateOnDisk {
            uids: SyncState::invert_state(self.uids.clone()),
            last_seen: self.last_seen.clone(),
        };
        ssod.save(&self.dir)
    }

    pub fn has_local_changes(&self) -> bool {
        (self.local_changes.added.len()
            + self.local_changes.modified.len()
            + self.local_changes.deleted.len())
            > 0
    }

    /// Discard all local changes in the state and the maildir
    pub fn discard_local_changes(&mut self) -> Result<bool, StateError> {
        let mut needs_refresh = false;
        let maildir = Maildir::from(self.dir.clone());

        while let Some((uid, _)) = self.local_changes.modified.pop_first() {
            let (id, flags) = self.uids.get(&uid).expect("inconsistent state");
            maildir.set_flags(id, flags)?;
            // TODO fall back to removing and setting needs_refresh = true in case of error?
        }

        while let Some((uid, _)) = self.local_changes.deleted.pop_first() {
            // Remove from state so that the sync downloads the item again
            needs_refresh = true;
            self.uids.remove(&uid);
        }

        while let Some(id) = self.local_changes.added.pop() {
            if let Err(e) = maildir.delete(&id.id) {
                eprintln!(
                    "Error deleting {} during local state discard: {}",
                    &id.id, e
                );
            }
        }
        Ok(needs_refresh)
    }

    /// Delete the given UID from the state unless
    ///
    /// Returns an error if there are conflicting local changes in the state.
    pub fn safe_delete(&mut self, uid: &u32) -> Result<Option<String>, StateError> {
        if self.local_changes.modified.contains_key(uid) {
            return Err(StateError::ConflictError(*uid));
        }
        Ok(self.uids.remove(uid).map(|(id, _)| id))
    }

    /// Update the state for the given UID
    ///
    /// Returns an error if there are conflicting local changes in the state.
    pub fn safe_update(&mut self, uid: u32, id: &str, flags: &str) -> Result<(), StateError> {
        if self.local_changes.modified.contains_key(&uid)
            || self.local_changes.deleted.contains_key(&uid)
        {
            return Err(StateError::ConflictError(uid));
        }
        self.uids
            .insert(uid, (String::from(id), String::from(flags)));
        Ok(())
    }

    fn local_changes(dir: &Path, ids: &BTreeMap<String, MailStateOnDisk>) -> LocalChanges {
        // When working on this code: be aware that maildir.find() can be slow.
        // It should not be used in a loop, as it loops over all maildir entries itself.
        // Copying a hash map and doing some lookups is much more efficient.

        let maildir = Maildir::from(PathBuf::from(dir));

        let mut shadow_map = ids.clone();

        let mut added = Vec::new();
        let mut modified = BTreeMap::new();

        let it_cur = maildir.list_cur();
        let it_new = maildir.list_new();
        for mail in it_cur.chain(it_new) {
            let mail = mail.expect("Error during state reconciliation");
            let uid = shadow_map.remove(mail.id());

            match uid {
                Some((uid, flags)) => {
                    // Compare flags
                    if flags != mail.flags() {
                        modified.insert(
                            uid,
                            LocalModification {
                                id: String::from(mail.id()),
                                new_flags: String::from(mail.flags()),
                            },
                        );
                    }
                }
                None => {
                    // TODO check against time stamp?
                    added.push(LocalAddition {
                        id: String::from(mail.id()),
                        flags: String::from(mail.flags()),
                    });
                }
            }
        }

        // All IDs still in the temporary map have been deleted locally
        let deleted: BTreeMap<_, _> = shadow_map
            .into_values()
            .map(|(uid, _flags)| (uid, LocalDeletion {}))
            .collect();

        LocalChanges {
            added,
            deleted,
            modified,
        }
    }

    /// Apply the given local change to the state, consuming the change
    ///
    /// Should be called after a local change has been successfully applied to the remote state.
    pub fn apply(&mut self, uid: u32, change: LocalChange) {
        match change {
            LocalChange::Addition(added) => {
                self.uids.insert(uid, (added.id, added.flags));
            }
            LocalChange::Deletion(_) => {
                self.uids.remove(&uid);
            }
            LocalChange::Modification(modified) => {
                self.uids.insert(uid, (modified.id, modified.new_flags));
            }
        }
    }

    fn invert_state(state: BTreeMap<u32, MailState>) -> BTreeMap<String, MailStateOnDisk> {
        state
            .into_iter()
            .map(|(uid, (id, flags))| (id, (uid, flags)))
            .collect()
    }

    fn invert_state_on_disk(ssod: BTreeMap<String, MailStateOnDisk>) -> BTreeMap<u32, MailState> {
        ssod.into_iter()
            .map(|(id, (uid, flags))| (uid, (id, flags)))
            .collect()
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use std::env;
    use tempfile::{tempdir, TempDir};

    fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
        fs::create_dir_all(&destination)?;
        for entry in fs::read_dir(source)? {
            let entry = entry?;
            let filetype = entry.file_type()?;
            if filetype.is_dir() {
                copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
            } else {
                fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
            }
        }
        Ok(())
    }

    fn setup(dir: &TempDir) {
        let testdir = env::var("CARGO_MANIFEST_DIR").unwrap();
        let maildir = format!("{}/resources/test/maildir", testdir);
        copy_recursively(&maildir, dir.path()).unwrap();
    }

    #[test]
    fn test_invert_state() {
        let state: BTreeMap<_, _> = vec![
            (23, (String::from("foo"), String::from("a"))),
            (42, (String::from("bar"), String::from("x"))),
        ]
        .into_iter()
        .collect();

        let inverted = SyncState::invert_state(state);
        let mut i = inverted.into_iter();
        assert_eq!(
            i.next().unwrap(),
            (String::from("bar"), (42, String::from("x")))
        );
        assert_eq!(
            i.next().unwrap(),
            (String::from("foo"), (23, String::from("a")))
        );
        assert!(i.next().is_none());
    }

    #[test]
    fn test_invert_state_on_disk() {
        let ssod: BTreeMap<_, _> = vec![
            (String::from("foo"), (23, String::from("a"))),
            (String::from("bar"), (42, String::from("x"))),
        ]
        .into_iter()
        .collect();

        let inverted = SyncState::invert_state_on_disk(ssod);
        let mut i = inverted.into_iter();
        assert_eq!(
            i.next().unwrap(),
            (23, (String::from("foo"), String::from("a")))
        );
        assert_eq!(
            i.next().unwrap(),
            (42, (String::from("bar"), String::from("x")))
        );
        assert!(i.next().is_none());
    }

    #[test]
    fn test_load_and_save_immutable() {
        let dir = tempdir().unwrap();
        setup(&dir);

        let state1 = SyncState::load(&dir).unwrap();
        assert!(state1.has_local_changes());
        state1.save().unwrap();

        let state2 = SyncState::load(&dir).unwrap();
        assert!(state2.has_local_changes());
        assert_eq!(state1.uids, state2.uids);
    }
}