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
// Rust guideline compliant 2025-10-17
use sha2::{Digest, Sha256};
use crate::db::Database;
use crate::errors::Result;
/// Compute SHA-256 content hash of file content.
pub fn content_hash(content: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(content.as_bytes());
let result = hasher.finalize();
hex::encode(result)
}
/// Find files whose stored content hash differs from the current hash.
pub async fn find_stale_files(db: &Database, current_hashes: &[(String, String)]) -> Result<Vec<String>> {
let mut stale = Vec::new();
for (path, current_hash) in current_hashes {
if let Some(file_record) = db.get_file(path).await? {
if file_record.content_hash != *current_hash {
stale.push(path.clone());
}
}
}
Ok(stale)
}
/// Find files that exist on disk but not in the database.
pub async fn find_new_files(db: &Database, current_files: &[String]) -> Result<Vec<String>> {
let mut new_files = Vec::new();
for path in current_files {
if db.get_file(path).await?.is_none() {
new_files.push(path.clone());
}
}
Ok(new_files)
}
/// Find files that are in the database but no longer exist on disk.
pub async fn find_removed_files(db: &Database, current_files: &[String]) -> Result<Vec<String>> {
let all_db_files = db.get_all_files().await?;
let current_set: std::collections::HashSet<&str> =
current_files.iter().map(|s| s.as_str()).collect();
let mut removed = Vec::new();
for file_record in &all_db_files {
if !current_set.contains(file_record.path.as_str()) {
removed.push(file_record.path.clone());
}
}
Ok(removed)
}