From c8568eecd41d924f61b5e4d568db0439fcb3416d Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 10 Sep 2023 10:10:57 +0300 Subject: [PATCH] utils/file: add basename/dirname helpers and use them --- lib/message/mu-message-file.cc | 19 ++++++------------- lib/message/mu-message-part.cc | 4 ++-- lib/mu-maildir.cc | 2 +- lib/utils/mu-utils-file.cc | 12 ++++++++++++ lib/utils/mu-utils-file.hh | 22 ++++++++++++++++++++++ 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/message/mu-message-file.cc b/lib/message/mu-message-file.cc index 15e12cf8..b077c3bb 100644 --- a/lib/message/mu-message-file.cc +++ b/lib/message/mu-message-file.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2022 Dirk-Jan C. Binnema +** Copyright (C) 2022-2023 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -18,6 +18,7 @@ */ #include "mu-message-file.hh" +#include "utils/mu-utils-file.hh" using namespace Mu; @@ -73,17 +74,11 @@ Mu::base_message_dir_file(const std::string& path) { constexpr auto newdir{"/new"}; - char *dirname{g_path_get_dirname(path.c_str())}; - bool is_new{!!g_str_has_suffix(dirname, newdir)}; + const auto dname{dirname(path)}; + bool is_new{!!g_str_has_suffix(dname.c_str(), newdir)}; - std::string mdir{dirname, ::strlen(dirname) - 4}; - g_free(dirname); - - char *basename{g_path_get_basename(path.c_str())}; - std::string bname{basename}; - g_free(basename); - - return Ok(DirFile{std::move(mdir), std::move(bname), is_new}); + std::string mdir{dname.substr(0, dname.size() - 4)}; + return Ok(DirFile{std::move(mdir), basename(path), is_new}); } Mu::Result @@ -116,8 +111,6 @@ Mu::flags_from_path(const std::string& path) } - - #ifdef BUILD_TESTS #include "utils/mu-test-utils.hh" diff --git a/lib/message/mu-message-part.cc b/lib/message/mu-message-part.cc index 73316cb5..d1c7ac56 100644 --- a/lib/message/mu-message-part.cc +++ b/lib/message/mu-message-part.cc @@ -19,9 +19,9 @@ #include "mu-message-part.hh" -#include "glibconfig.h" #include "mu-mime-object.hh" #include "utils/mu-utils.hh" +#include "utils/mu-utils-file.hh" #include using namespace Mu; @@ -48,7 +48,7 @@ cook(const std::string& fname, const std::vector& forbidden) std::string clean; clean.reserve(fname.length()); - for (auto& c: to_string_gchar(g_path_get_basename(fname.c_str()))) + for (auto& c: basename(fname)) if (seq_some(forbidden,[&](char fc){return ::iscntrl(c) || c == fc;})) clean += '-'; else diff --git a/lib/mu-maildir.cc b/lib/mu-maildir.cc index 6fd84d96..ad9f89b4 100644 --- a/lib/mu-maildir.cc +++ b/lib/mu-maildir.cc @@ -155,7 +155,7 @@ get_target_fullpath(const std::string& src, const std::string& targetpath, if (auto&& res = check_subdir(src, in_cur); !res) return Err(std::move(res.error())); - const auto srcfile{to_string_gchar(g_path_get_basename(src.c_str()))}; + const auto srcfile{basename(src)}; /* create target-path; note: make the filename *cough* unique by * including a hash of the srcname in the targetname. This helps if diff --git a/lib/utils/mu-utils-file.cc b/lib/utils/mu-utils-file.cc index b8966f77..419aca83 100644 --- a/lib/utils/mu-utils-file.cc +++ b/lib/utils/mu-utils-file.cc @@ -151,6 +151,18 @@ Mu::canonicalize_filename(const std::string& path, const std::string& relative_t return str; } +std::string +Mu::basename(const std::string& path) +{ + return to_string_gchar(g_path_get_basename(path.c_str())); +} + +std::string +Mu::dirname(const std::string& path) +{ + return to_string_gchar(g_path_get_dirname(path.c_str())); +} + Result Mu::make_temp_dir() { diff --git a/lib/utils/mu-utils-file.hh b/lib/utils/mu-utils-file.hh index 8edebcbc..fd889e53 100644 --- a/lib/utils/mu-utils-file.hh +++ b/lib/utils/mu-utils-file.hh @@ -85,6 +85,28 @@ std::string canonicalize_filename(const std::string& path, const std::string& re Result expand_path(const std::string& str); +/** + * Get the basename for path, i.e. without leading directory component, + * @see g_path_get_basename + * + * @param path + * + * @return the basename + */ +std::string basename(const std::string& path); + + +/** + * Get the dirname for path, i.e. without leading directory component, + * @see g_path_get_dirname + * + * @param path + * + * @return the dirname + */ +std::string dirname(const std::string& path); + + /* * for OSs with out support for direntry->d_type, like Solaris */