update to use fmt-based apis
Not complete, but a first big stab converting users of Mu::Error and various g_warning & friends, format to the new libfmt-based APIs.
This commit is contained in:
@ -208,7 +208,7 @@ Document::contacts_value(Field::Id id) const noexcept
|
||||
|
||||
const auto ctype{contact_type_from_field_id(id)};
|
||||
if (G_UNLIKELY(!ctype)) {
|
||||
g_critical("invalid field-id for contact-type: <%zu>",
|
||||
mu_critical("invalid field-id for contact-type: <{}>",
|
||||
static_cast<size_t>(id));
|
||||
return {};
|
||||
}
|
||||
@ -217,7 +217,7 @@ Document::contacts_value(Field::Id id) const noexcept
|
||||
|
||||
const auto pos = s.find(SepaChar2);
|
||||
if (G_UNLIKELY(pos == std::string::npos)) {
|
||||
g_critical("invalid contact data '%s'", s.c_str());
|
||||
mu_critical("invalid contact data '{}'", s);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -348,7 +348,7 @@ Document::remove(Field::Id field_id)
|
||||
try {
|
||||
xdoc_.remove_term(term);
|
||||
} catch(const Xapian::InvalidArgumentError& xe) {
|
||||
g_critical("failed to remove '%s'", term.c_str());
|
||||
mu_critical("failed to remove '{}'", term);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -77,8 +77,7 @@ validate_field_shortcuts()
|
||||
#ifdef BUILD_TESTS
|
||||
if (shortcut != 0) {
|
||||
if (++no_dups[static_cast<size_t>(shortcut-'a')] > 1) {
|
||||
g_critical("shortcut '%c' is duplicated",
|
||||
shortcut);
|
||||
mu_critical("shortcut '{}' is duplicated", shortcut);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -105,7 +104,7 @@ validate_field_flags()
|
||||
++flagnum;
|
||||
|
||||
if (flagnum > 1) {
|
||||
//g_warning("invalid field %*.s", STR_V(field.name));
|
||||
//mu_warning("invalid field {}", field.name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,21 +27,19 @@ 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()});
|
||||
"root '{}' is not a root for path '{}'", root, path});
|
||||
|
||||
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()});
|
||||
"invalid path: {}", path});
|
||||
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());
|
||||
"cannot find '/new' or '/cur' - invalid path: {}", path);
|
||||
|
||||
if (mdir.length() == 4)
|
||||
return "/";
|
||||
@ -109,7 +107,7 @@ Mu::flags_from_path(const std::string& path)
|
||||
if (!flags) {
|
||||
/* LCOV_EXCL_START*/
|
||||
return Err(Error{Error::Code::InvalidArgument,
|
||||
"invalid flags ('%s')", parts.flags_suffix.c_str()});
|
||||
"invalid flags ('{}')", parts.flags_suffix});
|
||||
/* LCOV_EXCL_STOP*/
|
||||
}
|
||||
|
||||
@ -182,8 +180,8 @@ test_flags_from_path()
|
||||
assert_valid_result(res);
|
||||
/* LCOV_EXCL_START*/
|
||||
if (g_test_verbose()) {
|
||||
g_print("%s -> <%s>\n", tcase.first.c_str(),
|
||||
to_string(res.value()).c_str());
|
||||
mu_println("{} -> <{}>", tcase.first,
|
||||
to_string(res.value()));
|
||||
g_assert_true(res.value() == tcase.second);
|
||||
}
|
||||
/*LCOV_EXCL_STOP*/
|
||||
|
||||
@ -80,20 +80,18 @@ get_statbuf(const std::string& path, Message::Options opts = Message::Options::N
|
||||
{
|
||||
if (none_of(opts & Message::Options::AllowRelativePath) &&
|
||||
!g_path_is_absolute(path.c_str()))
|
||||
return Err(Error::Code::File, "path '%s' is not absolute",
|
||||
path.c_str());
|
||||
return Err(Error::Code::File, "path '{}' is not absolute", path);
|
||||
|
||||
if (::access(path.c_str(), R_OK) != 0)
|
||||
return Err(Error::Code::File, "file @ '%s' is not readable",
|
||||
path.c_str());
|
||||
return Err(Error::Code::File, "file @ '{}' is not readable", path);
|
||||
|
||||
struct stat statbuf{};
|
||||
if (::stat(path.c_str(), &statbuf) < 0)
|
||||
return Err(Error::Code::File, "cannot stat %s: %s", path.c_str(),
|
||||
g_strerror(errno));
|
||||
return Err(Error::Code::File, "cannot stat {}: {}", path,
|
||||
g_strerror(errno));
|
||||
|
||||
if (!S_ISREG(statbuf.st_mode))
|
||||
return Err(Error::Code::File, "not a regular file: %s", path.c_str());
|
||||
return Err(Error::Code::File, "not a regular file: {}", path);
|
||||
|
||||
return Ok(std::move(statbuf));
|
||||
}
|
||||
@ -200,13 +198,13 @@ Message::set_maildir(const std::string& maildir)
|
||||
maildir.at(0) != '/' ||
|
||||
(maildir.size() > 1 && maildir.at(maildir.length()-1) == '/'))
|
||||
return Err(Error::Code::Message,
|
||||
"'%s' is not a valid maildir", maildir.c_str());
|
||||
"'{}' is not a valid maildir", maildir.c_str());
|
||||
|
||||
const auto path{document().string_value(Field::Id::Path)};
|
||||
if (path == maildir || path.find(maildir) == std::string::npos)
|
||||
return Err(Error::Code::Message,
|
||||
"'%s' is not a valid maildir for message @ %s",
|
||||
maildir.c_str(), path.c_str());
|
||||
"'{}' is not a valid maildir for message @ {}",
|
||||
maildir, path);
|
||||
|
||||
priv_->doc.remove(Field::Id::Maildir);
|
||||
priv_->doc.add(Field::Id::Maildir, maildir);
|
||||
@ -229,8 +227,8 @@ Message::load_mime_message(bool reload) const
|
||||
|
||||
const auto path{document().string_value(Field::Id::Path)};
|
||||
if (auto mime_msg{MimeMessage::make_from_file(path)}; !mime_msg) {
|
||||
g_warning("failed to load '%s': %s",
|
||||
path.c_str(), mime_msg.error().what());
|
||||
mu_warning("failed to load '{}': {}",
|
||||
path, mime_msg.error().what());
|
||||
return false;
|
||||
} else {
|
||||
priv_->mime_msg = std::move(mime_msg.value());
|
||||
@ -258,17 +256,17 @@ get_priority(const MimeMessage& mime_msg)
|
||||
constexpr std::array<std::pair<std::string_view, Priority>, 10>
|
||||
prio_alist = {{
|
||||
{"high", Priority::High},
|
||||
{"1", Priority::High},
|
||||
{"2", Priority::High},
|
||||
{"1", Priority::High},
|
||||
{"2", Priority::High},
|
||||
|
||||
{"normal", Priority::Normal},
|
||||
{"3", Priority::Normal},
|
||||
{"3", Priority::Normal},
|
||||
|
||||
{"low", Priority::Low},
|
||||
{"low", Priority::Low},
|
||||
{"list", Priority::Low},
|
||||
{"bulk", Priority::Low},
|
||||
{"4", Priority::Low},
|
||||
{"5", Priority::Low}
|
||||
{"4", Priority::Low},
|
||||
{"5", Priority::Low}
|
||||
}};
|
||||
|
||||
const auto opt_str = mime_msg.header("Precedence")
|
||||
@ -440,14 +438,13 @@ handle_encrypted(const MimeMultipartEncrypted& part, Message::Private& info)
|
||||
const auto proto{part.content_type_parameter("protocol").value_or("unknown")};
|
||||
const auto ctx = MimeCryptoContext::make(proto);
|
||||
if (!ctx) {
|
||||
g_warning("failed to create context for protocol <%s>",
|
||||
proto.c_str());
|
||||
mu_warning("failed to create context for protocol <{}>", proto);
|
||||
return;
|
||||
}
|
||||
|
||||
auto res{part.decrypt(*ctx)};
|
||||
if (!res) {
|
||||
g_warning("failed to decrypt: %s", res.error().what());
|
||||
mu_warning("failed to decrypt: {}", res.error().what());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -538,9 +535,9 @@ process_message(const MimeMessage& mime_msg, const std::string& path,
|
||||
if (info.body_txt) { /* attempt to get the body-language */
|
||||
if (const auto lang{detect_language(info.body_txt.value())}; lang) {
|
||||
info.language = lang->code;
|
||||
g_debug("detected language: %s", lang->code);
|
||||
mu_debug("detected language: {}", lang->code);
|
||||
} else
|
||||
g_debug("could not detect language");
|
||||
mu_debug("could not detect language");
|
||||
}
|
||||
}
|
||||
|
||||
@ -551,8 +548,8 @@ calculate_sha256(const std::string& path)
|
||||
|
||||
FILE *file{::fopen(path.c_str(), "r")};
|
||||
if (!file)
|
||||
return Err(Error{Error::Code::File, "failed to open %s: %s",
|
||||
path.c_str(), ::strerror(errno)});
|
||||
return Err(Error{Error::Code::File, "failed to open {}: {}",
|
||||
path, ::strerror(errno)});
|
||||
|
||||
std::array<uint8_t, 4096> buf{};
|
||||
while (true) {
|
||||
@ -566,7 +563,7 @@ calculate_sha256(const std::string& path)
|
||||
::fclose(file);
|
||||
|
||||
if (has_err)
|
||||
return Err(Error{Error::Code::File, "failed to read %s", path.c_str()});
|
||||
return Err(Error{Error::Code::File, "failed to read {}", path});
|
||||
|
||||
return Ok(g_checksum_get_string(checksum));
|
||||
}
|
||||
@ -585,18 +582,19 @@ fake_message_id(const std::string& path)
|
||||
|
||||
// not a very good message-id, only for testing.
|
||||
if (path.empty() || ::access(path.c_str(), R_OK) != 0)
|
||||
return format("%08x%s", g_str_hash(path.c_str()), mu_suffix);
|
||||
return mu_format("{:08x}{}", g_str_hash(path.c_str()), mu_suffix);
|
||||
if (const auto sha256_res{calculate_sha256(path)}; !sha256_res)
|
||||
return format("%08x%s", g_str_hash(path.c_str()), mu_suffix);
|
||||
return mu_format("{:08x}{}", g_str_hash(path.c_str()), mu_suffix);
|
||||
else
|
||||
return format("%s%s", sha256_res.value().c_str(), mu_suffix);
|
||||
return mu_format("{}{}", sha256_res.value(), mu_suffix);
|
||||
}
|
||||
|
||||
/* many of the doc.add(fiels ....) automatically update the sexp-list as well;
|
||||
/* many of the doc.add(fields ....) automatically update the sexp-list as well;
|
||||
* however, there are some _extra_ values in the sexp-list that are not
|
||||
* based on a field. So we add them here.
|
||||
*/
|
||||
|
||||
|
||||
static void
|
||||
doc_add_list_post(Document& doc, const MimeMessage& mime_msg)
|
||||
{
|
||||
@ -797,11 +795,10 @@ Message::cache_path(Option<size_t> index) const
|
||||
|
||||
if (index) {
|
||||
GError *err{};
|
||||
auto tpath = format("%s/%zu", priv_->cache_path.c_str(), *index);
|
||||
auto tpath = mu_format("{}/{}", priv_->cache_path, *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);
|
||||
"failed to create cache dir '{}'; err={}", tpath, errno);
|
||||
return Ok(std::move(tpath));
|
||||
} else
|
||||
|
||||
|
||||
@ -49,7 +49,7 @@ Mu::init_gmime(void)
|
||||
if (gmime_initialized)
|
||||
return; // already
|
||||
|
||||
g_debug("initializing gmime %u.%u.%u",
|
||||
mu_debug("initializing gmime {}.{}.{}",
|
||||
gmime_major_version,
|
||||
gmime_minor_version,
|
||||
gmime_micro_version);
|
||||
@ -58,7 +58,7 @@ Mu::init_gmime(void)
|
||||
gmime_initialized = true;
|
||||
|
||||
std::atexit([] {
|
||||
g_debug("shutting down gmime");
|
||||
mu_debug("shutting down gmime");
|
||||
g_mime_shutdown();
|
||||
gmime_initialized = false;
|
||||
});
|
||||
@ -146,7 +146,7 @@ MimeObject::to_file(const std::string& path, bool overwrite) const noexcept
|
||||
S_IRUSR|S_IWUSR,
|
||||
&err)};
|
||||
if (!strm)
|
||||
return Err(Error::Code::File, &err, "failed to open '%s'", path.c_str());
|
||||
return Err(Error::Code::File, &err, "failed to open '{}'", path);
|
||||
|
||||
MimeStream stream{MimeStream::make_from_stream(strm)};
|
||||
return write_to_stream({}, stream);
|
||||
@ -158,14 +158,14 @@ MimeObject::to_string_opt() const noexcept
|
||||
{
|
||||
auto stream{MimeStream::make_mem()};
|
||||
if (!stream) {
|
||||
g_warning("failed to create mem stream");
|
||||
mu_warning("failed to create mem stream");
|
||||
return Nothing;
|
||||
}
|
||||
|
||||
const auto written = g_mime_object_write_to_stream(
|
||||
self(), {}, GMIME_STREAM(stream.object()));
|
||||
if (written < 0) {
|
||||
g_warning("failed to write object to stream");
|
||||
mu_warning("failed to write object to stream");
|
||||
return Nothing;
|
||||
}
|
||||
|
||||
@ -246,16 +246,15 @@ MimeCryptoContext::setup_gpg_test(const std::string& testpath)
|
||||
|
||||
if (g_mkdir_with_parents((testpath + "/.gnupg").c_str(), 0700) != 0)
|
||||
return Err(Error::Code::File,
|
||||
"failed to create gnupg dir; err=%d", errno);
|
||||
"failed to create gnupg dir; err={}", errno);
|
||||
|
||||
auto write_gpgfile=[&](const std::string& fname, const std::string& data)
|
||||
-> Result<void> {
|
||||
|
||||
GError *err{};
|
||||
std::string path{format("%s/%s", testpath.c_str(), fname.c_str())};
|
||||
std::string path{mu_format("{}/{}", testpath, fname)};
|
||||
if (!g_file_set_contents(path.c_str(), data.c_str(), data.size(), &err))
|
||||
return Err(Error::Code::File, &err,
|
||||
"failed to write %s", path.c_str());
|
||||
return Err(Error::Code::File, &err, "failed to write {}", path);
|
||||
else
|
||||
return Ok();
|
||||
};
|
||||
@ -303,7 +302,7 @@ MimeMessage::make_from_file(const std::string& path)
|
||||
init_gmime();
|
||||
if (auto&& stream{g_mime_stream_file_open(path.c_str(), "r", &err)}; !stream)
|
||||
return Err(Error::Code::Message, &err,
|
||||
"failed to open stream for %s", path.c_str());
|
||||
"failed to open stream for {}", path);
|
||||
else
|
||||
return make_from_stream(std::move(stream));
|
||||
}
|
||||
@ -480,13 +479,13 @@ MimePart::size() const noexcept
|
||||
{
|
||||
auto wrapper{g_mime_part_get_content(self())};
|
||||
if (!wrapper) {
|
||||
g_warning("failed to get content wrapper");
|
||||
mu_warning("failed to get content wrapper");
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto stream{g_mime_data_wrapper_get_stream(wrapper)};
|
||||
if (!stream) {
|
||||
g_warning("failed to get stream");
|
||||
mu_warning("failed to get stream");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -510,13 +509,13 @@ MimePart::to_string() const noexcept
|
||||
*/
|
||||
GMimeDataWrapper *wrapper{g_mime_part_get_content(self())};
|
||||
if (!wrapper) { /* this happens with invalid mails */
|
||||
g_debug("failed to create data wrapper");
|
||||
mu_warning("failed to create data wrapper");
|
||||
return Nothing;
|
||||
}
|
||||
|
||||
GMimeStream *stream{g_mime_stream_mem_new()};
|
||||
if (!stream) {
|
||||
g_warning("failed to create mem stream");
|
||||
mu_warning("failed to create mem stream");
|
||||
return Nothing;
|
||||
}
|
||||
|
||||
@ -554,7 +553,7 @@ MimePart::to_file(const std::string& path, bool overwrite) const noexcept
|
||||
S_IRUSR|S_IWUSR,
|
||||
&err)};
|
||||
if (!strm)
|
||||
return Err(Error::Code::File, &err, "failed to open '%s'", path.c_str());
|
||||
return Err(Error::Code::File, &err, "failed to open '{}'", path);
|
||||
|
||||
MimeStream stream{MimeStream::make_from_stream(strm)};
|
||||
ssize_t written{g_mime_data_wrapper_write_to_stream(
|
||||
@ -563,7 +562,7 @@ MimePart::to_file(const std::string& path, bool overwrite) const noexcept
|
||||
|
||||
if (written < 0) {
|
||||
return Err(Error::Code::File, &err,
|
||||
"failed to write to '%s'", path.c_str());
|
||||
"failed to write to '{}'", path);
|
||||
}
|
||||
|
||||
return Ok(static_cast<size_t>(written));
|
||||
@ -635,7 +634,7 @@ MimeMultipartSigned::verify(const MimeCryptoContext& ctx, VerifyFlags vflags) co
|
||||
const auto sign_proto{ctx.signature_protocol()};
|
||||
|
||||
if (!proto || !sign_proto || !mime_types_equal(*proto, *sign_proto))
|
||||
return Err(Error::Code::Crypto, "unsupported protocol " +
|
||||
return Err(Error::Code::Crypto, "unsupported protocol {}",
|
||||
proto.value_or("<unknown>"));
|
||||
|
||||
const auto sig{signed_signature_part()};
|
||||
@ -734,7 +733,7 @@ MimeMultipartEncrypted::decrypt(const MimeCryptoContext& ctx, DecryptFlags dflag
|
||||
const auto enc_proto{ctx.encryption_protocol()};
|
||||
|
||||
if (!proto || !enc_proto || !mime_types_equal(*proto, *enc_proto))
|
||||
return Err(Error::Code::Crypto, "unsupported protocol " +
|
||||
return Err(Error::Code::Crypto, "unsupported protocol {}",
|
||||
proto.value_or("<unknown>"));
|
||||
|
||||
const auto version{encrypted_version_part()};
|
||||
@ -744,14 +743,14 @@ MimeMultipartEncrypted::decrypt(const MimeCryptoContext& ctx, DecryptFlags dflag
|
||||
|
||||
if (!mime_types_equal(version->mime_type().value_or(""), proto.value()))
|
||||
return Err(Error::Code::Crypto,
|
||||
"cannot decrypt; unexpected version content-type '%s' != '%s'",
|
||||
version->mime_type().value_or("").c_str(),
|
||||
proto.value().c_str());
|
||||
"cannot decrypt; unexpected version content-type '{}' != '{}'",
|
||||
version->mime_type().value_or(""), proto.value());
|
||||
|
||||
if (!mime_types_equal(encrypted->mime_type().value_or(""), "application/octet-stream"))
|
||||
if (!mime_types_equal(encrypted->mime_type().value_or(""),
|
||||
"application/octet-stream"))
|
||||
return Err(Error::Code::Crypto,
|
||||
"cannot decrypt; unexpected encrypted content-type '%s'",
|
||||
encrypted->mime_type().value_or("").c_str());
|
||||
"cannot decrypt; unexpected encrypted content-type '{}'",
|
||||
encrypted->mime_type().value_or(""));
|
||||
|
||||
const auto content{encrypted->content()};
|
||||
auto ciphertext{MimeStream::make_mem()};
|
||||
|
||||
@ -674,7 +674,8 @@ struct MimeCryptoContext : public Object {
|
||||
make(const std::string& protocol) {
|
||||
auto ctx = g_mime_crypto_context_new(protocol.c_str());
|
||||
if (!ctx)
|
||||
return Err(Error::Code::Crypto, "unsupported protocol " + protocol);
|
||||
return Err(Error::Code::Crypto,
|
||||
"unsupported protocol {}", protocol);
|
||||
MimeCryptoContext mctx{ctx};
|
||||
mctx.unref(); /* remove extra ref */
|
||||
return Ok(std::move(mctx));
|
||||
|
||||
@ -369,7 +369,7 @@ Q46aYjxe0As6AP90bcAZ3dcn5RcTJaM0UhZssguawZ+tnriD3+5DPkMMCg==
|
||||
const auto mpart{MimeMultipartSigned(mobj)};
|
||||
const auto sigs{mpart.verify(*ctx)};
|
||||
if (!sigs)
|
||||
g_warning("%s", sigs.error().what());
|
||||
mu_warning("{}", sigs.error().what());
|
||||
|
||||
g_assert_true(!!sigs);
|
||||
g_assert_cmpuint(sigs->size(), ==, 1);
|
||||
|
||||
Reference in New Issue
Block a user