When xdg-mime not installed, trying to use file -i failed

This commit is contained in:
Jean-Francois Dockes 2017-03-01 10:07:45 +01:00
parent faac5c8731
commit 3ff1338ae8
2 changed files with 19 additions and 13 deletions

View File

@ -23,7 +23,6 @@
#include <ctype.h> #include <ctype.h>
#include <string> #include <string>
#include <list> #include <list>
using namespace std;
#include "mimetype.h" #include "mimetype.h"
#include "log.h" #include "log.h"
@ -33,6 +32,8 @@ using namespace std;
#include "idfile.h" #include "idfile.h"
#include "pxattr.h" #include "pxattr.h"
using namespace std;
/// Identification of file from contents. This is called for files with /// Identification of file from contents. This is called for files with
/// unrecognized extensions. /// unrecognized extensions.
/// ///
@ -49,6 +50,7 @@ using namespace std;
static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc) static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc)
{ {
LOGDEB1("mimetypefromdata: fn [" << fn << "]\n");
// First try the internal identifying routine // First try the internal identifying routine
string mime = idFile(fn.c_str()); string mime = idFile(fn.c_str());
@ -58,12 +60,12 @@ static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc)
// 'file' fallback if the configured command (default: // 'file' fallback if the configured command (default:
// xdg-mime) is not found // xdg-mime) is not found
static const vector<string> tradfilecmd = {{FILE_PROG}, {"-i"}, {fn}}; static const vector<string> tradfilecmd = {{FILE_PROG}, {"-i"}};
vector<string> cmd; vector<string> cmd;
string scommand; string scommand;
if (cfg->getConfParam("systemfilecommand", scommand)) { if (cfg->getConfParam("systemfilecommand", scommand)) {
LOGDEB2("mimetype: syscmd from config: " << (scommand) << "\n" ); LOGDEB2("mimetype: syscmd from config: " << scommand << "\n");
stringToStrings(scommand, cmd); stringToStrings(scommand, cmd);
string exe; string exe;
if (cmd.empty()) { if (cmd.empty()) {
@ -75,17 +77,20 @@ static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc)
} }
cmd.push_back(fn); cmd.push_back(fn);
} else { } else {
LOGDEB("mimetype:systemfilecommand not found, using " << (stringsToString(tradfilecmd)) << "\n" ); LOGDEB("mimetype:systemfilecommand not found, using " <<
stringsToString(tradfilecmd) << "\n");
cmd = tradfilecmd; cmd = tradfilecmd;
} }
string result; string result;
LOGDEB2("mimetype: executing: [" << stringsToString(cmd) << "]\n");
if (!ExecCmd::backtick(cmd, result)) { if (!ExecCmd::backtick(cmd, result)) {
LOGERR("mimetypefromdata: exec " << (stringsToString(cmd)) << " failed\n" ); LOGERR("mimetypefromdata: exec " <<
stringsToString(cmd) << " failed\n");
return string(); return string();
} }
trimstring(result, " \t\n\r"); trimstring(result, " \t\n\r");
LOGDEB2("mimetype: systemfilecommand output [" << (result) << "]\n" ); LOGDEB2("mimetype: systemfilecommand output [" << result << "]\n");
// 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
@ -106,7 +111,8 @@ static string mimetypefromdata(RclConfig *cfg, const string &fn, bool usfc)
if (result.find(fn) != 0) { if (result.find(fn) != 0) {
// Garbage "file" output. Maybe the result of a charset // Garbage "file" output. Maybe the result of a charset
// conversion attempt? // conversion attempt?
LOGERR("mimetype: can't interpret 'file' output: [" << (result) << "]\n" ); LOGERR("mimetype: can't interpret output from [" <<
stringsToString(cmd) << "] : [" << result << "]\n");
return string(); return string();
} }
result = result.substr(fn.size()); result = result.substr(fn.size());
@ -160,9 +166,9 @@ string mimetype(const string &fn, const struct stat *stp,
// Extended attribute has priority on everything, as per: // Extended attribute has priority on everything, as per:
// http://freedesktop.org/wiki/CommonExtendedAttributes // http://freedesktop.org/wiki/CommonExtendedAttributes
if (pxattr::get(fn, "mime_type", &mtype)) { if (pxattr::get(fn, "mime_type", &mtype)) {
LOGDEB0("Mimetype: 'mime_type' xattr : [" << (mtype) << "]\n" ); LOGDEB0("Mimetype: 'mime_type' xattr : [" << mtype << "]\n");
if (mtype.empty()) { if (mtype.empty()) {
LOGDEB0("Mimetype: getxattr() returned empty mime type !\n" ); LOGDEB0("Mimetype: getxattr() returned empty mime type !\n");
} else { } else {
return mtype; return mtype;
} }
@ -170,12 +176,12 @@ string mimetype(const string &fn, const struct stat *stp,
#endif #endif
if (cfg == 0) { if (cfg == 0) {
LOGERR("Mimetype: null config ??\n" ); LOGERR("Mimetype: null config ??\n");
return mtype; return mtype;
} }
if (cfg->inStopSuffixes(fn)) { if (cfg->inStopSuffixes(fn)) {
LOGDEB("mimetype: fn [" << (fn) << "] in stopsuffixes\n" ); LOGDEB("mimetype: fn [" << fn << "] in stopsuffixes\n");
return mtype; return mtype;
} }

View File

@ -32,8 +32,8 @@ class RclConfig;
* @param cfg recoll config * @param cfg recoll config
* @param usfc Use system's 'file' command as last resort (or not) * @param usfc Use system's 'file' command as last resort (or not)
*/ */
string mimetype(const std::string &filename, const struct stat *stp, std::string mimetype(const std::string &filename, const struct stat *stp,
RclConfig *cfg, bool usfc); RclConfig *cfg, bool usfc);
#endif /* _MIMETYPE_H_INCLUDED_ */ #endif /* _MIMETYPE_H_INCLUDED_ */