lib: use join_paths

Convert some manual concatenation to the new join_paths helper.
This commit is contained in:
Dirk-Jan C. Binnema 2023-01-28 22:09:05 +02:00
parent 25635c5cfe
commit a8daec9598
4 changed files with 24 additions and 35 deletions

View File

@ -33,6 +33,7 @@
#include <glib.h>
#include "utils/mu-utils.hh"
#include "utils/mu-utils-file.hh"
#include "utils/mu-error.hh"
using namespace Mu;
@ -44,10 +45,7 @@ struct Scanner::Private {
if (!handler_)
throw Mu::Error{Error::Code::Internal, "missing handler"};
}
~Private()
{
stop();
}
~Private() { stop(); }
bool start();
bool stop();
@ -67,9 +65,6 @@ is_dotdir(const char *d_name)
if (d_name[0] == '\0' || (d_name[1] == '\0' && d_name[0] == '.') ||
(d_name[2] == '\0' && d_name[0] == '.' && d_name[1] == '.'))
return true;
/* gnus? */
if (d_name[0] == '.' && g_strcmp0(d_name + 1, "nnmaildir") == 0)
return true;
return false;
}
@ -97,7 +92,7 @@ Scanner::Private::process_dentry(const std::string& path, struct dirent *dentry,
return true; // ignore
}
const auto fullpath{path + "/" + d_name};
const auto fullpath{join_paths(path, d_name)};
struct stat statbuf {};
if (::stat(fullpath.c_str(), &statbuf) != 0) {
g_warning("failed to stat %s: %s", fullpath.c_str(), g_strerror(errno));

View File

@ -73,7 +73,7 @@ Mu::message_file_parts(const std::string& file)
Mu::Result<DirFile>
Mu::base_message_dir_file(const std::string& path)
{
constexpr auto newdir{ G_DIR_SEPARATOR_S "new"};
constexpr auto newdir{"/new"};
char *dirname{g_path_get_dirname(path.c_str())};
bool is_new{!!g_str_has_suffix(dirname, newdir)};

View File

@ -74,7 +74,7 @@ create_maildir(const std::string& path, mode_t mode)
std::array<std::string,3> subdirs = {"new", "cur", "tmp"};
for (auto&& subdir: subdirs) {
const auto fullpath{path + G_DIR_SEPARATOR_S + subdir};
const auto fullpath{join_paths(path, subdir)};
/* if subdir already exists, don't try to re-create
* it */
@ -98,7 +98,7 @@ create_maildir(const std::string& path, mode_t mode)
static Mu::Result<void> /* create a noindex file if requested */
create_noindex(const std::string& path)
{
const auto noindexpath{path + G_DIR_SEPARATOR_S MU_MAILDIR_NOINDEX_FILE};
const auto noindexpath{join_paths(path, MU_MAILDIR_NOINDEX_FILE)};
/* note, if the 'close' failed, creation may still have succeeded...*/
int fd = ::creat(noindexpath.c_str(), 0644);
@ -163,18 +163,15 @@ get_target_fullpath(const std::string& src, const std::string& targetpath,
*/
std::string fulltargetpath;
if (unique_names)
fulltargetpath = format("%s%c%s%c%u_%s",
targetpath.c_str(),
G_DIR_SEPARATOR, in_cur ? "cur" : "new",
G_DIR_SEPARATOR,
g_str_hash(src.c_str()),
srcfile);
fulltargetpath = join_paths(targetpath,
in_cur ? "cur" : "new",
format("%u_%s",
g_str_hash(src.c_str()),
srcfile));
else
fulltargetpath = format("%s%c%s%c%s",
targetpath.c_str(),
G_DIR_SEPARATOR, in_cur ? "cur" : "new",
G_DIR_SEPARATOR,
srcfile);
fulltargetpath = join_paths(targetpath,
in_cur ? "cur" : "new",
srcfile);
g_free(srcfile);
return fulltargetpath;
@ -213,8 +210,7 @@ clear_links(const std::string& path, DIR* dir)
if (dentry->d_name[0] == '.')
continue; /* ignore .,.. other dotdirs */
const auto fullpath{
format("%s" G_DIR_SEPARATOR_S "%s",path.c_str(), dentry->d_name)};
const auto fullpath{join_paths(path, dentry->d_name)};
const auto d_type = get_dtype(dentry, fullpath.c_str(), true/*lstat*/);
switch(d_type) {
case DT_LNK:
@ -441,8 +437,8 @@ Mu::maildir_determine_target(const std::string& old_path,
/* if target_mdir is empty, the src_dir does not change (though cur/
* maybe become new or vice-versa) */
const auto dst_mdir{target_maildir.empty() ? src_mdir :
root_maildir_path + target_maildir};
const auto dst_mdir = target_maildir.empty() ? src_mdir :
join_paths(root_maildir_path, target_maildir);
/* now calculate the message name (incl. its immediate parent dir) */
const auto dst_file{determine_dst_filename(src_file, newflags, new_name)};
@ -455,5 +451,5 @@ Mu::maildir_determine_target(const std::string& old_path,
return "new";
});
return dst_mdir + G_DIR_SEPARATOR_S + subdir + G_DIR_SEPARATOR_S + dst_file;
return join_paths(dst_mdir, subdir,dst_file);
}

View File

@ -153,11 +153,9 @@ Mu::runtime_path(Mu::RuntimePath path, const std::string& muhome)
{
auto [mu_cache, mu_config] =
std::invoke([&]()->std::pair<std::string, std::string> {
static std::string mu{"/mu"};
if (muhome.empty())
return { g_get_user_cache_dir() + mu,
g_get_user_config_dir() + mu };
return { join_paths(g_get_user_cache_dir(), "mu"),
join_paths(g_get_user_config_dir(), "mu")};
else
return { muhome, muhome };
});
@ -166,15 +164,15 @@ Mu::runtime_path(Mu::RuntimePath path, const std::string& muhome)
case Mu::RuntimePath::Cache:
return mu_cache;
case Mu::RuntimePath::XapianDb:
return mu_cache + "/xapian";
return join_paths(mu_cache, "xapian");
case Mu::RuntimePath::LogFile:
return mu_cache + "/mu.log";
return join_paths(mu_cache, "mu.log");
case Mu::RuntimePath::Bookmarks:
return mu_config + "/bookmarks";
return join_paths(mu_config, "bookmarks");
case Mu::RuntimePath::Config:
return mu_config;
case Mu::RuntimePath::Scripts:
return mu_config + "/scripts";
return join_paths(mu_config, "scripts");
default:
throw std::logic_error("unknown path");
}