From 31d04d5594614e26803dd47dc2050e84293de242 Mon Sep 17 00:00:00 2001 From: "Ericson \"Fogo\" Soares" Date: Tue, 17 Jan 2023 11:17:14 -0300 Subject: [PATCH] [ENG-333] Handle .spacedrive existing instead of throwing error (#521) * Removing metadata files that failed to be deserialized * Reintroducing deserialize error for location metadata on prod builds --- core/src/location/metadata.rs | 31 ++++++++++++++++++++++++++++--- core/src/object/fs/encrypt.rs | 8 +++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/core/src/location/metadata.rs b/core/src/location/metadata.rs index 63790adb3..d685881f6 100644 --- a/core/src/location/metadata.rs +++ b/core/src/location/metadata.rs @@ -7,6 +7,7 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use thiserror::Error; use tokio::{fs, io}; +use tracing::error; use uuid::Uuid; static SPACEDRIVE_LOCATION_METADATA_FILE: &str = ".spacedrive"; @@ -45,10 +46,34 @@ impl SpacedriveLocationMetadataFile { match fs::read(&metadata_file_name).await { Ok(data) => Ok(Some(Self { + metadata: match serde_json::from_slice(&data) { + Ok(data) => data, + Err(e) => { + #[cfg(debug_assertions)] + { + error!( + "Failed to deserialize corrupted metadata file, \ + we will remove it and create a new one; File: {}; Error: {e}", + metadata_file_name.display() + ); + fs::remove_file(&metadata_file_name).await.map_err(|e| { + LocationMetadataError::Delete( + e, + location_path.as_ref().to_path_buf(), + ) + })?; + + return Ok(None); + } + + #[cfg(not(debug_assertions))] + return Err(LocationMetadataError::Deserialize( + e, + location_path.as_ref().to_path_buf(), + )); + } + }, path: metadata_file_name, - metadata: serde_json::from_slice(&data).map_err(|e| { - LocationMetadataError::Deserialize(e, location_path.as_ref().to_path_buf()) - })?, })), Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None), Err(e) => Err(LocationMetadataError::Read( diff --git a/core/src/object/fs/encrypt.rs b/core/src/object/fs/encrypt.rs index adbb89c5f..77e882993 100644 --- a/core/src/object/fs/encrypt.rs +++ b/core/src/object/fs/encrypt.rs @@ -216,9 +216,11 @@ impl StatefulJob for FileEncryptorJob { .find_unique(object::id::equals(obj_id)) .exec() .await? - .ok_or(JobError::JobDataNotFound(String::from( - "can't find information about the object", - )))?; + .ok_or_else(|| { + JobError::JobDataNotFound(String::from( + "can't find information about the object", + )) + })?; if state.init.metadata { let metadata = Metadata {