mu-maildir: rework mu_maildir_from_path

Take a 'root' parameter
This commit is contained in:
Dirk-Jan C. Binnema 2022-04-28 22:48:35 +03:00
parent 9dd1aaeef5
commit 4b9c663ded
2 changed files with 36 additions and 4 deletions

View File

@ -19,7 +19,6 @@
#include "config.h"
#include <optional>
#include <string>
#include <unistd.h>
#include <sys/types.h>
@ -246,6 +245,38 @@ Mu::mu_maildir_clear_links(const std::string& path)
}
Result<std::string>
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

View File

@ -21,7 +21,6 @@
#define MU_MAILDIR_HH__
#include <string>
#include <optional>
#include <utils/mu-result.hh>
#include <glib.h>
@ -85,13 +84,15 @@ Result<Flags> 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<std::string> mu_maildir_from_path(const std::string& path);
Result<std::string> 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