summaryrefslogtreecommitdiff
path: root/cabinet
diff options
context:
space:
mode:
Diffstat (limited to 'cabinet')
-rw-r--r--cabinet/magic.cpp12
-rw-r--r--cabinet/magic.hpp4
-rw-r--r--cabinet/main.cpp23
3 files changed, 35 insertions, 4 deletions
diff --git a/cabinet/magic.cpp b/cabinet/magic.cpp
index 96e212f..b5c1aaa 100644
--- a/cabinet/magic.cpp
+++ b/cabinet/magic.cpp
@@ -4,6 +4,8 @@
#include <cerrno>
#include <expected>
+#include <filesystem>
+#include <string>
#include <string_view>
#include <system_error>
#include <utility>
@@ -51,6 +53,16 @@ namespace cab
return *this;
}
+ auto magic::process(std::filesystem::path path) -> std::expected<std::string, std::string>
+ {
+ auto result = ::magic_file(m_cookie, path.native().c_str());
+ 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 25cf60b..222d488 100644
--- a/cabinet/magic.hpp
+++ b/cabinet/magic.hpp
@@ -4,6 +4,8 @@
#include <magic.h>
#include <expected>
+#include <filesystem>
+#include <string>
#include <string_view>
#include <system_error>
#include <utility>
@@ -52,6 +54,8 @@ namespace cab
auto operator=(magic const &) -> magic & = delete;
auto operator=(magic && other) noexcept -> magic &;
+ auto process(std::filesystem::path path) -> std::expected<std::string, std::string>;
+
private:
explicit magic(::magic_t cookie) noexcept;
diff --git a/cabinet/main.cpp b/cabinet/main.cpp
index bec0a5c..77a73ca 100644
--- a/cabinet/main.cpp
+++ b/cabinet/main.cpp
@@ -1,18 +1,33 @@
#include <cabinet/magic.hpp>
#include <cstdlib>
+#include <filesystem>
#include <iostream>
#include <print>
-auto main() -> int
+using flags = cab::magic::flags;
+
+auto main(int argc, char ** argv) -> int
{
- auto magic = cab::magic::open(cab::magic::flags::print_debug | //
- cab::magic::flags::follow_symlinks | //
- cab::magic::flags::mime);
+ if (argc != 2)
+ {
+ std::println(std::cerr, "Expected a filename");
+ return EXIT_FAILURE;
+ }
+ auto magic = cab::magic::open(flags::follow_symlinks | flags::mime_type);
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]});
+ 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());
}