From 5d9bb72c0baf03308540b9fe0f219ac6a13e9d6c Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 15 May 2022 11:28:48 +0300 Subject: [PATCH] message: augment cache-path() so it supports subparts Take an optional index parameter for a *subpart*. This is for the case where we save attachments from a message (in particular, when forwarding). We can't save them in the same directory for the (rare) case when there are multiple attachments with the same name. And we don't want to uniquify the name, since that shows up in e.g. the forwarded file name. This can be solved by saving each in their own indexed subdir. --- lib/message/mu-message.cc | 23 +++++++++++++++++------ lib/message/mu-message.hh | 5 ++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/message/mu-message.cc b/lib/message/mu-message.cc index 2d1d333e..facfc726 100644 --- a/lib/message/mu-message.cc +++ b/lib/message/mu-message.cc @@ -36,6 +36,7 @@ #include #include +#include #include #include "gmime/gmime-message.h" @@ -231,6 +232,7 @@ Message::load_mime_message(bool reload) const return false; } else { priv_->mime_msg = std::move(mime_msg.value()); + fill_document(*priv_); return true; } } @@ -784,20 +786,29 @@ Message::parts() const } Result -Message::cache_path() const +Message::cache_path(Option index) const { /* create tmpdir for this message, if needed */ if (priv_->cache_path.empty()) { GError *err{}; - auto tpath{to_string_opt_gchar( - g_dir_make_tmp("mu-message-part-XXXXXX", &err))}; + auto tpath{to_string_opt_gchar(g_dir_make_tmp("mu-cache-XXXXXX", &err))}; if (!tpath) - return Err(Error::Code::File, &err, - "failed to create temp dir"); + return Err(Error::Code::File, &err, "failed to create temp dir"); + priv_->cache_path = std::move(tpath.value()); } - return Ok(std::string{priv_->cache_path}); + if (index) { + GError *err{}; + auto tpath = format("%s/%zu", priv_->cache_path.c_str(), *index); + if (g_mkdir(tpath.c_str(), 0700) != 0) + return Err(Error::Code::File, &err, + "failed to create cache dir '%s'; err=%d", + tpath.c_str(), errno); + return Ok(std::move(tpath)); + } else + + return Ok(std::string{priv_->cache_path}); } diff --git a/lib/message/mu-message.hh b/lib/message/mu-message.hh index 9b60c61c..650a53e1 100644 --- a/lib/message/mu-message.hh +++ b/lib/message/mu-message.hh @@ -399,9 +399,12 @@ public: * Get the path to a cche directory for this message, which * is useful for temporarily saving attachments * + * @param index optionally, create / instead; + * this is useful for having part-specific subdirectories. + * * @return path to a (created) cache directory, or an error. */ - Result cache_path() const; + Result cache_path(Option index={}) const; /**