Parent: [6b24b1] (diff)

Child: [1c80f0] (diff)

Download this file

rclconfig.cpp    597 lines (534 with data), 15.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#ifndef lint
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.38 2006-12-19 08:40:50 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_RCLCONFIG
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <langinfo.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef __FreeBSD__
#include <osreldate.h>
#endif
#include <iostream>
#include "pathut.h"
#include "rclconfig.h"
#include "conftree.h"
#include "debuglog.h"
#include "smallut.h"
#ifndef NO_NAMESPACES
using namespace std;
#endif /* NO_NAMESPACES */
RclConfig::RclConfig(const string *argcnf)
{
zeroMe();
// Compute our data dir name, typically /usr/local/share/recoll
const char *cdatadir = getenv("RECOLL_DATADIR");
if (cdatadir == 0) {
// If not in environment, use the compiled-in constant.
m_datadir = RECOLL_DATADIR;
} else {
m_datadir = cdatadir;
}
// Command line config name overrides environment
if (argcnf && !argcnf->empty()) {
m_confdir = *argcnf;
} else {
const char *cp = getenv("RECOLL_CONFDIR");
if (cp) {
m_confdir = cp;
} else {
m_confdir = path_home();
m_confdir += ".recoll/";
}
}
if (access(m_confdir.c_str(), 0) < 0) {
if (!initUserConfig())
return;
}
list<string> cdirs;
cdirs.push_back(m_confdir);
cdirs.push_back(path_cat(m_datadir, "examples"));
m_conf = new ConfStack<ConfTree>("recoll.conf", cdirs, true);
if (m_conf == 0 || !m_conf->ok()) {
m_reason = string("No/bad main configuration file in: ") + m_confdir;
return;
}
mimemap = new ConfStack<ConfTree>("mimemap", cdirs, true);
if (mimemap == 0 || !mimemap->ok()) {
m_reason = string("No or bad mimemap file in: ") + m_confdir;
return;
}
mimeconf = new ConfStack<ConfTree>("mimeconf", cdirs, true);
if (mimeconf == 0 || !mimeconf->ok()) {
m_reason = string("No/bad mimeconf in: ") + m_confdir;
return;
}
mimeview = new ConfStack<ConfTree>("mimeview", cdirs, true);
if (mimeconf == 0 || !mimeconf->ok()) {
m_reason = string("No/bad mimeview in: ") + m_confdir;
return;
}
setKeyDir("");
m_ok = true;
return;
}
bool RclConfig::getConfParam(const std::string &name, int *ivp)
{
string value;
if (!getConfParam(name, value))
return false;
errno = 0;
long lval = strtol(value.c_str(), 0, 0);
if (lval == 0 && errno)
return 0;
if (ivp)
*ivp = int(lval);
return true;
}
bool RclConfig::getConfParam(const std::string &name, bool *bvp)
{
if (!bvp)
return false;
*bvp = false;
string s;
if (!getConfParam(name, s))
return false;
*bvp = stringToBool(s);
return true;
}
list<string> RclConfig::getTopdirs()
{
list<string> tdl;
// Retrieve the list of directories to be indexed.
string topdirs;
if (!getConfParam("topdirs", topdirs)) {
LOGERR(("RclConfig::getTopdirs: no top directories in config\n"));
return tdl;
}
if (!stringToStrings(topdirs, tdl)) {
LOGERR(("RclConfig::getTopdirs: parse error for directory list\n"));
return tdl;
}
for (list<string>::iterator it = tdl.begin(); it != tdl.end(); it++) {
*it = path_tildexpand(*it);
*it = path_canon(*it);
}
return tdl;
}
// Get charset to be used for transcoding to utf-8 if unspecified by doc
// For document contents:
// If defcharset was set (from the config or a previous call), use it.
// Else, try to guess it from the locale
// Use iso8859-1 as ultimate default
// defcharset is reset on setKeyDir()
// For filenames, same thing except that we do not use the config file value
// (only the locale).
const string& RclConfig::getDefCharset(bool filename)
{
static string localecharset; // This supposedly never changes
if (localecharset.empty()) {
const char *cp;
cp = nl_langinfo(CODESET);
// We don't keep US-ASCII. It's better to use a superset
// Ie: me have a C locale and some french file names, and I
// can't imagine a version of iconv that couldn't translate
// from iso8859?
// The 646 thing is for solaris.
if (cp && *cp && strcmp(cp, "US-ASCII")
#ifdef sun
&& strcmp(cp, "646")
#endif
) {
localecharset = string(cp);
} else {
// Note: it seems that all versions of iconv will take
// iso-8859. Some won't take iso8859
localecharset = string("ISO-8859-1");
}
LOGDEB1(("RclConfig::getDefCharset: localecharset [%s]\n",
localecharset.c_str()));
}
if (defcharset.empty()) {
defcharset = localecharset;
}
if (filename) {
return localecharset;
} else {
return defcharset;
}
}
// Get all known document mime values (for indexing). We get them from
// the mimeconf 'index' submap: values not in there (ie from mimemap
// or idfile) can't possibly belong to documents in the database.
std::list<string> RclConfig::getAllMimeTypes()
{
std::list<string> lst;
if (mimeconf == 0)
return lst;
// mimeconf->sortwalk(mtypesWalker, &lst);
lst = mimeconf->getNames("index");
lst.sort();
lst.unique();
return lst;
}
const list<string>* RclConfig::getStopSuffixes()
{
if (stopsuffixes == 0 && (stopsuffixes = new list<string>) != 0) {
string stp;
if (mimemap && mimemap->get("recoll_noindex", stp, m_keydir)) {
stringToStrings(stp, *stopsuffixes);
}
}
return stopsuffixes;
}
string RclConfig::getMimeTypeFromSuffix(const string &suff)
{
string mtype;
mimemap->get(suff, mtype, m_keydir);
return mtype;
}
string RclConfig::getSuffixFromMimeType(const string &mt)
{
string suffix;
list<string>sfs = mimemap->getNames("");
string mt1;
for (list<string>::const_iterator it = sfs.begin();
it != sfs.end(); it++) {
if (mimemap->get(*it, mt1, ""))
if (!stringicmp(mt, mt1))
return *it;
}
return "";
}
string RclConfig::getMimeHandlerDef(const std::string &mtype)
{
string hs;
if (!mimeconf->get(mtype, hs, "index")) {
LOGDEB1(("getMimeHandler: no handler for '%s'\n", mtype.c_str()));
}
return hs;
}
string RclConfig::getMimeViewerDef(const string &mtype)
{
string hs;
mimeview->get(mtype, hs, "view");
return hs;
}
bool RclConfig::getMimeViewerDefs(vector<pair<string, string> >& defs)
{
if (mimeview == 0)
return false;
list<string>tps = mimeview->getNames("view");
for (list<string>::const_iterator it = tps.begin(); it != tps.end();it++) {
defs.push_back(pair<string, string>(*it, getMimeViewerDef(*it)));
}
return true;
}
bool RclConfig::setMimeViewerDef(const string& mt, const string& def)
{
string pconfname = path_cat(m_confdir, "mimeview");
// Make sure this exists
close(open(pconfname.c_str(), O_CREAT|O_WRONLY, 0600));
ConfTree tree(pconfname.c_str());
if (!tree.set(mt, def, "view")) {
m_reason = string("RclConfig::setMimeViewerDef: cant set value in ")
+ pconfname;
return false;
}
list<string> cdirs;
cdirs.push_back(m_confdir);
cdirs.push_back(path_cat(m_datadir, "examples"));
delete mimeview;
mimeview = new ConfStack<ConfTree>("mimeview", cdirs, true);
if (mimeconf == 0 || !mimeconf->ok()) {
m_reason = string("No/bad mimeview in: ") + m_confdir;
return false;
}
return true;
}
/**
* Return icon name and path
*/
string RclConfig::getMimeIconName(const string &mtype, string *path)
{
string iconname;
mimeconf->get(mtype, iconname, "icons");
if (iconname.empty())
iconname = "document";
if (path) {
string iconsdir;
#if defined (__FreeBSD__) && __FreeBSD_version < 500000
// gcc 2.95 dies if we call getConfParam here ??
if (m_conf) m_conf->get(string("iconsdir"), iconsdir, m_keydir);
#else
getConfParam("iconsdir", iconsdir);
#endif
if (iconsdir.empty()) {
iconsdir = path_cat(m_datadir, "images");
} else {
iconsdir = path_tildexpand(iconsdir);
}
*path = path_cat(iconsdir, iconname) + ".png";
}
return iconname;
}
string RclConfig::getDbDir()
{
string dbdir;
if (!getConfParam("dbdir", dbdir)) {
LOGERR(("RclConfig::getDbDir: no db directory in configuration\n"));
} else {
dbdir = path_tildexpand(dbdir);
// If not an absolute path, compute relative to config dir
if (dbdir.at(0) != '/') {
LOGDEB1(("Dbdir not abs, catting with confdir\n"));
dbdir = path_cat(m_confdir, dbdir);
}
}
LOGDEB1(("RclConfig::getDbDir: dbdir: [%s]\n", dbdir.c_str()));
return dbdir;
}
list<string> RclConfig::getSkippedNames()
{
list<string> skpl;
string skipped;
if (getConfParam("skippedNames", skipped)) {
stringToStrings(skipped, skpl);
}
// If current keydir is dbdir's ancestor, add dbdir name to skipped
// This is mainly for the real-time monitor that will otherwise go
// into a loop
// We'd prefer to do it for the direct ancestor only, but getSkippedNames()
// is only called for the topdirs, so this doesn't work
string kd = path_canon(getKeyDir());
string dbd = path_canon(getDbDir());
if (dbd.find(kd) == 0) {
skpl.push_back(path_getsimple(dbd));
}
return skpl;
}
// Look up an executable filter. We look in $RECOLL_FILTERSDIR,
// filtersdir in config file, then let the system use the PATH
string RclConfig::findFilter(const string &icmd)
{
// If the path is absolute, this is it
if (icmd[0] == '/')
return icmd;
string cmd;
const char *cp;
// Filters dir from environment ?
if ((cp = getenv("RECOLL_FILTERSDIR"))) {
cmd = path_cat(cp, icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
}
// Filters dir as configuration parameter?
if (getConfParam(string("filtersdir"), cmd)) {
cmd = path_cat(cmd, icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
}
// Filters dir as datadir subdir. Actually the standard case, but
// this is normally the same value found in config file (previous step)
cmd = path_cat(m_datadir, "filters");
cmd = path_cat(cmd, icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
// Last resort for historical reasons: check in personal config
// directory
cmd = path_cat(getConfDir(), icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
// Let the shell try to find it...
return icmd;
}
/**
* Return decompression command line for given mime type
*/
bool RclConfig::getUncompressor(const string &mtype, list<string>& cmd)
{
string hs;
mimeconf->get(mtype, hs, "");
if (hs.empty())
return false;
list<string> tokens;
stringToStrings(hs, tokens);
if (tokens.empty()) {
LOGERR(("getUncompressor: empty spec for mtype %s\n", mtype.c_str()));
return false;
}
list<string>::iterator it = tokens.begin();
if (tokens.size() < 2)
return false;
if (stringlowercmp("uncompress", *it++))
return false;
cmd.clear();
cmd.push_back(findFilter(*it++));
cmd.insert(cmd.end(), it, tokens.end());
return true;
}
static const char blurb0[] =
"# The system-wide configuration files for recoll are located in:\n"
"# %s\n"
"# The default configuration files are commented, you should take a look\n"
"# at them for an explanation of what can be set (you could also take a look\n"
"# at the manual instead).\n"
"# Values set in this file will override the system-wide values for the file\n"
"# with the same name in the central directory. The syntax for setting\n"
"# values is identical.\n"
;
// Create initial user config by creating commented empty files
static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf",
"mimeview"};
static int ncffiles = sizeof(configfiles) / sizeof(char *);
bool RclConfig::initUserConfig()
{
// Explanatory text
char blurb[sizeof(blurb0)+1025];
string exdir = path_cat(m_datadir, "examples");
sprintf(blurb, blurb0, exdir.c_str());
// Use protective 700 mode to create the top configuration
// directory: documents can be reconstructed from index data.
if (access(m_confdir.c_str(), 0) < 0 &&
mkdir(m_confdir.c_str(), 0700) < 0) {
m_reason += string("mkdir(") + m_confdir + ") failed: " +
strerror(errno);
return false;
}
for (int i = 0; i < ncffiles; i++) {
string dst = path_cat(m_confdir, string(configfiles[i]));
if (access(dst.c_str(), 0) < 0) {
FILE *fp = fopen(dst.c_str(), "w");
if (fp) {
fprintf(fp, "%s\n", blurb);
fclose(fp);
} else {
m_reason += string("fopen ") + dst + ": " + strerror(errno);
return false;
}
}
}
return true;
}
void RclConfig::initFrom(const RclConfig& r)
{
zeroMe();
if (!(m_ok = r.m_ok))
return;
m_reason = r.m_reason;
m_confdir = r.m_confdir;
m_datadir = r.m_datadir;
m_keydir = r.m_datadir;
// We should use reference-counted objects instead!
if (r.m_conf)
m_conf = new ConfStack<ConfTree>(*(r.m_conf));
if (r.mimemap)
mimemap = new ConfStack<ConfTree>(*(r.mimemap));
if (r.mimeconf)
mimeconf = new ConfStack<ConfTree>(*(r.mimeconf));
if (r.mimeview)
mimeview = new ConfStack<ConfTree>(*(r.mimeview));
if (r.stopsuffixes)
stopsuffixes = new std::list<std::string>(*(r.stopsuffixes));
defcharset = r.defcharset;
guesscharset = r.guesscharset;
}
#else // -> Test
#include <stdio.h>
#include <signal.h>
#include <sys/stat.h>
#include <iostream>
#include <list>
#include <string>
using namespace std;
#include "debuglog.h"
#include "rclinit.h"
#include "rclconfig.h"
static char *thisprog;
static char usage [] =
" \n\n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s [-s subkey] [-q param]", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_s 0x2
#define OPT_q 0x4
int main(int argc, char **argv)
{
string pname, skey;
thisprog = argv[0];
argc--; argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
Usage();
while (**argv)
switch (*(*argv)++) {
case 's': op_flags |= OPT_s; if (argc < 2) Usage();
skey = *(++argv);
argc--;
goto b1;
case 'q': op_flags |= OPT_q; if (argc < 2) Usage();
pname = *(++argv);
argc--;
goto b1;
default: Usage(); break;
}
b1: argc--; argv++;
}
if (argc != 0)
Usage();
string reason;
RclConfig *config = recollinit(0, 0, reason);
if (config == 0 || !config->ok()) {
cerr << "Configuration problem: " << reason << endl;
exit(1);
}
if (op_flags & OPT_s)
config->setKeyDir(skey);
if (op_flags & OPT_q) {
string value;
if (!config->getConfParam(pname, value)) {
fprintf(stderr, "getConfParam failed for [%s]\n", pname.c_str());
exit(1);
}
printf("[%s] -> [%s]\n", pname.c_str(), value.c_str());
} else {
list<string> names = config->getConfNames("");
names.sort();
names.unique();
for (list<string>::iterator it = names.begin();
it != names.end();it++) {
string value;
config->getConfParam(*it, value);
cout << *it << " -> [" << value << "]" << endl;
}
}
exit(0);
}
#endif // TEST_RCLCONFIG