mirror of
https://github.com/chatmail/core.git
synced 2026-05-17 05:46:30 +03:00
Create parent directory if creating a new file fails (#2978)
With this PR, my encrypted-storage Android PR now works, at least I couldn't find any further bugs. Without it, configuring fails with: `Failed to create blob icon-saved-messages-1803424689.png in /data/user/0/com.b44t.messenger.beta/files/accounts/0e402b37dcd14a9586aea46294c908f2/dc.db-blobs: No such file or directory (os error 2)`. Also see https://github.com/deltachat/deltachat-core-rust/pull/2972.
This commit is contained in:
@@ -24,6 +24,7 @@ use crate::constants::{
|
|||||||
};
|
};
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::events::EventType;
|
use crate::events::EventType;
|
||||||
|
use crate::log::LogExt;
|
||||||
use crate::message;
|
use crate::message;
|
||||||
|
|
||||||
/// Represents a file in the blob directory.
|
/// Represents a file in the blob directory.
|
||||||
@@ -63,7 +64,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
) -> std::result::Result<BlobObject<'a>, BlobError> {
|
) -> std::result::Result<BlobObject<'a>, BlobError> {
|
||||||
let blobdir = context.get_blobdir();
|
let blobdir = context.get_blobdir();
|
||||||
let (stem, ext) = BlobObject::sanitise_name(suggested_name);
|
let (stem, ext) = BlobObject::sanitise_name(suggested_name);
|
||||||
let (name, mut file) = BlobObject::create_new_file(blobdir, &stem, &ext).await?;
|
let (name, mut file) = BlobObject::create_new_file(context, blobdir, &stem, &ext).await?;
|
||||||
file.write_all(data)
|
file.write_all(data)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| BlobError::WriteFailure {
|
.map_err(|err| BlobError::WriteFailure {
|
||||||
@@ -87,6 +88,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
|
|
||||||
// Creates a new file, returning a tuple of the name and the handle.
|
// Creates a new file, returning a tuple of the name and the handle.
|
||||||
async fn create_new_file(
|
async fn create_new_file(
|
||||||
|
context: &Context,
|
||||||
dir: &Path,
|
dir: &Path,
|
||||||
stem: &str,
|
stem: &str,
|
||||||
ext: &str,
|
ext: &str,
|
||||||
@@ -109,6 +111,8 @@ impl<'a> BlobObject<'a> {
|
|||||||
blobname: name,
|
blobname: name,
|
||||||
cause: err,
|
cause: err,
|
||||||
});
|
});
|
||||||
|
} else if attempt == 1 && !dir.exists().await {
|
||||||
|
fs::create_dir_all(dir).await.ok_or_log(context);
|
||||||
} else {
|
} else {
|
||||||
name = format!("{}-{}{}", stem, rand::random::<u32>(), ext);
|
name = format!("{}-{}{}", stem, rand::random::<u32>(), ext);
|
||||||
}
|
}
|
||||||
@@ -149,7 +153,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
})?;
|
})?;
|
||||||
let (stem, ext) = BlobObject::sanitise_name(&src.to_string_lossy());
|
let (stem, ext) = BlobObject::sanitise_name(&src.to_string_lossy());
|
||||||
let (name, mut dst_file) =
|
let (name, mut dst_file) =
|
||||||
BlobObject::create_new_file(context.get_blobdir(), &stem, &ext).await?;
|
BlobObject::create_new_file(context, context.get_blobdir(), &stem, &ext).await?;
|
||||||
let name_for_err = name.clone();
|
let name_for_err = name.clone();
|
||||||
if let Err(err) = io::copy(&mut src_file, &mut dst_file).await {
|
if let Err(err) = io::copy(&mut src_file, &mut dst_file).await {
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user