allow choosing the "file" command from configure

This commit is contained in:
dockes 2009-08-13 06:26:59 +00:00
parent ea82bc9020
commit ae61b25fc4
3 changed files with 1694 additions and 1262 deletions

2868
src/configure vendored

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,30 @@ fi
AC_CHECK_HEADERS(sys/mount.h sys/statfs.h sys/statvfs.h sys/vfs.h)
# Use specific 'file' command ? (Useful on solaris to specify
# /usr/local/bin/file instead of the system's which doesn't understand '-i'
AC_ARG_WITH(file-command,
AC_HELP_STRING([--with-file-command],
[Specify version of 'file' command (ie: --with-file-command=/usr/local/bin/file)]),
withFileCommand=$withval, withFileCommand=file)
case $withFileCommand in
file)
AC_PATH_PROG(fileProg, file);;
*)
fileProg=$withFileCommand;;
esac
if test ! -x "$fileProg"; then
AC_MSG_ERROR([$fileProg does not exist or is not executable])
fi
AC_DEFINE_UNQUOTED(FILE_PROG, "$fileProg", [Path to the file program])
# Can't use Solaris standard 'file' command, it doesn't support -i
if test X$sys != XSunOS -o X$fileProg != X/usr/bin/file; then
AC_DEFINE(USE_SYSTEM_FILE_COMMAND)
fi
# Use aspell to provide spelling expansions ?
# The default is yes. If we do find an aspell installation, we use it. Else
# we do compile the aspell module using an internal copy of aspell.h

View File

@ -29,6 +29,7 @@ static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.22 2008-11-18 13:25:48 dockes Ex
using namespace std;
#endif /* NO_NAMESPACES */
#include "autoconfig.h"
#include "mimetype.h"
#include "debuglog.h"
#include "execmd.h"
@ -36,30 +37,25 @@ using namespace std;
#include "smallut.h"
#include "idfile.h"
// Solaris8's 'file' command doesnt understand -i
#ifndef sun
#define USE_SYSTEM_FILE_COMMAND
#endif
/// Identification of file from contents. This is called for files with
/// unrecognized extensions (none, or not known either for indexing or
/// stop list)
/// unrecognized extensions.
///
/// The system 'file' utility is not that great for us. For exemple it
/// will mistake mail folders for simple text files if there is no
/// 'Received' header, which would be the case, for exemple in a 'Sent'
/// folder. Also "file -i" does not exist on all systems, and it's
/// quite costly.
/// The system 'file' utility does not always work for us. For exemple
/// it will mistake mail folders for simple text files if there is no
/// 'Received' header, which would be the case, for exemple in a
/// 'Sent' folder. Also "file -i" does not exist on all systems, and
/// is quite costly to execute.
/// So we first call the internal file identifier, which currently
/// only knows about mail, but in which we can add the more
/// current/interesting file types.
/// As a last resort we execute 'file'
/// As a last resort we execute 'file' (except if forbidden by config)
static string mimetypefromdata(const string &fn, bool usfc)
{
// In any case first try the internal identifier
// First try the internal identifying routine
string mime = idFile(fn.c_str());
// Then exec 'file -i'
#ifdef USE_SYSTEM_FILE_COMMAND
if (usfc && mime.empty()) {
// Last resort: use "file -i"
@ -69,7 +65,7 @@ static string mimetypefromdata(const string &fn, bool usfc)
args.push_back(fn);
ExecCmd ex;
string result;
string cmd = "file";
string cmd = FILE_PROG;
int status = ex.doexec(cmd, args, 0, &result);
if (status) {
LOGERR(("mimetypefromdata: doexec: status 0x%x\n", status));
@ -96,48 +92,50 @@ static string mimetypefromdata(const string &fn, bool usfc)
if(mime.find("/") == string::npos)
mime.clear();
}
#endif
#endif //USE_SYSTEM_FILE_COMMAND
return mime;
}
/// Guess mime type, first from suffix, then from file data. We also
/// have a list of suffixes that we don't touch at all (ie: .jpg,
/// etc...)
/// have a list of suffixes that we don't touch at all.
string mimetype(const string &fn, const struct stat *stp,
RclConfig *cfg, bool usfc)
{
// Use stat data if available to check for non regular files
if (stp) {
if (S_ISDIR(stp->st_mode))
return "application/x-fsdirectory";
if (!S_ISREG(stp->st_mode))
return "application/x-fsspecial";
}
if (cfg == 0)
return string();
string mtype;
if (cfg == 0) // ?!?
return mtype;
if (cfg->inStopSuffixes(fn)) {
LOGDEB(("mimetype: fn [%s] in stopsuffixes\n", fn.c_str()));
return string();
return mtype;
}
// First look for suffix in mimetype map
// Compute file name suffix and search the mimetype map
string::size_type dot = fn.find_last_of(".");
string suff;
if (dot != string::npos) {
suff = fn.substr(dot);
for (unsigned int i = 0; i < suff.length(); i++)
suff[i] = tolower(suff[i]);
string mtype = cfg->getMimeTypeFromSuffix(suff);
if (!mtype.empty())
return mtype;
suff = stringtolower(fn.substr(dot));
mtype = cfg->getMimeTypeFromSuffix(suff);
}
// Finally examine data
if (!stp)
return string();
return mimetypefromdata(fn, usfc);
// If type was not determined from suffix, examine file data. Can
// only do this if we have an actual file (as opposed to a pure
// name).
if (mtype.empty() && stp)
mtype = mimetypefromdata(fn, usfc);
out:
return mtype;
}