From 4b9c663ded6df708380e82360452dedd48629ba0 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 28 Apr 2022 22:48:35 +0300 Subject: [PATCH] mu-maildir: rework mu_maildir_from_path Take a 'root' parameter --- lib/mu-maildir.cc | 33 ++++++++++++++++++++++++++++++++- lib/mu-maildir.hh | 7 ++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/mu-maildir.cc b/lib/mu-maildir.cc index 9ca0d1e8..b363906b 100644 --- a/lib/mu-maildir.cc +++ b/lib/mu-maildir.cc @@ -19,7 +19,6 @@ #include "config.h" -#include #include #include #include @@ -246,6 +245,38 @@ Mu::mu_maildir_clear_links(const std::string& path) } + +Result +Mu::mu_maildir_from_path(const std::string& path, const std::string& root) +{ + const auto pos = path.find(root); + if (pos != 0 || path[root.length()] != '/') + return Err(Error{Error::Code::InvalidArgument, + "root '%s' is not a root for path '%s'", + root.c_str(), path.c_str()}); + + auto mdir{path.substr(root.length())}; + auto slash{mdir.rfind('/')}; + + if (G_UNLIKELY(slash == std::string::npos) || slash < 4) + return Err(Error{Error::Code::InvalidArgument, + "invalid path: %s", path.c_str()}); + mdir.erase(slash); + auto subdir = mdir.data() + slash - 4; + if (G_UNLIKELY(strncmp(subdir, "/cur", 4) != 0 && strncmp(subdir, "/new", 4))) + return Err(Error::Code::InvalidArgument, + "cannot find '/new' or '/cur' - invalid path: %s", + path.c_str()); + + if (mdir.length() == 4) + return "/"; + + mdir.erase(mdir.length() - 4); + + return Ok(std::move(mdir)); +} + + /* * The file-components, ie. * 1631819685.fb7b279bbb0a7b66.evergrey:2,RS diff --git a/lib/mu-maildir.hh b/lib/mu-maildir.hh index bce1d874..68fc4ed9 100644 --- a/lib/mu-maildir.hh +++ b/lib/mu-maildir.hh @@ -21,7 +21,6 @@ #define MU_MAILDIR_HH__ #include -#include #include #include @@ -85,13 +84,15 @@ Result mu_maildir_flags_from_path(const std::string& pathname); /** * get the maildir for a certain message path, ie, the path *before* - * cur/ or new/ + * cur/ or new/ and *after* the root. * * @param path path for some message + * @param root filesystem root for the maildir * * @return the maildir or an Error */ -Result mu_maildir_from_path(const std::string& path); +Result mu_maildir_from_path(const std::string& path, + const std::string& root); /** * Move a message file to another maildir. If the target file already exists, it