Working Location-Watcher system for Android app (#2212)

* Update android.rs

* Clean up

* import `debug`

* `cargo fmt`
This commit is contained in:
Arnab Chakraborty 2024-03-15 05:38:32 +00:00 committed by GitHub
parent ae4e65de93
commit 4768fddef4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,10 +1,10 @@
//! Android file system watcher implementation. //! Android file system watcher implementation.
//! TODO: Still being worked on by @Rocky43007 on Branch Rocky43007:location-watcher-test-3 //! Just basically linux.rs
//! DO NOT TOUCH FOR NOW
use crate::{invalidate_query, library::Library, location::manager::LocationManagerError, Node}; use crate::{invalidate_query, library::Library, location::manager::LocationManagerError, Node};
use sd_prisma::prisma::location; use sd_prisma::prisma::location;
use sd_utils::error::FileIOError;
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
@ -13,12 +13,15 @@ use std::{
}; };
use async_trait::async_trait; use async_trait::async_trait;
use notify::Event; use notify::{
use tokio::time::Instant; event::{CreateKind, DataChange, ModifyKind, RenameMode},
use tracing::{error, info, trace}; Event, EventKind,
};
use tokio::{fs, time::Instant};
use tracing::{debug, error, trace};
use super::{ use super::{
utils::{recalculate_directories_size, remove, update_file}, utils::{create_dir, recalculate_directories_size, remove, rename, update_file},
EventHandler, HUNDRED_MILLIS, ONE_SECOND, EventHandler, HUNDRED_MILLIS, ONE_SECOND,
}; };
@ -58,92 +61,92 @@ impl<'lib> EventHandler<'lib> for AndroidEventHandler<'lib> {
} }
async fn handle_event(&mut self, event: Event) -> Result<(), LocationManagerError> { async fn handle_event(&mut self, event: Event) -> Result<(), LocationManagerError> {
info!("Received Android event: {:#?}", event); debug!("Received Android event: {:#?}", event);
// let Event { let Event {
// kind, mut paths, .. kind, mut paths, ..
// } = event; } = event;
// match kind { match kind {
// EventKind::Create(CreateKind::File) EventKind::Create(CreateKind::File)
// | EventKind::Modify(ModifyKind::Data(DataChange::Any)) => { | EventKind::Modify(ModifyKind::Data(DataChange::Any)) => {
// // When we receive a create, modify data or metadata events of the abore kinds // When we receive a create, modify data or metadata events of the abore kinds
// // we just mark the file to be updated in a near future // we just mark the file to be updated in a near future
// // each consecutive event of these kinds that we receive for the same file // each consecutive event of these kinds that we receive for the same file
// // we just store the path again in the map below, with a new instant // we just store the path again in the map below, with a new instant
// // that effectively resets the timer for the file to be updated // that effectively resets the timer for the file to be updated
// let path = paths.remove(0); let path = paths.remove(0);
// if self.files_to_update.contains_key(&path) { if self.files_to_update.contains_key(&path) {
// if let Some(old_instant) = if let Some(old_instant) =
// self.files_to_update.insert(path.clone(), Instant::now()) self.files_to_update.insert(path.clone(), Instant::now())
// { {
// self.reincident_to_update_files self.reincident_to_update_files
// .entry(path) .entry(path)
// .or_insert(old_instant); .or_insert(old_instant);
// } }
// } else { } else {
// self.files_to_update.insert(path, Instant::now()); self.files_to_update.insert(path, Instant::now());
// } }
// } }
// EventKind::Create(CreateKind::Folder) => { EventKind::Create(CreateKind::Folder) => {
// let path = &paths[0]; let path = &paths[0];
// // Don't need to dispatch a recalculate directory event as `create_dir` dispatches // Don't need to dispatch a recalculate directory event as `create_dir` dispatches
// // a `scan_location_sub_path` function, which recalculates the size already // a `scan_location_sub_path` function, which recalculates the size already
// create_dir( create_dir(
// self.location_id, self.location_id,
// path, path,
// &fs::metadata(path) &fs::metadata(path)
// .await .await
// .map_err(|e| FileIOError::from((path, e)))?, .map_err(|e| FileIOError::from((path, e)))?,
// self.node, self.node,
// self.library, self.library,
// ) )
// .await?; .await?;
// } }
// EventKind::Modify(ModifyKind::Name(RenameMode::From)) => { EventKind::Modify(ModifyKind::Name(RenameMode::From)) => {
// // Just in case we can't garantee that we receive the Rename From event before the // Just in case we can't garantee that we receive the Rename From event before the
// // Rename Both event. Just a safeguard // Rename Both event. Just a safeguard
// if self.recently_renamed_from.remove(&paths[0]).is_none() { if self.recently_renamed_from.remove(&paths[0]).is_none() {
// self.rename_from.insert(paths.remove(0), Instant::now()); self.rename_from.insert(paths.remove(0), Instant::now());
// } }
// } }
// EventKind::Modify(ModifyKind::Name(RenameMode::Both)) => { EventKind::Modify(ModifyKind::Name(RenameMode::Both)) => {
// let from_path = &paths[0]; let from_path = &paths[0];
// let to_path = &paths[1]; let to_path = &paths[1];
// self.rename_from.remove(from_path); self.rename_from.remove(from_path);
// rename( rename(
// self.location_id, self.location_id,
// to_path, to_path,
// from_path, from_path,
// fs::metadata(to_path) fs::metadata(to_path)
// .await .await
// .map_err(|e| FileIOError::from((to_path, e)))?, .map_err(|e| FileIOError::from((to_path, e)))?,
// self.library, self.library,
// ) )
// .await?; .await?;
// self.recently_renamed_from self.recently_renamed_from
// .insert(paths.swap_remove(0), Instant::now()); .insert(paths.swap_remove(0), Instant::now());
// } }
// EventKind::Remove(_) => { EventKind::Remove(_) => {
// let path = paths.remove(0); let path = paths.remove(0);
// if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
// if parent != Path::new("") { if parent != Path::new("") {
// self.to_recalculate_size self.to_recalculate_size
// .insert(parent.to_path_buf(), Instant::now()); .insert(parent.to_path_buf(), Instant::now());
// } }
// } }
// remove(self.location_id, &path, self.library).await?; remove(self.location_id, &path, self.library).await?;
// } }
// other_event_kind => { other_event_kind => {
// trace!("Other Linux event that we don't handle for now: {other_event_kind:#?}"); trace!("Other Linux event that we don't handle for now: {other_event_kind:#?}");
// } }
// } }
Ok(()) Ok(())
} }