Fix file sync timing and prevent crash on missing SyncFromDiskMetadata (#2595)

* max_time is the current_time(); SyncFromDiskMetadata might not be in state

* using metadata_or_insert_with
This commit is contained in:
cube0x8 2024-10-09 18:16:32 +03:00 committed by GitHub
parent 2b05e0a1fd
commit 1e4d38d744
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -102,23 +102,22 @@ where
}
}
let max_time = match last {
None => None,
Some(last) => Some(last + self.interval),
};
let new_max_time = max_time.unwrap_or(current_time());
let new_max_time = current_time();
let mut new_files = vec![];
for dir in &self.sync_dirs {
log::debug!("Syncing from dir: {:?}", dir);
let new_dir_files = find_new_files_rec(dir, &max_time)?;
let new_dir_files = find_new_files_rec(dir, &last)?;
new_files.extend(new_dir_files);
}
*state.metadata_mut::<SyncFromDiskMetadata>().unwrap() = SyncFromDiskMetadata {
last_time: new_max_time,
left_to_sync: new_files,
};
let sync_from_disk_metadata = state.metadata_mut::<SyncFromDiskMetadata>().unwrap();
let sync_from_disk_metadata = state
.metadata_or_insert_with(|| SyncFromDiskMetadata::new(new_max_time, new_files.clone()));
// At the very first sync, last_time and file_to_sync are set twice
sync_from_disk_metadata.last_time = new_max_time;
sync_from_disk_metadata.left_to_sync = new_files;
// Iterate over the paths of files left to sync.
// By keeping track of these files, we ensure that no file is missed during synchronization,
// even in the event of a target restart.