Skip to content

Commit

Permalink
Track and display libmagic errors (e.g. magic.mgc file missing)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlu5 committed May 22, 2017
1 parent fbbd70d commit 1b1761b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fileinsight.cpp
Expand Up @@ -88,11 +88,25 @@ void FileInsight::chooseFile()
}
}

// Display libmagic errors from the last call, if any. Also returns true if there was an error,
// and false otherwise.
bool FileInsight::getMagicError(magic_t magic_cookie)
{
QString error_text = magic_error(magic_cookie);
if (!error_text.isEmpty()) {
QMessageBox::critical(this, tr("libmagic error"),
tr("The libmagic backend encountered an error: ") + error_text);
return true;
}
return false;
}

QString FileInsight::getMagicInfo(QString filename)
{
// Tell libmagic to open the filename - it will return a string describing the file.
QString magic_output = magic_file(this->magic_cookie, this->QStringToConstChar(filename));
qDebug() << "Got libmagic output: " << magic_output;
getMagicError(this->magic_cookie);
return magic_output;
}

Expand Down Expand Up @@ -159,6 +173,7 @@ QString FileInsight::getMimeType(QString filename)
} else {
// libmagic MIME type backend
mimetype = magic_file(this->magic_cookie_mime, this->QStringToConstChar(filename));
getMagicError(this->magic_cookie_mime);
}
return mimetype;
}
Expand Down
2 changes: 2 additions & 0 deletions fileinsight.h
Expand Up @@ -70,6 +70,8 @@ protected slots:

void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);

bool getMagicError(magic_t magic_cookie);
};

#endif // FILEINSIGHT_H

0 comments on commit 1b1761b

Please sign in to comment.