-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlibmagicbackend.cpp
More file actions
55 lines (46 loc) · 1.97 KB
/
libmagicbackend.cpp
File metadata and controls
55 lines (46 loc) · 1.97 KB
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
#include "libmagicbackend.h"
LibmagicBackend::LibmagicBackend(QWidget* parent) : FileInsightBackend(parent)
{
/* Initialize libmagic by fetching ourselves special cookies from magic_open() - this is
* similar to fetching a specific instance of a class. Libmagic's API is documented here:
* https://linux.die.net/man/3/libmagic
*/
this->magic_cookie = magic_open(MAGIC_CHECK | MAGIC_COMPRESS);
qDebug() << "libmagic cookie: " << this->magic_cookie;
// Tell libmagic to load the default file type definition
magic_load(this->magic_cookie, nullptr);
// Repeat the above process for a second instance of libmagic, specifically used to find MIME
// types. (set MAGIC_MIME_TYPE)
this->magic_cookie_mime = magic_open(MAGIC_CHECK | MAGIC_MIME_TYPE);
qDebug() << "libmagic cookie_mime: " << this->magic_cookie_mime;
magic_load(this->magic_cookie_mime, nullptr);
}
// Display libmagic errors from the last call, if any. Returns true if there was an error,
// and false otherwise.
bool LibmagicBackend::getError(magic_t magic_cookie)
{
QString error_text = magic_error(magic_cookie);
if (!error_text.isEmpty()) {
QMessageBox::critical(parent, tr("libmagic error"),
tr("The libmagic backend encountered an error: ") + error_text);
return true;
}
return false;
}
QString LibmagicBackend::getMimeType(QString filename) {
const char* buf = FileInsightUtils::QStringToConstChar(filename);
QString mimetype = magic_file(this->magic_cookie_mime, buf);
getError(this->magic_cookie_mime);
delete buf;
return mimetype;
}
// Gets file type output from libmagic
QString LibmagicBackend::getExtendedInfo(QString filename)
{
const char* buf = FileInsightUtils::QStringToConstChar(filename);
QString magic_output = magic_file(this->magic_cookie, buf);
qDebug() << "Got libmagic output: " << magic_output;
getError(this->magic_cookie);
delete buf;
return magic_output;
}