Skip to main content

surjection_core/remote/
sync_artifacts.rs

1//! Sync tool artifacts under folder remotes (SPEC 9.6 table): conflict
2//! copies and temp files Dropbox, OneDrive, Google Drive, Syncthing and
3//! Resilio Sync leave behind. Readers accept only names of their exact
4//! grammar anyway; names that match a known pattern are also reported as
5//! `sync_artifact_ignored`. They are never read and never deleted:
6//! deleting a Resilio `.Conflict` file can delete the real file on a peer.
7
8use std::path::PathBuf;
9
10/// One reported artifact.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Artifact {
13    pub path: PathBuf,
14    /// `Dropbox`, `Google Drive`, `Syncthing`, `Resilio Sync`, `OS`.
15    pub tool: &'static str,
16}
17
18/// The sync tool a file or directory name belongs to, if it matches one of
19/// the known conflict or temp patterns.
20pub fn artifact_tool(name: &str) -> Option<&'static str> {
21    // Dropbox.
22    if name.contains(" (conflicted copy")
23        || name.contains(" (Case Conflict")
24        || name.contains(" (Selective Sync Conflict")
25        || matches!(name, ".dropbox" | ".dropbox.attr" | ".dropbox.cache")
26    {
27        return Some("Dropbox");
28    }
29    // Syncthing.
30    if name.contains(".sync-conflict-")
31        || (name.starts_with(".syncthing.") && name.ends_with(".tmp"))
32        || (name.starts_with("~syncthing~") && name.ends_with(".tmp"))
33        || matches!(name, ".stfolder" | ".stversions" | ".stignore")
34    {
35        return Some("Syncthing");
36    }
37    // Resilio Sync.
38    if name.contains(".Conflict")
39        || name.ends_with(".!sync")
40        || name.ends_with(".rsls")
41        || name == ".sync"
42    {
43        return Some("Resilio Sync");
44    }
45    // Google Drive: `name (1).ext`, and its transfer folders.
46    if matches!(name, ".tmp.drivedownload" | ".tmp.driveupload") || numbered_copy(name) {
47        return Some("Google Drive");
48    }
49    if matches!(name, ".DS_Store" | "Thumbs.db" | "desktop.ini") || name.starts_with("~$") {
50        return Some("OS");
51    }
52    None
53}
54
55/// `* (<n>)` before the extension, or at the end.
56fn numbered_copy(name: &str) -> bool {
57    let stem = match name.rfind('.') {
58        Some(dot) if dot > 0 => &name[..dot],
59        _ => name,
60    };
61    let Some(inner) = stem.strip_suffix(')') else {
62        return false;
63    };
64    let Some(open) = inner.rfind(" (") else {
65        return false;
66    };
67    let digits = &inner[open + 2..];
68    !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit())
69}