xdg-mime replaces "file" as default MIME identifying command. "file" kept as last resource fallback

This commit is contained in:
Jean-Francois Dockes 2016-02-26 08:49:13 +01:00
parent bd31c1dca4
commit 07e20eef4b
2 changed files with 26 additions and 9 deletions

View file

@ -44,7 +44,8 @@ using namespace std;
/// So we first call the internal file identifier, which currently /// So we first call the internal file identifier, which currently
/// only knows about mail, but in which we can add the more /// only knows about mail, but in which we can add the more
/// current/interesting file types. /// current/interesting file types.
/// As a last resort we execute 'file' (except if forbidden by config) /// As a last resort we execute 'file' or its configured replacement
/// (except if forbidden by config)
static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc) static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc)
{ {
@ -54,20 +55,35 @@ static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc)
#ifdef USE_SYSTEM_FILE_COMMAND #ifdef USE_SYSTEM_FILE_COMMAND
if (usfc && mime.empty()) { if (usfc && mime.empty()) {
// Last resort: use "file -i", or its configured replacement. // Last resort: use "file -i", or its configured replacement.
vector<string> cmd = create_vector<string>(FILE_PROG) ("-i") (fn);
// 'file' fallback if the configured command (default:
// xdg-mime) is not found
static const vector<string> tradfilecmd =
create_vector<string>(FILE_PROG) ("-i") (fn);
vector<string> cmd;
string scommand; string scommand;
if (cfg->getConfParam("systemfilecommand", scommand)) { if (cfg->getConfParam("systemfilecommand", scommand)) {
stringToStrings(scommand, cmd); stringToStrings(scommand, cmd);
string exe;
if (cmd.empty()) {
cmd = tradfilecmd;
} else if (!ExecCmd::which(cmd[0], exe)) {
cmd = tradfilecmd;
} else {
cmd[0] = exe;
}
cmd.push_back(fn); cmd.push_back(fn);
} }
string result; string result;
if (!ExecCmd::backtick(cmd, result)) { if (!ExecCmd::backtick(cmd, result)) {
LOGERR(("mimetypefromdata: exec %s failed\n", FILE_PROG)); LOGERR(("mimetypefromdata: exec %s failed\n",
stringsToString(cmd).c_str()));
return string(); return string();
} }
LOGDEB2(("mimetype: [%s] \"file\" output [%s]\n", trimstring(result, " \t\n\r");
result.c_str(), fn.c_str())); LOGDEB2(("mimetype: systemfilecommand output [%s]\n", result.c_str()));
// The normal output from "file -i" looks like the following: // The normal output from "file -i" looks like the following:
// thefilename.xxx: text/plain; charset=us-ascii // thefilename.xxx: text/plain; charset=us-ascii
@ -75,8 +91,7 @@ static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc)
// mimetype.cpp: text/x-c charset=us-ascii // mimetype.cpp: text/x-c charset=us-ascii
// And sometimes we only get the mime type. This apparently happens // And sometimes we only get the mime type. This apparently happens
// when 'file' believes that the file name is binary // when 'file' believes that the file name is binary
// xdg-mime only outputs the MIME type.
trimstring(result, " \t\n\r");
// If there is no colon and there is a slash, this is hopefuly // If there is no colon and there is a slash, this is hopefuly
// the mime type // the mime type

View file

@ -215,11 +215,13 @@ idxflushmb = 10
# identification ? This may be useful, but will usually cause the # identification ? This may be useful, but will usually cause the
# indexation of many bogus 'text' files # indexation of many bogus 'text' files
usesystemfilecommand = 1 usesystemfilecommand = 1
# Actual command to use as "file -i" workalike. xdg-mime works fine here. # Actual command to use as "file -i" workalike.
# The file path will be added as a last parameter to the command line. If # The file path will be added as a last parameter to the command line. If
# that's not what your preferred command would like, use an intermediary # that's not what your preferred command would like, use an intermediary
# script. # script.
# systemfilecommand = xdg-mime query filetype # xdg-mime now works better than the traditional "file" command, and is now
# the configured default (with a hard-coded fallback to "file")
systemfilecommand = xdg-mime query filetype
# systemfilecommand = file -i filetype # systemfilecommand = file -i filetype
# Should we index the file names of files with mime types we don't # Should we index the file names of files with mime types we don't