summaryrefslogtreecommitdiff
path: root/cabinet/magic.cpp
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2026-06-05 09:05:03 +0200
committerFelix Morgner <felix.morgner@gmail.com>2026-06-05 09:05:03 +0200
commitc1c317014002311874006f319b04121c88ef4672 (patch)
tree0c577bd0d09fa66abd45b6585a9442480219d000 /cabinet/magic.cpp
parent81fa478a642b25715adecf27c283e6f15334d705 (diff)
downloadcabinet-c1c317014002311874006f319b04121c88ef4672.tar.xz
cabinet-c1c317014002311874006f319b04121c88ef4672.zip
magic: improve error handling
Diffstat (limited to 'cabinet/magic.cpp')
-rw-r--r--cabinet/magic.cpp29
1 files changed, 20 insertions, 9 deletions
diff --git a/cabinet/magic.cpp b/cabinet/magic.cpp
index 08b3046..96b78a3 100644
--- a/cabinet/magic.cpp
+++ b/cabinet/magic.cpp
@@ -53,24 +53,35 @@ namespace cab
return *this;
}
- auto magic::process(std::filesystem::path path) -> std::expected<std::string, std::string>
+ auto magic::process(std::filesystem::path path) -> std::expected<std::string, std::error_code>
{
- auto result = ::magic_file(m_cookie, path.native().c_str());
- if (!result)
+ return handle_result(::magic_file(m_cookie, path.native().c_str()));
+ }
+
+ auto magic::process(int file_descriptor) -> std::expected<std::string, std::error_code>
+ {
+ if (file_descriptor < 0)
{
- return std::unexpected{std::string{::magic_error(m_cookie)}};
+ return std::unexpected{std::make_error_code(std::errc::invalid_argument)};
}
- return result;
+
+ return handle_result(::magic_descriptor(m_cookie, file_descriptor));
}
- auto magic::process(int file_descriptor) -> std::expected<std::string, std::string>
+ auto magic::handle_result(char const * result) -> std::expected<std::string, std::error_code>
{
- auto result = ::magic_descriptor(m_cookie, file_descriptor);
+ auto errno_copy = errno;
+
if (!result)
{
- return std::unexpected{std::string{::magic_error(m_cookie)}};
+ auto magic_errno = ::magic_errno(m_cookie);
+ if (!magic_errno && !errno)
+ {
+ return std::unexpected{std::make_error_code(std::errc::invalid_argument)};
+ }
}
- return result;
+
+ return std::string{result};
}
auto swap(magic & lhs, magic & rhs) noexcept -> void