fix some misc bugs & log

This commit is contained in:
OMGeeky
2023-06-25 17:22:53 +02:00
parent 0563b7ff7e
commit b7354800fe
4 changed files with 54 additions and 23 deletions

View File

@@ -520,7 +520,7 @@ impl Filesystem for DriveFilesystem {
match_provider_response!(response, reply, ProviderResponse::Rename, { match_provider_response!(response, reply, ProviderResponse::Rename, {
// //
debug!("Sending Ok.") debug!("Sending Ok.");
reply.ok(); reply.ok();
}); });
} }

View File

@@ -1,3 +1,4 @@
use std::mem::swap;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fmt::{Debug, Formatter}, fmt::{Debug, Formatter},
@@ -63,7 +64,7 @@ pub struct FileData {
} }
impl FileData { impl FileData {
fn get_id(&self) -> Option<DriveId> { fn get_id(&self) -> Option<DriveId> {
self.metadata.id.map(|x| DriveId::from(x)) self.metadata.id.as_ref().map(DriveId::from)
} }
} }
@@ -562,6 +563,7 @@ impl DriveFileProvider {
//endregion //endregion
//region rename //region rename
#[instrument(skip(request))]
async fn rename(&mut self, request: ProviderRenameRequest) -> Result<()> { async fn rename(&mut self, request: ProviderRenameRequest) -> Result<()> {
let original_parent = self.get_correct_id(request.original_parent.clone()); let original_parent = self.get_correct_id(request.original_parent.clone());
let original_name = request.original_name.into_string(); let original_name = request.original_name.into_string();
@@ -594,6 +596,7 @@ impl DriveFileProvider {
send_response!(request, ProviderResponse::Rename) send_response!(request, ProviderResponse::Rename)
} }
#[instrument(skip(self))]
async fn rename_inner( async fn rename_inner(
&mut self, &mut self,
original_parent: &DriveId, original_parent: &DriveId,
@@ -617,15 +620,21 @@ impl DriveFileProvider {
.wait_for_running_drive_request_if_exists(&file_id) .wait_for_running_drive_request_if_exists(&file_id)
.await; .await;
if let Err(e) = wait_res { if let Err(e) = wait_res {
return Err((e.to_string(), libc::EIO)); let msg = e.to_string();
error!("{}", msg);
return Err((msg, libc::EIO));
} }
if self.check_id_exists(&new_parent) { if !self.check_id_exists(new_parent) {
return Err((format!("Folder does not exist"), libc::ENOENT)); let msg = format!("Folder does not exist");
error!("{}", msg);
return Err((msg, libc::ENOENT));
} }
if self.does_target_name_exist_under_parent(&new_parent, &new_name) { if self.does_target_name_exist_under_parent(new_parent, new_name) {
return Err((format!("Target name is already used"), libc::EADDRINUSE)); let msg = format!("Target name is already used");
error!("{}", msg);
return Err((msg, libc::EADDRINUSE));
} }
let entry = self let entry = self
@@ -634,6 +643,7 @@ impl DriveFileProvider {
.expect("We checked shortly before if the entry exists"); .expect("We checked shortly before if the entry exists");
if original_name != new_name { if original_name != new_name {
trace!("Updating name");
//check if the filename has been changed and update it in the metadata and on google drive //check if the filename has been changed and update it in the metadata and on google drive
entry.changed_metadata.name = Some(new_name.clone()); entry.changed_metadata.name = Some(new_name.clone());
} }
@@ -642,6 +652,7 @@ impl DriveFileProvider {
entry.attr.mtime = now; entry.attr.mtime = now;
//check if the path is changed (child-parent relationships) and modify them accordingly //check if the path is changed (child-parent relationships) and modify them accordingly
if original_parent != new_parent { if original_parent != new_parent {
trace!("Updating child-parent relations");
entry.changed_metadata.parents = Some(vec![new_parent.to_string()]); entry.changed_metadata.parents = Some(vec![new_parent.to_string()]);
self.remove_parent_child_relation(original_parent.clone(), file_id.clone()); self.remove_parent_child_relation(original_parent.clone(), file_id.clone());
self.add_parent_child_relation(new_parent.clone(), file_id.clone()); self.add_parent_child_relation(new_parent.clone(), file_id.clone());
@@ -649,10 +660,9 @@ impl DriveFileProvider {
let upload_result = self.update_remote_metadata(file_id).await; let upload_result = self.update_remote_metadata(file_id).await;
if let Err(e) = upload_result { if let Err(e) = upload_result {
return Err(( let msg = format!("Error while uploading Metadata: {:?}", e);
format!("Error while uploading Metadata: {:?}", e), error!("{}", msg);
libc::EREMOTEIO, return Err((msg, libc::EREMOTEIO));
));
} }
Ok(()) Ok(())
@@ -682,11 +692,7 @@ impl DriveFileProvider {
//endregion //endregion
//region request helpers //region request helpers
fn does_target_name_exist_under_parent( fn does_target_name_exist_under_parent(&self, new_parent: &DriveId, new_name: &String) -> bool {
&self,
new_parent: &&DriveId,
new_name: &&String,
) -> bool {
let new_file_entry = self.find_first_child_by_name(&new_name, &new_parent); let new_file_entry = self.find_first_child_by_name(&new_name, &new_parent);
return new_file_entry.is_some(); return new_file_entry.is_some();
} }
@@ -867,17 +873,37 @@ impl DriveFileProvider {
changes changes
} }
async fn update_remote_metadata(&self, id: DriveId) -> Result<()> { #[instrument]
let file_data = self.entries.get(&id); async fn update_remote_metadata(&mut self, id: DriveId) -> Result<()> {
trace!("Uploading changed metadata");
let mut file_data = self.entries.get_mut(&id);
if file_data.is_none() { if file_data.is_none() {
return Err(anyhow!("Could not get entry with id: {}", id)); return Err(anyhow!("Could not get entry with id: {}", id));
} }
let file_data = file_data.unwrap(); let file_data = file_data.unwrap();
let mut metadata = file_data.changed_metadata.clone(); //extract changes from the file_data and replaces with new empty one
let mut metadata = DriveFileMetadata::default();
swap(&mut file_data.changed_metadata, &mut metadata);
Self::prepare_changed_metadata_for_upload(&id, &mut metadata); Self::prepare_changed_metadata_for_upload(&id, &mut metadata);
self.drive
.update_file_metadata_on_drive(metadata, &file_data.metadata);
self.drive
.update_file_metadata_on_drive(metadata, &file_data.metadata)
.await?;
self.reset_local_metadata_to_remote_version(&id).await?;
Ok(())
}
async fn reset_local_metadata_to_remote_version(&mut self, id: &DriveId) -> Result<()> {
let new_metadata = self.drive.get_metadata_for_file(id.clone()).await?;
let file_data = self.entries.get_mut(id);
if file_data.is_none() {
return Err(anyhow!("Could not get entry with id: {}", id));
}
let file_data = file_data.unwrap();
file_data.metadata = new_metadata;
file_data.changed_metadata = DriveFileMetadata::default();
Ok(()) Ok(())
} }

View File

@@ -504,10 +504,14 @@ pub async fn update_file_metadata_on_drive(
if has_parent_change { if has_parent_change {
//remove old parents //remove old parents
original_file.parents.map(|x| call.remove_parents(x)); if let Some(existing_parents) = original_file.parents.as_ref() {
for x in existing_parents.iter() {
call = call.remove_parents(x);
}
}
//add new parents //add new parents
for new_parent in parents { for new_parent in parents {
call.add_parents(new_parent.as_str()); call = call.add_parents(new_parent.as_str());
} }
} }

View File

@@ -30,6 +30,7 @@ pub mod fs;
pub mod google_drive; pub mod google_drive;
mod macros; mod macros;
pub mod prelude; pub mod prelude;
//region drive2 full example //region drive2 full example
pub async fn sample_drive2() -> Result<()> { pub async fn sample_drive2() -> Result<()> {
let mountpoint = Path::new("/tmp/fuse/3"); let mountpoint = Path::new("/tmp/fuse/3");