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
use crate::hasher::{hash_files, HashType};
use anyhow::{anyhow, Error};
use bytes::BytesMut;
use chrono::Utc;
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::{env, fs, thread};

#[derive(Debug, Clone)]
pub struct Snapshot {
    pub file_hashes: Arc<Mutex<HashMap<String, FileMetadata>>>,
    pub black_list: Vec<String>,
    pub root_path: String,
    pub hash_type: HashType,
    pub uuid: String,
    pub date_created: i64,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FileMetadata {
    pub path: String,
    pub check_sum: Vec<u8>,
    pub size: u64,
    pub ino: u64,
    pub ctime: i64,
    pub mtime: i64,
}

impl Default for FileMetadata {
    fn default() -> Self {
        FileMetadata {
            path: "".to_string(),
            check_sum: vec![],
            size: 0,
            ino: 0,
            ctime: 0,
            mtime: 0,
        }
    }
}

impl Snapshot {
    pub fn new(
        path: &Path,
        hash_type: HashType,
        black_list: Vec<String>,
    ) -> Result<Snapshot, Error> {
        let root_path = match path.to_str() {
            None => "".to_string(),
            Some(p) => p.to_string(),
        };
        let mut rand = thread_rng();
        let uuid_int: i128 = rand.gen();
        let uuid = uuid_int.to_string();
        let file_paths = walkdir::WalkDir::new(path).sort_by_file_name();
        let file_hashes: Arc<Mutex<HashMap<String, FileMetadata>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let mut hashers: Vec<JoinHandle<()>> = vec![];

        for p in file_paths.into_iter().flatten() {
            let file_path = p.path().to_str().expect("path_string_error").to_string();
            if p.path().is_file() && !black_list.contains(&file_path) {
                let bind = file_hashes.clone();

                let handle = thread::spawn(move || {
                    let mut binding = bind.lock();
                    let ht = binding.as_mut().expect("binding error");
                    let _ = hash_files(p.path(), ht, hash_type);
                });
                hashers.push(handle)
            }
        }

        for handle in hashers {
            handle.join().expect("could not join handle")
        }

        Ok(Snapshot {
            file_hashes,
            black_list,
            root_path,
            hash_type,
            uuid,
            date_created: Utc::now().timestamp(),
        })
    }
}

impl Default for Snapshot {
    fn default() -> Self {
        let black_list: Vec<String> = vec![];
        Snapshot {
            file_hashes: Arc::new(Mutex::new(HashMap::new())),
            black_list,
            root_path: "".to_string(),
            hash_type: HashType::BLAKE3,
            uuid: "".to_string(),
            date_created: 0,
        }
    }
}

pub enum SnapshotChangeType {
    None,
    Created,
    Deleted,
    Changed,
}

#[derive(Debug)]
pub struct SnapshotCompareResult {
    pub created: Vec<String>,
    pub deleted: Vec<String>,
    pub changed: Vec<String>,
}

pub fn compare(
    left: Snapshot,
    right: Snapshot,
) -> Option<(SnapshotChangeType, SnapshotCompareResult)> {
    #[allow(unused)]
    let success = true;
    let mut created: Vec<String> = vec![];
    let mut deleted: Vec<String> = vec![];
    let mut changed: Vec<String> = vec![];

    if let Ok(left_lock) = left.file_hashes.lock() {
        // for each entry in the hash list
        for left_entry in left_lock.iter() {
            if let Ok(curr_lock) = right.file_hashes.lock() {
                match curr_lock.get(left_entry.0) {
                    // check for mis-matching checksum
                    Some(right_entry) => {
                        if !right_entry.check_sum.eq(&left_entry.1.check_sum) {
                            changed.push(right_entry.path.to_string());
                        }
                    }
                    // check for deletion
                    None => {
                        deleted.push(left_entry.0.to_string());
                    }
                }
            }
        }
    }

    if let Ok(e) = right.file_hashes.lock() {
        for right_entry in e.iter() {
            // check for file creations
            if left.file_hashes.lock().ok()?.get(right_entry.0).is_none() {
                created.push(right_entry.0.to_string());
            }
        }
    }

    let mut return_type = SnapshotChangeType::None;
    if !created.is_empty() {
        return_type = SnapshotChangeType::Created;
    }
    if !deleted.is_empty() {
        return_type = SnapshotChangeType::Deleted;
    }
    if !changed.is_empty() {
        return_type = SnapshotChangeType::Changed;
    }

    Some((
        return_type,
        SnapshotCompareResult {
            created,
            deleted,
            changed,
        },
    ))
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct SerializableSnapshot {
    pub file_hashes: Vec<FileMetadata>,
    pub root_path: String,
    pub hash_type: HashType,
    pub uuid: String,
    pub date_created: i64,
}

fn path_resolve(path: String) -> String {
    #[allow(unused)]
    let mut full_path = String::new();
    if path.starts_with("./") {
        let mut cur_dir: String = match env::current_dir() {
            Ok(pb) => match pb.to_str() {
                None => String::new(),
                Some(str) => str.to_string(),
            },
            Err(_) => String::new(),
        };
        cur_dir.push('/');
        full_path = path.replace("./", cur_dir.as_str());
    } else {
        full_path = path.to_string();
    }
    full_path
}

pub fn export(snapshot: Snapshot, path: String, overwrite: bool) -> Result<(), Error> {
    let full_path = path_resolve(path);

    let mut fh: Vec<FileMetadata> = vec![];

    if let Ok(unlocked) = snapshot.file_hashes.lock() {
        for entry in unlocked.iter() {
            fh.push(entry.1.clone())
        }
    }

    let serializable = SerializableSnapshot {
        file_hashes: fh,
        root_path: snapshot.root_path,
        hash_type: snapshot.hash_type,
        uuid: snapshot.uuid,
        date_created: snapshot.date_created,
    };

    let serialized = serde_json::to_string(&serializable)?;
    // println!("{:#?}", serialized);
    let filename = full_path
        .split('/')
        .last()
        .expect("unable to get full path");
    let path_only = full_path.replace(filename, "");
    // println!("{}", full_path);
    // println!("{}", path_only);

    if Path::new(&full_path).exists() && overwrite {
        fs::remove_file(&full_path)?;
        write_to_file(path_only, full_path, serialized)?
    } else if !Path::new(&full_path).exists() {
        write_to_file(path_only, full_path, serialized)?
    };
    Ok(())
}

fn write_to_file(path_only: String, full_path: String, serialized: String) -> Result<(), Error> {
    if fs::create_dir_all(&path_only).is_ok() {
        if let Ok(mut file_handle) = File::create(full_path) {
            Ok(file_handle.write_all(serialized.as_bytes())?)
        } else {
            Err(anyhow!("Unable to write to path: {}", path_only.clone()))
        }
    } else {
        Err(anyhow!("Unable to write to path: {}", path_only.clone()))
    }
}

pub fn import(path: String) -> Result<Snapshot, Error> {
    #[allow(unused)]
    let buffer = BytesMut::new();
    let full_path = path_resolve(path);
    if let Ok(bytes) = fs::read(full_path) {
        let snapshot = serde_json::from_slice::<SerializableSnapshot>(&bytes)?;

        let mut fh: HashMap<String, FileMetadata> = HashMap::new();

        // println!("{}", snapshot.file_hashes.len());

        for entry in snapshot.file_hashes {
            if let Some(_res) = fh.insert(entry.path.clone(), entry.clone()) {
                // println!("successfully imported: {}", entry.path);
            }
        }
        let black_list: Vec<String> = vec![];
        Ok(Snapshot {
            file_hashes: Arc::new(Mutex::new(fh)),
            black_list,
            root_path: snapshot.root_path,
            hash_type: snapshot.hash_type,
            uuid: snapshot.uuid,
            date_created: snapshot.date_created,
        })
    } else {
        Ok(Snapshot::default())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compare_snapshots;
    use std::fs;
    use std::fs::File;
    use std::path::Path;

    #[test]
    fn create_snapshot_blake3() {
        let test_snap_b3 = Snapshot::new(Path::new("/etc"), HashType::BLAKE3, vec![]);
        assert!(test_snap_b3.unwrap().file_hashes.lock().unwrap().len() > 0);
    }
    #[test]
    fn create_snapshot_md5() {
        let test_snap_md5 = Snapshot::new(Path::new("/etc"), HashType::MD5, vec![]);
        assert!(test_snap_md5.unwrap().file_hashes.lock().unwrap().len() > 0);
    }
    #[test]
    fn create_snapshot_sha3() {
        let test_snap_sha3 = Snapshot::new(Path::new("/etc"), HashType::SHA3, vec![]);
        assert!(test_snap_sha3.unwrap().file_hashes.lock().unwrap().len() > 0);
    }

    #[test]
    fn export_snapshot() {
        assert!(!Path::new("./build/out.snapshot").exists());
        let test_snap_export = Snapshot::new(Path::new("/etc"), HashType::BLAKE3, vec![]);
        let _ = export(
            test_snap_export.unwrap().clone(),
            "./build/out.snapshot".to_string(),
            true,
        );
        assert!(Path::new("./build/out.snapshot").exists());
        fs::remove_file(Path::new("./build/out.snapshot")).unwrap();
    }

    #[test]
    fn import_snapshot() {
        let test_snap_import = Snapshot::new(Path::new("/etc"), HashType::BLAKE3, vec![]);
        let _ = export(
            test_snap_import.unwrap(),
            "./build/in.snapshot".to_string(),
            true,
        );
        let snapshot = import("./build/in.snapshot".to_string());
        assert!(snapshot.unwrap().file_hashes.lock().unwrap().len() > 0);
        fs::remove_file(Path::new("./build/in.snapshot")).unwrap();
    }

    #[test]
    fn creation_detection() {
        assert!(!Path::new("./build/test_creation/").exists());
        fs::create_dir_all(Path::new("./build/test_creation/")).unwrap();
        let test_snap_creation_1 = Snapshot::new(
            Path::new("./build/test_creation/"),
            HashType::BLAKE3,
            vec![],
        );
        File::create(Path::new("./build/test_creation/test1")).unwrap();
        File::create(Path::new("./build/test_creation/test2")).unwrap();
        File::create(Path::new("./build/test_creation/test3")).unwrap();
        let test_snap_creation_2 = Snapshot::new(
            Path::new("./build/test_creation/"),
            HashType::BLAKE3,
            vec![],
        );
        assert_eq!(
            compare_snapshots(test_snap_creation_1.unwrap(), test_snap_creation_2.unwrap())
                .unwrap()
                .1
                .created
                .len(),
            3
        );
        fs::remove_dir_all(Path::new("./build/test_creation/")).unwrap();
    }

    #[test]
    fn deletion_detection() {
        assert!(!Path::new("./build/test_deletion/").exists());
        fs::create_dir_all(Path::new("./build/test_deletion/")).unwrap();
        let test_snap_deletion_1 = Snapshot::new(
            Path::new("./build/test_deletion/"),
            HashType::BLAKE3,
            vec![],
        );
        File::create(Path::new("./build/test_deletion/test1")).unwrap();
        File::create(Path::new("./build/test_deletion/test2")).unwrap();
        File::create(Path::new("./build/test_deletion/test3")).unwrap();
        let test_snap_deletion_2 = Snapshot::new(
            Path::new("./build/test_deletion/"),
            HashType::BLAKE3,
            vec![],
        );
        assert_eq!(
            compare_snapshots(test_snap_deletion_2.unwrap(), test_snap_deletion_1.unwrap())
                .unwrap()
                .1
                .deleted
                .len(),
            3
        );
        fs::remove_dir_all(Path::new("./build/test_deletion/")).unwrap();
    }

    #[test]
    fn change_detection() {
        assert!(!Path::new("./build/test_change/").exists());
        fs::create_dir_all(Path::new("./build/test_change/")).unwrap();
        let mut file1 = File::create(Path::new("./build/test_change/test1")).unwrap();
        let mut file2 = File::create(Path::new("./build/test_change/test2")).unwrap();
        let mut file3 = File::create(Path::new("./build/test_change/test3")).unwrap();
        let test_snap_change_1 =
            Snapshot::new(Path::new("./build/test_change/"), HashType::BLAKE3, vec![]);
        file1.write_all("file1".as_bytes()).unwrap();
        file2.write_all("file2".as_bytes()).unwrap();
        file3.write_all("file3".as_bytes()).unwrap();
        let test_snap_change_2 =
            Snapshot::new(Path::new("./build/test_change/"), HashType::BLAKE3, vec![]);
        assert_eq!(
            compare_snapshots(test_snap_change_1.unwrap(), test_snap_change_2.unwrap())
                .unwrap()
                .1
                .changed
                .len(),
            3
        );
        fs::remove_dir_all(Path::new("./build/test_change/")).unwrap();
    }
}