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
//! Ditto Root Directory Wrapper
//!
//! A utility module for creating, managing, and removing the Ditto directory
//! structures on the local file system

use_prelude!();
use std::{
    env, fs,
    path::{Path, PathBuf},
};

use crate::error::{DittoError, ErrorKind};

/// Interface provided by the Filesystem directory into which Ditto will store
/// its local data
pub trait DittoRoot: Send + Sync {
    /// Return the root path of the Ditto directory
    fn root_path(&self) -> &Path;

    /// Return the path of the data in the Ditto directory
    #[deprecated(note = "Use root_path instead")]
    fn data_path(&self) -> &Path {
        self.root_path()
    }

    /// Emits the Ditto root directory path as a null-terminated UTF-8 C-string
    fn root_dir_to_c_str(&self) -> Result<char_p::Box, DittoError> {
        Ok(char_p::new(self.root_path_as_str()?))
    }

    /// Emits the Ditto data directory path as a null-terminated UTF-8 C-string
    #[deprecated(note = "Use root_dir_to_c_str instead")]
    fn data_dir_to_c_str(&self) -> Result<char_p::Box, DittoError> {
        self.root_dir_to_c_str()
    }

    /// Return true if the Root path exists
    fn exists(&self) -> bool;

    /// Return true if the provided Root path is a valid path
    fn is_valid(&self) -> Result<(), DittoError>;

    /// Return the Root path as a `&str`
    fn root_path_as_str(&self) -> Result<&str, DittoError> {
        self.root_path().to_str().ok_or_else(|| {
            DittoError::new(
                ErrorKind::InvalidInput,
                "Path is not valid UTF-8".to_string(),
            )
        })
    }

    /// Returns the path of the Ditto data directory as an `&str`
    #[deprecated(note = "Use root_path_as_str instead")]
    fn data_path_as_str(&self) -> Result<&str, DittoError> {
        self.root_path_as_str()
    }
}

/// A persistent working directory where Ditto will store its data across restarts of the host
/// process on the local file system
pub struct PersistentRoot {
    root: PathBuf,
}

/// A Temporary Ditto root which will clean itself up on exit
pub struct TempRoot {
    root: PathBuf,
}

impl Drop for TempRoot {
    fn drop(&mut self) {
        // we need to step up one level
        let mut ditto_root = self.root.clone(); // tempdir/jitter/ditto_root
        ditto_root.pop(); // tempdir/jitter <-- remove here
        ::log::debug!("Removing TempRoot {}", ditto_root.display());
        let _ = ::std::fs::remove_dir_all(&ditto_root);
    }
}

impl Default for PersistentRoot {
    fn default() -> Self {
        PersistentRoot::from_current_exe().unwrap() // This should never fail
    }
}

impl DittoRoot for PersistentRoot {
    fn root_path(&self) -> &Path {
        self.root.as_path()
    }
    fn exists(&self) -> bool {
        self.root.exists()
    }
    fn is_valid(&self) -> Result<(), DittoError> {
        Ok(()) // TODO
    }
}

// Constructors
impl PersistentRoot {
    /// Manually construct a root directory
    pub fn new(root_dir: impl Into<PathBuf>) -> Result<Self, DittoError> {
        let root = root_dir.into();
        std::fs::create_dir_all(&root).map_err(DittoError::from)?;
        let dir = PersistentRoot { root };

        if let Err(e) = dir.is_valid() {
            Err(e)
        } else {
            Ok(dir)
        }
    }

    /// Get a Ditto directory from the path of the current executable
    pub fn from_current_exe() -> Result<Self, DittoError> {
        let root_dir = env::current_exe()
            .ok()
            .and_then(|abspath| abspath.parent().map(|x| x.to_path_buf()))
            .ok_or_else(|| {
                DittoError::new(
                    ErrorKind::InvalidInput,
                    "Unable to resolve a default data directory on this platform".to_string(),
                )
            })?;
        Self::new(root_dir)
    }

