summaryrefslogtreecommitdiff
path: root/cabinet/magic.cpp
diff options
context:
space:
mode:
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