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