summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cabinet/magic.cpp10
-rw-r--r--cabinet/magic.hpp1
-rw-r--r--cabinet/main.cpp9
3 files changed, 16 insertions, 4 deletions
diff --git a/cabinet/magic.cpp b/cabinet/magic.cpp
index b5c1aaa..08b3046 100644
--- a/cabinet/magic.cpp
+++ b/cabinet/magic.cpp
@@ -63,6 +63,16 @@ namespace cab
return result;
}
+ auto magic::process(int file_descriptor) -> std::expected<std::string, std::string>
+ {
+ auto result = ::magic_descriptor(m_cookie, file_descriptor);
+ if (!result)
+ {
+ return std::unexpected{std::string{::magic_error(m_cookie)}};
+ }
+ return result;
+ }
+
auto swap(magic & lhs, magic & rhs) noexcept -> void
{
auto temp = std::move(lhs);
diff --git a/cabinet/magic.hpp b/cabinet/magic.hpp
index 222d488..96f4b08 100644
--- a/cabinet/magic.hpp
+++ b/cabinet/magic.hpp
@@ -55,6 +55,7 @@ namespace cab
auto operator=(magic && other) noexcept -> magic &;
auto process(std::filesystem::path path) -> std::expected<std::string, std::string>;
+ auto process(int file_descriptor) -> std::expected<std::string, std::string>;
private:
explicit magic(::magic_t cookie) noexcept;
diff --git a/cabinet/main.cpp b/cabinet/main.cpp
index 77a73ca..db2d43b 100644
--- a/cabinet/main.cpp
+++ b/cabinet/main.cpp
@@ -1,7 +1,8 @@
#include <cabinet/magic.hpp>
+#include <fcntl.h>
+
#include <cstdlib>
-#include <filesystem>
#include <iostream>
#include <print>
@@ -15,19 +16,19 @@ auto main(int argc, char ** argv) -> int
return EXIT_FAILURE;
}
- auto magic = cab::magic::open(flags::follow_symlinks | flags::mime_type);
+ auto magic = cab::magic::open(flags::follow_symlinks | flags::mime_type | flags::forward_errors);
if (!magic)
{
std::println(std::cerr, "Failed to initialize libmagic: {}", magic.error().message());
return EXIT_FAILURE;
}
- auto mime_type = magic->process(std::filesystem::path{argv[1]});
+ auto fd = open(argv[1], O_RDONLY);
+ auto mime_type = magic->process(fd);
if (!mime_type)
{
std::println(std::cerr, "Failed to determine file type: {}", mime_type.error());
return EXIT_FAILURE;
}
-
std::println("{}: {}", argv[1], mime_type.value());
}