    /// Create a Ditto directory from env vars
    pub fn from_env() -> Result<Self, DittoError> {
        let root_dir: PathBuf = env::var_os("DITTO_ROOT_PATH")
            .map(PathBuf::from)
            .ok_or_else(|| {
                DittoError::new(
                    ErrorKind::InvalidInput,
                    "The DITTO_ROOT_PATH env var is not set".to_string(),
                )
            })?;
        Self::new(root_dir)
    }
}

impl DittoRoot for TempRoot {
    fn root_path(&self) -> &Path {
        self.root.as_path()
    }
    fn exists(&self) -> bool {
        self.root.exists()
    }
    fn is_valid(&self) -> Result<(), DittoError> {
        Ok(()) // TODO
    }
}

impl TempRoot {
    // Temp randomization impl adapted from temp_dir crate but with a contemporary version of the
    // rand crate
    const NUM_RETRIES: u32 = 1 << 31;
    const NUM_RAND_CHARS: usize = 12;

    /// Create a new Ditto root which will be deleted when `TempRoot` is dropped
    pub fn new() -> Self {
        use std::iter;

        use rand::{distributions::Alphanumeric, Rng};
        let mut ditto_root;
        let tmpdir = std::env::temp_dir(); // the OS provided temp root from TMPDIR
        if !tmpdir.is_absolute() {
            let cur_dir = env::current_exe()
                .ok()
                .and_then(|abspath| abspath.parent().map(|x| x.to_path_buf()))
                .unwrap();
            ditto_root = cur_dir;
            ditto_root.push(tmpdir);
        } else {
            ditto_root = tmpdir;
        }

        let mut rng = rand::thread_rng();
        for _ in 0..Self::NUM_RETRIES {
            let jitter: String = iter::repeat(())
                .map(|()| rng.sample(Alphanumeric))
                .map(char::from)
                .take(Self::NUM_RAND_CHARS)
                .collect();
            ditto_root.push(jitter);
            match fs::create_dir_all(&ditto_root) {
                Ok(_) => {
                    ditto_root.push("ditto");
                    fs::create_dir_all(&ditto_root).unwrap();
                    return TempRoot { root: ditto_root };
                }
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
                Err(e) => {
                    panic!("Unable to create tempdir {:?}", e);
                }
            }
        }
        panic!("TempRoot {:?} already exists!", ditto_root.display());
    }
}

impl Default for TempRoot {
    fn default() -> Self {
        TempRoot::new()
    }
}

/// In v2 of the Ditto SDK, data used to be stored in a subdirectory named `ditto_data`.
///
/// This function will:
///   - if the `ditto_data` directory still exists
///     - drain its content to the parent directory
///     - delete the `ditto_data` directory
///   - else it will do nothing.
pub(crate) fn drain_ditto_data_dir(root: &Arc<dyn DittoRoot>) {
    let root: PathBuf = root.root_path().into();
    let old_data: PathBuf = root.join("ditto_data");
    if old_data.exists() {
        log::debug!("Migrating `ditto_data` to 'ditto' dir ({old_data:?}->{root:?})");
        let mut copy_options = fs_extra::dir::CopyOptions::new();
        copy_options.overwrite = true;
        // read_dir can only fail if:
        // * old_data is not a directory
        // * lacking permissions
        // * old_data do not exists
        // Unwrapping here is safe then.
        let data_children: Vec<_> = std::fs::read_dir(&old_data)
            .unwrap_or_else(|err| panic!("Failed to access {old_data:?}: {err}"))
            .filter_map(|child| child.ok())
            .map(|child| child.path())
            .collect();
        // Copy recursively all directories and their content to the root folder
        fs_extra::copy_items(&data_children, &root, &copy_options).unwrap();
        std::fs::remove_dir_all(old_data).unwrap();
    }
}