[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
This commit is contained in:
Ericson "Fogo" Soares 2023-01-17 11:17:14 -03:00 committed by GitHub
parent 1a1ddf3409
commit 31d04d5594
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 6 deletions

View file

@ -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(

View file

@ -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 {