|
a/src/common/rclconfig.cpp |
|
b/src/common/rclconfig.cpp |
1 |
#ifndef lint
|
1 |
#ifndef lint
|
2 |
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.18 2006-01-10 12:58:39 dockes Exp $ (C) 2004 J.F.Dockes";
|
2 |
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.19 2006-01-19 17:11:46 dockes Exp $ (C) 2004 J.F.Dockes";
|
3 |
#endif
|
3 |
#endif
|
4 |
#include <unistd.h>
|
4 |
#include <unistd.h>
|
5 |
#include <stdio.h>
|
5 |
#include <stdio.h>
|
6 |
#include <errno.h>
|
6 |
#include <errno.h>
|
7 |
#include <sys/types.h>
|
7 |
#include <sys/types.h>
|
|
... |
|
... |
21 |
#endif /* NO_NAMESPACES */
|
21 |
#endif /* NO_NAMESPACES */
|
22 |
|
22 |
|
23 |
static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"};
|
23 |
static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"};
|
24 |
static int ncffiles = sizeof(configfiles) / sizeof(char *);
|
24 |
static int ncffiles = sizeof(configfiles) / sizeof(char *);
|
25 |
|
25 |
|
26 |
static bool createConfig(string &reason)
|
26 |
static bool createConfig(const string &datadir, string &reason)
|
27 |
{
|
27 |
{
|
28 |
const char *cprefix = getenv("RECOLL_PREFIX");
|
28 |
// Samples directory
|
29 |
if (cprefix == 0)
|
29 |
string exdir = path_cat(datadir, "examples");
|
30 |
cprefix = RECOLL_PREFIX;
|
30 |
// User's
|
31 |
string prefix = path_cat(cprefix, "share/recoll/examples");
|
|
|
32 |
|
|
|
33 |
string recolldir = path_tildexpand("~/.recoll");
|
31 |
string recolldir = path_tildexpand("~/.recoll");
|
34 |
if (mkdir(recolldir.c_str(), 0755) < 0) {
|
32 |
if (mkdir(recolldir.c_str(), 0755) < 0) {
|
35 |
reason += string("mkdir(") + recolldir + ") failed: " +
|
33 |
reason += string("mkdir(") + recolldir + ") failed: " +
|
36 |
strerror(errno);
|
34 |
strerror(errno);
|
37 |
return false;
|
35 |
return false;
|
38 |
}
|
36 |
}
|
39 |
for (int i = 0; i < ncffiles; i++) {
|
37 |
for (int i = 0; i < ncffiles; i++) {
|
40 |
string src = path_cat((const string&)prefix, string(configfiles[i]));
|
38 |
string src = path_cat((const string&)exdir, string(configfiles[i]));
|
41 |
string dst = path_cat((const string&)recolldir, string(configfiles[i]));
|
39 |
string dst = path_cat((const string&)recolldir, string(configfiles[i]));
|
42 |
if (!copyfile(src.c_str(), dst.c_str(), reason)) {
|
40 |
if (!copyfile(src.c_str(), dst.c_str(), reason)) {
|
43 |
LOGERR(("Copyfile failed: %s\n", reason.c_str()));
|
41 |
LOGERR(("Copyfile failed: %s\n", reason.c_str()));
|
44 |
return false;
|
42 |
return false;
|
45 |
}
|
43 |
}
|
|
... |
|
... |
47 |
return true;
|
45 |
return true;
|
48 |
}
|
46 |
}
|
49 |
|
47 |
|
50 |
|
48 |
|
51 |
RclConfig::RclConfig()
|
49 |
RclConfig::RclConfig()
|
52 |
: m_ok(false), conf(0), mimemap(0), mimeconf(0), stopsuffixes(0)
|
50 |
: m_ok(false), m_conf(0), mimemap(0), mimeconf(0), mimemap_local(0),
|
53 |
|
51 |
stopsuffixes(0)
|
54 |
{
|
52 |
{
|
55 |
static int loginit = 0;
|
53 |
static int loginit = 0;
|
56 |
if (!loginit) {
|
54 |
if (!loginit) {
|
57 |
DebugLog::setfilename("stderr");
|
55 |
DebugLog::setfilename("stderr");
|
58 |
DebugLog::getdbl()->setloglevel(10);
|
56 |
DebugLog::getdbl()->setloglevel(10);
|
59 |
loginit = 1;
|
57 |
loginit = 1;
|
60 |
}
|
58 |
}
|
61 |
|
59 |
|
|
|
60 |
// Compute our data dir name, typically /usr/local/share/recoll
|
|
|
61 |
const char *cdatadir = getenv("RECOLL_DATADIR");
|
|
|
62 |
if (cdatadir == 0) {
|
|
|
63 |
// If not in environment, use the compiled-in constant.
|
|
|
64 |
m_datadir = RECOLL_DATADIR;
|
|
|
65 |
} else {
|
|
|
66 |
m_datadir = cdatadir;
|
|
|
67 |
}
|
|
|
68 |
|
62 |
const char *cp = getenv("RECOLL_CONFDIR");
|
69 |
const char *cp = getenv("RECOLL_CONFDIR");
|
63 |
if (cp) {
|
70 |
if (cp) {
|
64 |
confdir = cp;
|
71 |
m_confdir = cp;
|
65 |
} else {
|
72 |
} else {
|
66 |
confdir = path_home();
|
73 |
m_confdir = path_home();
|
67 |
confdir += ".recoll/";
|
74 |
m_confdir += ".recoll/";
|
68 |
}
|
75 |
}
|
69 |
string cfilename = path_cat(confdir, "recoll.conf");
|
76 |
string cfilename = path_cat(m_confdir, "recoll.conf");
|
70 |
|
77 |
|
71 |
if (access(confdir.c_str(), 0) != 0 || access(cfilename.c_str(), 0) != 0) {
|
78 |
if (access(m_confdir.c_str(), 0) != 0 ||
|
|
|
79 |
access(cfilename.c_str(), 0) != 0) {
|
72 |
if (!createConfig(reason))
|
80 |
if (!createConfig(m_datadir, reason))
|
73 |
return;
|
81 |
return;
|
74 |
}
|
82 |
}
|
75 |
|
83 |
|
76 |
// Open readonly here so as not to casually create a config file
|
84 |
// Open readonly here so as not to casually create a config file
|
77 |
conf = new ConfTree(cfilename.c_str(), true);
|
85 |
m_conf = new ConfTree(cfilename.c_str(), true);
|
78 |
if (conf == 0 ||
|
86 |
if (m_conf == 0 ||
|
79 |
(conf->getStatus() != ConfSimple::STATUS_RO &&
|
87 |
(m_conf->getStatus() != ConfSimple::STATUS_RO &&
|
80 |
conf->getStatus() != ConfSimple::STATUS_RW)) {
|
88 |
m_conf->getStatus() != ConfSimple::STATUS_RW)) {
|
81 |
reason = string("No main configuration file: ") + cfilename +
|
89 |
reason = string("No main configuration file: ") + cfilename +
|
82 |
" does not exist or cannot be parsed";
|
90 |
" does not exist or cannot be parsed";
|
83 |
return;
|
91 |
return;
|
84 |
}
|
92 |
}
|
85 |
|
93 |
|
86 |
string mimemapfile;
|
94 |
string mimemapfile;
|
87 |
if (!conf->get("mimemapfile", mimemapfile, "")) {
|
95 |
if (!m_conf->get("mimemapfile", mimemapfile, "")) {
|
88 |
mimemapfile = "mimemap";
|
96 |
mimemapfile = "mimemap";
|
89 |
}
|
97 |
}
|
90 |
string mpath = path_cat(confdir, mimemapfile);
|
98 |
string mpath = path_cat(m_confdir, mimemapfile);
|
91 |
mimemap = new ConfTree(mpath.c_str(), true);
|
99 |
mimemap = new ConfTree(mpath.c_str(), true);
|
92 |
if (mimemap == 0 ||
|
100 |
if (mimemap == 0 ||
|
93 |
(mimemap->getStatus() != ConfSimple::STATUS_RO &&
|
101 |
(mimemap->getStatus() != ConfSimple::STATUS_RO &&
|
94 |
mimemap->getStatus() != ConfSimple::STATUS_RW)) {
|
102 |
mimemap->getStatus() != ConfSimple::STATUS_RW)) {
|
95 |
reason = string("No mime map configuration file: ") + mpath +
|
103 |
reason = string("No mime map configuration file: ") + mpath +
|
|
... |
|
... |
97 |
return;
|
105 |
return;
|
98 |
}
|
106 |
}
|
99 |
// mimemap->list();
|
107 |
// mimemap->list();
|
100 |
|
108 |
|
101 |
string mimeconffile;
|
109 |
string mimeconffile;
|
102 |
if (!conf->get("mimeconffile", mimeconffile, "")) {
|
110 |
if (!m_conf->get("mimeconffile", mimeconffile, "")) {
|
103 |
mimeconffile = "mimeconf";
|
111 |
mimeconffile = "mimeconf";
|
104 |
}
|
112 |
}
|
105 |
mpath = path_cat(confdir, mimeconffile);
|
113 |
mpath = path_cat(m_confdir, mimeconffile);
|
106 |
mimeconf = new ConfTree(mpath.c_str(), true);
|
114 |
mimeconf = new ConfTree(mpath.c_str(), true);
|
107 |
if (mimeconf == 0 ||
|
115 |
if (mimeconf == 0 ||
|
108 |
(mimeconf->getStatus() != ConfSimple::STATUS_RO &&
|
116 |
(mimeconf->getStatus() != ConfSimple::STATUS_RO &&
|
109 |
mimeconf->getStatus() != ConfSimple::STATUS_RW)) {
|
117 |
mimeconf->getStatus() != ConfSimple::STATUS_RW)) {
|
110 |
reason = string("No mime configuration file: ") + mpath +
|
118 |
reason = string("No mime configuration file: ") + mpath +
|
|
... |
|
... |
208 |
string hs;
|
216 |
string hs;
|
209 |
mimeconf->get(mtype, hs, "icons");
|
217 |
mimeconf->get(mtype, hs, "icons");
|
210 |
return hs;
|
218 |
return hs;
|
211 |
}
|
219 |
}
|
212 |
|
220 |
|
213 |
// Look up an executable filter.
|
221 |
// Look up an executable filter. We look in $RECOLL_FILTERSDIR,
|
214 |
// We look in RECOLL_FILTERSDIR, filtersdir param, then
|
222 |
// filtersdir in config file, then let the system use the PATH
|
215 |
// let the system use the PATH
|
223 |
string RclConfig::findFilter(const string &icmd)
|
216 |
string find_filter(RclConfig *conf, const string &icmd)
|
|
|
217 |
{
|
224 |
{
|
218 |
// If the path is absolute, this is it
|
225 |
// If the path is absolute, this is it
|
219 |
if (icmd[0] == '/')
|
226 |
if (icmd[0] == '/')
|
220 |
return icmd;
|
227 |
return icmd;
|
221 |
|
228 |
|
222 |
string cmd;
|
229 |
string cmd;
|
223 |
const char *cp;
|
230 |
const char *cp;
|
224 |
|
231 |
|
|
|
232 |
// Filters dir from environment ?
|
225 |
if ((cp = getenv("RECOLL_FILTERSDIR"))) {
|
233 |
if ((cp = getenv("RECOLL_FILTERSDIR"))) {
|
226 |
cmd = path_cat(cp, icmd);
|
234 |
cmd = path_cat(cp, icmd);
|
227 |
if (access(cmd.c_str(), X_OK) == 0)
|
235 |
if (access(cmd.c_str(), X_OK) == 0)
|
228 |
return cmd;
|
236 |
return cmd;
|
|
|
237 |
}
|
|
|
238 |
// Filters dir as configuration parameter?
|
229 |
} else if (conf->getConfParam(string("filtersdir"), cmd)) {
|
239 |
if (getConfParam(string("filtersdir"), cmd)) {
|
230 |
cmd = path_cat(cmd, icmd);
|
240 |
cmd = path_cat(cmd, icmd);
|
231 |
if (access(cmd.c_str(), X_OK) == 0)
|
241 |
if (access(cmd.c_str(), X_OK) == 0)
|
232 |
return cmd;
|
242 |
return cmd;
|
233 |
} else {
|
243 |
}
|
234 |
cmd = path_cat(conf->getConfDir(), icmd);
|
244 |
|
|
|
245 |
// Filters dir as datadir subdir. Actually the standard case, but
|
|
|
246 |
// this is normally the same value found in config file (previous step)
|
|
|
247 |
cmd = path_cat(m_datadir, "filters");
|
|
|
248 |
cmd = path_cat(cmd, icmd);
|
235 |
if (access(cmd.c_str(), X_OK) == 0)
|
249 |
if (access(cmd.c_str(), X_OK) == 0)
|
236 |
return cmd;
|
250 |
return cmd;
|
237 |
}
|
251 |
|
|
|
252 |
// Last resort for historical reasons: check in personal config
|
|
|
253 |
// directory
|
|
|
254 |
cmd = path_cat(getConfDir(), icmd);
|
|
|
255 |
if (access(cmd.c_str(), X_OK) == 0)
|
|
|
256 |
return cmd;
|
|
|
257 |
|
|
|
258 |
// Let the shell try to find it...
|
238 |
return icmd;
|
259 |
return icmd;
|
239 |
}
|
260 |
}
|
240 |
|
261 |
|
241 |
/**
|
262 |
/**
|
242 |
* Return decompression command line for given mime type
|
263 |
* Return decompression command line for given mime type
|