mirror of
https://github.com/audacity/linuxdeploy.git
synced 2026-06-29 06:43:57 -05:00
Add libmagic wrapper
Also builds separate static library linuxdeploy_util providing old util header and new magic wrapper.
This commit is contained in:
2
src/util/CMakeLists.txt
Normal file
2
src/util/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
add_library(linuxdeploy_util STATIC magicwrapper.cpp magicwrapper.h util.h)
|
||||
target_include_directories(linuxdeploy_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
49
src/util/magicwrapper.cpp
Normal file
49
src/util/magicwrapper.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// system includes
|
||||
#include <magic.h>
|
||||
#include <string>
|
||||
|
||||
// local includes
|
||||
#include "magicwrapper.h"
|
||||
|
||||
namespace linuxdeploy {
|
||||
namespace util {
|
||||
namespace magic {
|
||||
class MagicWrapper::PrivateData {
|
||||
private:
|
||||
magic_t cookie;
|
||||
|
||||
public:
|
||||
PrivateData() noexcept(false) {
|
||||
cookie = magic_open(MAGIC_DEBUG | MAGIC_SYMLINK | MAGIC_MIME_TYPE);
|
||||
|
||||
if (cookie == nullptr)
|
||||
throw MagicError("Failed to open magic database");
|
||||
}
|
||||
|
||||
~PrivateData() {
|
||||
if (cookie != nullptr) {
|
||||
magic_close(cookie);
|
||||
cookie = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
std::string fileType(const std::string& path) {
|
||||
const auto* buf = magic_file(cookie, path.c_str());
|
||||
|
||||
if (buf == nullptr)
|
||||
return "";
|
||||
|
||||
return buf;
|
||||
}
|
||||
};
|
||||
|
||||
MagicWrapper::MagicWrapper() {
|
||||
d = new PrivateData();
|
||||
}
|
||||
|
||||
MagicWrapper::~MagicWrapper() {
|
||||
delete d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/util/magicwrapper.h
Normal file
37
src/util/magicwrapper.h
Normal file
@@ -0,0 +1,37 @@
|
||||
// system includes
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace linuxdeploy {
|
||||
namespace util {
|
||||
namespace magic {
|
||||
// thrown by constructors if opening the magic database fails
|
||||
class MagicError : std::exception {
|
||||
private:
|
||||
const std::string message;
|
||||
|
||||
public:
|
||||
explicit MagicError() : message() {}
|
||||
|
||||
explicit MagicError(std::string message) : message(std::move(message)) {}
|
||||
|
||||
const char* what() {
|
||||
return message.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class MagicWrapper {
|
||||
private:
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
|
||||
public:
|
||||
MagicWrapper();
|
||||
~MagicWrapper();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user