Hash the source files diff

This commit is contained in:
Alexander
2026-06-26 17:56:56 +02:00
parent 536e117576
commit 3d1f9a206f
5 changed files with 204 additions and 97 deletions
+113 -86
View File
@@ -1,7 +1,12 @@
use std::{
collections::BTreeMap,
fs, io,
os::unix::fs::{MetadataExt, PermissionsExt},
fs,
hash::{Hash, Hasher},
io,
os::unix::{
ffi::OsStrExt,
fs::{MetadataExt, PermissionsExt},
},
path::{Path, PathBuf},
sync::{Arc, Mutex, mpsc},
thread,
@@ -11,6 +16,7 @@ use std::{
use clap::builder::OsStr;
use fuser::{Errno, FileAttr, Filesystem, Generation, INodeNo, Request};
use notify::{Event, EventKind, RecursiveMode, Watcher};
use twox_hash::XxHash64;
use crate::file_watcher::FileWatcher;
use crate::music_metadata::MusicMetadata;
@@ -32,6 +38,37 @@ struct LocalItem {
music_metadata: Option<MusicMetadata>,
}
impl LocalItem {
fn hash(&self) -> u64 {
let seed = 1234;
let mut hasher = XxHash64::with_seed(seed);
let metadata = &self.metadata;
let mtime: u64 = metadata
.modified()
.unwrap_or(SystemTime::UNIX_EPOCH)
.duration_since(SystemTime::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let crtime: u64 = metadata
.created()
.unwrap_or(SystemTime::UNIX_EPOCH)
.duration_since(SystemTime::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
hasher.write_u64(self.inode.0);
hasher.write(self.name.as_bytes());
hasher.write(self.original_path.as_os_str().as_bytes());
hasher.write(self.local_path.as_os_str().as_bytes());
hasher.write_u64(metadata.ctime() as u64);
hasher.write_u64(mtime);
hasher.write_u64(crtime);
return hasher.finish();
}
}
#[derive(Debug)]
pub struct LocalOrigin {
source: PathBuf,
@@ -43,32 +80,15 @@ impl LocalOrigin {
pub fn new(source: String, destination: String) -> Result<LocalOrigin, io::Error> {
println!("Initializing the LocalOrigin");
let map: Arc<Mutex<BTreeMap<INodeNo, LocalItem>>> = Arc::new(Mutex::new(BTreeMap::new()));
let snapshot = LocalOrigin::build_snapshot(Path::new(&source), Path::new(&destination))?;
let local_root = LocalItem {
inode: INodeNo::ROOT,
name: "/".to_string(),
original_path: PathBuf::from(&source),
local_path: PathBuf::from(&destination),
file_type: FileType::Directory,
metadata: fs::metadata(&source).unwrap(),
music_metadata: Option::None,
let local_origin = LocalOrigin {
source: source.into(),
destination: destination.into(),
files: Arc::new(Mutex::new(snapshot)),
};
map.lock().unwrap().insert(INodeNo::ROOT, local_root);
match LocalOrigin::read_source(Path::new(&source), Path::new(&destination), &map) {
Ok(_) => {
let local_origin = LocalOrigin {
source: source.into(),
destination: destination.into(),
files: map,
};
dbg!(&local_origin);
return Ok(local_origin);
}
Err(err) => return Err(err),
}
dbg!(&local_origin);
Ok(local_origin)
}
fn fill_fileset(
@@ -76,84 +96,90 @@ impl LocalOrigin {
source: &Path,
destination: &Path,
) {
match LocalOrigin::build_snapshot(source, destination) {
Ok(new_snapshot) => {
let mut files = map.lock().unwrap();
files.retain(|ino, _| new_snapshot.contains_key(ino));
for (ino, new_item) in new_snapshot {
match files.get(&ino) {
Some(existing) if existing.hash() == new_item.hash() => {}
_ => {
files.insert(ino, new_item);
}
}
}
}
Err(e) => eprintln!("Error while reading source: {e}"),
}
}
fn build_snapshot(
source: &Path,
destination: &Path,
) -> Result<BTreeMap<INodeNo, LocalItem>, io::Error> {
let mut map = BTreeMap::new();
let local_root = LocalItem {
inode: INodeNo::ROOT,
name: "/".to_string(),
original_path: source.to_path_buf(),
local_path: destination.to_path_buf(),
file_type: FileType::Directory,
metadata: fs::metadata(&source).unwrap(),
music_metadata: Option::None,
metadata: fs::metadata(source)?,
music_metadata: None,
};
map.lock().unwrap().insert(INodeNo::ROOT, local_root);
map.insert(INodeNo::ROOT, local_root);
let _ = LocalOrigin::read_source(&source, &destination, &map)
.inspect_err(|e| eprintln!("Error while reading source: {e}"));
LocalOrigin::read_into_map(source, destination, &mut map)?;
Ok(map)
}
fn read_source(
fn read_into_map(
source: &Path,
destination: &Path,
map: &Arc<Mutex<BTreeMap<INodeNo, LocalItem>>>,
map: &mut BTreeMap<INodeNo, LocalItem>,
) -> Result<(), io::Error> {
match fs::read_dir(source) {
Ok(dir) => {
for item in dir {
match item {
Ok(entry) => {
let name = entry.file_name().to_string_lossy().into_owned();
let item_path: PathBuf = entry.path();
let metadata: fs::Metadata = entry.metadata().unwrap();
let file_type = if entry.file_type().unwrap().is_dir() {
FileType::Directory
} else {
FileType::File
};
for item in fs::read_dir(source)? {
let entry = item?;
let name = entry.file_name().to_string_lossy().into_owned();
let item_path = entry.path();
let metadata = entry.metadata()?;
let file_type = if entry.file_type()?.is_dir() {
FileType::Directory
} else {
FileType::File
};
let mut local_path: PathBuf = PathBuf::new();
// local_path.push(destination);
local_path.push(name.clone());
let mut local_path = PathBuf::new();
local_path.push(name.clone());
let music_metadata: Option<MusicMetadata> = if item_path
.extension()
.and_then(std::ffi::OsStr::to_str)
.unwrap()
== "flac"
{
MusicMetadata::parse_music_metadata(&item_path)
} else {
Option::None
};
let music_metadata =
if item_path.extension().and_then(std::ffi::OsStr::to_str) == Some("flac") {
MusicMetadata::parse_music_metadata(&item_path)
} else {
None
};
let local_item = LocalItem {
inode: INodeNo(metadata.ino()),
name: name,
original_path: item_path.clone(),
local_path: local_path,
file_type: file_type,
metadata: metadata,
music_metadata: music_metadata,
};
let inode = local_item.inode;
let inode = INodeNo(metadata.ino());
let local_item = LocalItem {
inode,
name,
original_path: item_path.clone(),
local_path,
file_type,
metadata,
music_metadata,
};
map.lock().unwrap().insert(inode, local_item);
map.insert(inode, local_item);
if file_type == FileType::Directory {
match LocalOrigin::read_source(&item_path, destination, map) {
Ok(_) => {}
Err(err) => return Err(err),
};
}
}
Err(err) => return Err(err),
}
}
if file_type == FileType::Directory {
LocalOrigin::read_into_map(&item_path, destination, map)?;
}
Err(err) => return Err(err),
};
return Ok(());
}
Ok(())
}
fn local_file_to_file_attr(local_file: &LocalItem) -> FileAttr {
@@ -243,12 +269,13 @@ impl Filesystem for LocalOrigin {
.lock()
.unwrap()
.iter()
.filter(|(key, _)| **key != INodeNo::ROOT)
.skip(offset as usize)
.enumerate()
{
let offset: u64 = 2 + i as u64;
let file_type: fuser::FileType = LocalOrigin::file_type(value.file_type);
let path = &value.local_path;
let path = &value.name;
let buffer_full: bool = reply.add(*key, offset, file_type, path);