summaryrefslogtreecommitdiff
path: root/cabinet/magic.cpp
blob: 3c36881253269d8e17d8373e7bf24b1adb1013c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <cabinet/magic.hpp>

#include <magic.h>

#include <cerrno>
#include <cstddef>
#include <expected>
#include <filesystem>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>

namespace cab
{

  auto magic::open(magic::flags flags, std::string_view database) noexcept -> std::expected<magic, std::error_code>
  {
    auto cookie = magic_open(std::to_underlying(flags));
    if (!cookie)
    {
      return std::unexpected{
        std::error_code{errno, std::system_category()}
      };
    }

    if (auto success = magic_load(cookie, database.data()); success < 0)
    {
      return std::unexpected{std::make_error_code(std::errc::invalid_argument)};
    }

    return magic{cookie};
  }

  magic::magic(::magic_t cookie) noexcept
      : m_cookie{cookie}
  {}

  magic::magic(magic && other) noexcept
      : m_cookie{std::exchange(other.m_cookie, nullptr)}
  {}

  magic::~magic()
  {
    if (m_cookie)
    {
      magic_close(m_cookie);
    }
  }

  auto magic::operator=(magic && other) noexcept -> magic &
  {
    std::swap(m_cookie, other.m_cookie);
    return *this;
  }

  auto magic::process(std::filesystem::path path) -> std::expected<std::string, std::error_code>
  {
    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::make_error_code(std::errc::invalid_argument)};
    }

    return handle_result(::magic_descriptor(m_cookie, file_descriptor));
  }

  auto magic::process(std::span<std::byte> data) -> std::expected<std::string, std::error_code>
  {
    return handle_result(::magic_buffer(m_cookie, data.data(), data.size_bytes()));
  }

  auto magic::handle_result(char const * result) -> std::expected<std::string, std::error_code>
  {
    auto errno_copy = errno;

    if (!result)
    {
      auto magic_errno = ::magic_errno(m_cookie);
      if (!magic_errno && !errno)
      {
        return std::unexpected{std::make_error_code(std::errc::invalid_argument)};
      }
    }

    return std::string{result};
  }

  auto swap(magic & lhs, magic & rhs) noexcept -> void
  {
    auto temp = std::move(lhs);
    lhs = std::move(rhs);
    rhs = std::move(lhs);
  }

}  // namespace cab