Replace user config with central values + override

This commit is contained in:
dockes 2006-03-22 14:25:46 +00:00
parent 541ad9adaf
commit 462000eca2
4 changed files with 197 additions and 105 deletions

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.23 2006-03-20 09:51:45 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.24 2006-03-22 14:25:46 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
/* /*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -30,7 +30,6 @@ static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.23 2006-03-20 09:51:45 dockes E
#include "conftree.h" #include "conftree.h"
#include "debuglog.h" #include "debuglog.h"
#include "smallut.h" #include "smallut.h"
#include "copyfile.h"
#ifndef NO_NAMESPACES #ifndef NO_NAMESPACES
using namespace std; using namespace std;
@ -55,53 +54,58 @@ RclConfig::RclConfig()
m_confdir = path_home(); m_confdir = path_home();
m_confdir += ".recoll/"; m_confdir += ".recoll/";
} }
string cfilename = path_cat(m_confdir, "recoll.conf");
if (access(m_confdir.c_str(), 0) != 0 || if (access(m_confdir.c_str(), 0) < 0) {
access(cfilename.c_str(), 0) != 0) {
if (!initUserConfig()) if (!initUserConfig())
return; return;
} }
// Open readonly here so as not to casually create a config file list<string>cfns;
m_conf = new ConfTree(cfilename.c_str(), true); string cpath;
if (m_conf == 0 ||
(m_conf->getStatus() != ConfSimple::STATUS_RO && cpath = path_cat(m_confdir, "recoll.conf");
m_conf->getStatus() != ConfSimple::STATUS_RW)) { cfns.push_back(cpath);
m_reason = string("No main configuration file: ") + cfilename + cpath = path_cat(m_datadir, "examples/recoll.conf");
" does not exist or cannot be parsed"; cfns.push_back(cpath);
m_conf = new ConfStack<ConfTree>(cfns, true);
if (m_conf == 0 || !m_conf->ok()) {
m_reason = string("No main configuration file: ");
for (list<string>::const_iterator it = cfns.begin(); it != cfns.end();
it++)
m_reason += "[" + *it + "] ";
m_reason += " do not exist or cannot be parsed";
return; return;
} }
string mimemapfile; cfns.clear();
if (!m_conf->get("mimemapfile", mimemapfile, "")) { cpath = path_cat(m_confdir, "mimemap");
mimemapfile = "mimemap"; cfns.push_back(cpath);
} cpath = path_cat(m_datadir, "examples/mimemap");
string mpath = path_cat(m_confdir, mimemapfile); cfns.push_back(cpath);
mimemap = new ConfTree(mpath.c_str(), true); mimemap = new ConfStack<ConfTree>(cfns, true);
if (mimemap == 0 || if (mimemap == 0 || !mimemap->ok()) {
(mimemap->getStatus() != ConfSimple::STATUS_RO && m_reason = string("No mime map configuration file: ");
mimemap->getStatus() != ConfSimple::STATUS_RW)) { for (list<string>::const_iterator it = cfns.begin(); it != cfns.end();
m_reason = string("No mime map configuration file: ") + mpath + it++)
" does not exist or cannot be parsed"; m_reason += "[" + *it + "] ";
m_reason += " do not exist or cannot be parsed";
return; return;
} }
// mimemap->list();
string mimeconffile; cfns.clear();
if (!m_conf->get("mimeconffile", mimeconffile, "")) { cpath = path_cat(m_confdir, "mimeconf");
mimeconffile = "mimeconf"; cfns.push_back(cpath);
} cpath = path_cat(m_datadir, "examples/mimeconf");
mpath = path_cat(m_confdir, mimeconffile); cfns.push_back(cpath);
mimeconf = new ConfTree(mpath.c_str(), true); mimeconf = new ConfStack<ConfTree>(cfns, true);
if (mimeconf == 0 || if (mimeconf == 0 || !mimeconf->ok()) {
(mimeconf->getStatus() != ConfSimple::STATUS_RO && m_reason = string("No mime configuration file: ");
mimeconf->getStatus() != ConfSimple::STATUS_RW)) { for (list<string>::const_iterator it = cfns.begin(); it != cfns.end();
m_reason = string("No mime configuration file: ") + mpath + it++)
" does not exist or cannot be parsed"; m_reason += "[" + *it + "] ";
m_reason += " do not exist or cannot be parsed";
return; return;
} }
// mimeconf->list();
setKeyDir(""); setKeyDir("");
@ -300,27 +304,44 @@ bool RclConfig::getUncompressor(const string &mtype, list<string>& cmd)
return true; 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 copying sample files // Create initial user config by creating commented empty files
static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"}; static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"};
static int ncffiles = sizeof(configfiles) / sizeof(char *); static int ncffiles = sizeof(configfiles) / sizeof(char *);
bool RclConfig::initUserConfig() bool RclConfig::initUserConfig()
{ {
// Samples directory // Explanatory text
char blurb[sizeof(blurb0)+1025];
string exdir = path_cat(m_datadir, "examples"); string exdir = path_cat(m_datadir, "examples");
// User's sprintf(blurb, blurb0, exdir.c_str());
string recolldir = path_tildexpand("~/.recoll");
if (mkdir(recolldir.c_str(), 0755) < 0) { if (access(m_confdir.c_str(), 0) < 0 &&
m_reason += string("mkdir(") + recolldir + ") failed: " + mkdir(m_confdir.c_str(), 0755) < 0) {
m_reason += string("mkdir(") + m_confdir + ") failed: " +
strerror(errno); strerror(errno);
return false; return false;
} }
for (int i = 0; i < ncffiles; i++) { for (int i = 0; i < ncffiles; i++) {
string src = path_cat((const string&)exdir, string(configfiles[i])); string dst = path_cat(m_confdir, string(configfiles[i]));
string dst = path_cat((const string&)recolldir, string(configfiles[i])); if (access(dst.c_str(), 0) < 0) {
if (!copyfile(src.c_str(), dst.c_str(), m_reason)) { FILE *fp = fopen(dst.c_str(), "w");
LOGERR(("Copyfile failed: %s\n", m_reason.c_str())); if (fp) {
return false; fprintf(fp, "%s\n", blurb);
fclose(fp);
} else {
m_reason += string("fopen ") + dst + ": " + strerror(errno);
return false;
}
} }
} }
return true; return true;
@ -337,13 +358,11 @@ void RclConfig::initFrom(const RclConfig& r)
m_keydir = r.m_datadir; m_keydir = r.m_datadir;
// We should use reference-counted objects instead! // We should use reference-counted objects instead!
if (r.m_conf) if (r.m_conf)
m_conf = new ConfTree(*(r.m_conf)); m_conf = new ConfStack<ConfTree>(*(r.m_conf));
if (r.mimemap) if (r.mimemap)
mimemap = new ConfTree(*(r.mimemap)); mimemap = new ConfStack<ConfTree>(*(r.mimemap));
if (r.mimeconf) if (r.mimeconf)
mimeconf = new ConfTree(*(r.mimeconf)); mimeconf = new ConfStack<ConfTree>(*(r.mimeconf));
if (r.mimemap_local)
mimemap_local = new ConfTree(*(r.mimemap_local));
if (r.stopsuffixes) if (r.stopsuffixes)
stopsuffixes = new std::list<std::string>(*(r.stopsuffixes)); stopsuffixes = new std::list<std::string>(*(r.stopsuffixes));
defcharset = r.defcharset; defcharset = r.defcharset;

View File

@ -16,7 +16,7 @@
*/ */
#ifndef _RCLCONFIG_H_INCLUDED_ #ifndef _RCLCONFIG_H_INCLUDED_
#define _RCLCONFIG_H_INCLUDED_ #define _RCLCONFIG_H_INCLUDED_
/* @(#$Id: rclconfig.h,v 1.16 2006-03-20 09:51:45 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: rclconfig.h,v 1.17 2006-03-22 14:25:46 dockes Exp $ (C) 2004 J.F.Dockes */
#include <list> #include <list>
@ -117,10 +117,10 @@ class RclConfig {
string m_datadir; // Example: /usr/local/share/recoll string m_datadir; // Example: /usr/local/share/recoll
string m_keydir; // Current directory used for parameter fetches. string m_keydir; // Current directory used for parameter fetches.
ConfTree *m_conf; // Parsed main configuration ConfStack<ConfTree> *m_conf; // Parsed configuration files
ConfTree *mimemap; // These are independant of current keydir. ConfStack<ConfTree> *mimemap; // The files don't change with keydir, but their
ConfTree *mimeconf; ConfStack<ConfTree> *mimeconf; // content may depend on it.
ConfTree *mimemap_local; //
std::list<std::string> *stopsuffixes; std::list<std::string> *stopsuffixes;
// Parameters auto-fetched on setkeydir // Parameters auto-fetched on setkeydir
@ -138,7 +138,6 @@ class RclConfig {
m_conf = 0; m_conf = 0;
mimemap = 0; mimemap = 0;
mimeconf = 0; mimeconf = 0;
mimemap_local = 0;
stopsuffixes = 0; stopsuffixes = 0;
} }
/** Free data then zero pointers */ /** Free data then zero pointers */
@ -146,7 +145,6 @@ class RclConfig {
delete m_conf; delete m_conf;
delete mimemap; delete mimemap;
delete mimeconf; delete mimeconf;
delete mimemap_local;
delete stopsuffixes; delete stopsuffixes;
// just in case // just in case
zeroMe(); zeroMe();

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid [] = "@(#$Id: conftree.cpp,v 1.5 2006-01-23 13:32:28 dockes Exp $ (C) 2003 J.F.Dockes"; static char rcsid [] = "@(#$Id: conftree.cpp,v 1.6 2006-03-22 14:25:46 dockes Exp $ (C) 2003 J.F.Dockes";
#endif #endif
/* /*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -203,24 +203,6 @@ int ConfSimple::get(const string &nm, string &value, const string &sk)
return 1; return 1;
} }
const char *ConfSimple::get(const char *nm, const char *sk)
{
if (status == STATUS_ERROR)
return 0;
if (sk == 0)
sk = "";
// Find submap
map<string, map<string, string> >::iterator ss;
if ((ss = submaps.find(sk)) == submaps.end())
return 0;
// Find named value
map<string, string>::iterator s;
if ((s = ss->second.find(nm)) == ss->second.end())
return 0;
return (s->second).c_str();
}
static ConfSimple::WalkerCode swalker(void *f, const string &nm, static ConfSimple::WalkerCode swalker(void *f, const string &nm,
const string &value) const string &value)
{ {
@ -540,26 +522,19 @@ int main(int argc, char **argv)
ConfSimple parms(filename); ConfSimple parms(filename);
if (parms.getStatus() != ConfSimple::STATUS_ERROR) { if (parms.getStatus() != ConfSimple::STATUS_ERROR) {
// It's ok for the file to not exist here // It's ok for the file to not exist here
string value;
const char *cp = parms.get("mypid"); if (parms.get("mypid", value)) {
if (cp) { printf("Value for mypid is '%s'\n", value.c_str());
printf("Value for mypid is '%s'\n", cp);
} else { } else {
printf("mypid not set\n"); printf("mypid not set\n");
} }
cp = parms.get("unstring");
if (cp) { if (parms.get("unstring", value)) {
printf("Value for unstring is '%s'\n", cp); printf("Value for unstring is '%s'\n", value.c_str());
} else { } else {
printf("unstring not set\n"); printf("unstring not set\n");
} }
string myval;
if (parms.get(string("unstring"), myval, "")) {
printf("std::string value for 'unstring' is '%s'\n",
myval.c_str());
} else {
printf("unstring not set (std::string)\n");
}
} }
char spid[100]; char spid[100];
sprintf(spid, "%d", getpid()); sprintf(spid, "%d", getpid());

View File

@ -16,6 +16,7 @@
*/ */
#ifndef _CONFTREE_H_ #ifndef _CONFTREE_H_
#define _CONFTREE_H_ #define _CONFTREE_H_
/** /**
* A simple configuration file implementation. * A simple configuration file implementation.
* *
@ -31,6 +32,7 @@
* Any line without a '=' is discarded when rewriting the file. * Any line without a '=' is discarded when rewriting the file.
* All 'set' calls currently cause an immediate file rewrite. * All 'set' calls currently cause an immediate file rewrite.
*/ */
#include <string> #include <string>
#include <map> #include <map>
#include <list> #include <list>
@ -77,15 +79,9 @@ class ConfSimple {
* global space if sk is empty). * global space if sk is empty).
* @return 0 if name not found, 1 else * @return 0 if name not found, 1 else
*/ */
virtual int get(const std::string &name, string &value, const string &sk); virtual int get(const std::string &name, string &value,
virtual int get(const std::string &name, string &value) { const string &sk = string(""));
return get(name, value, string("")); /* Note: the version returning char* was buggy and has been removed */
}
/*
* See comments for std::string variant
* @return 0 if name not found, const C string else
*/
virtual const char *get(const char *name, const char *sk = 0);
/** /**
* Set value for named parameter in specified subsection (or global) * Set value for named parameter in specified subsection (or global)
@ -155,8 +151,8 @@ class ConfSimple {
protected: protected:
bool dotildexpand; bool dotildexpand;
private:
StatusCode status; StatusCode status;
private:
string filename; // set if we're working with a file string filename; // set if we're working with a file
string *data; // set if we're working with an in-memory string string *data; // set if we're working with an in-memory string
map<string, map<string, string> > submaps; map<string, map<string, string> > submaps;
@ -180,9 +176,6 @@ class ConfSimple {
*/ */
class ConfTree : public ConfSimple { class ConfTree : public ConfSimple {
/* Dont want this to be accessible: keep only the string-based one */
virtual const char *get(const char *, const char *) {return 0;}
public: public:
/** /**
* Build the object by reading content from file. * Build the object by reading content from file.
@ -208,4 +201,111 @@ class ConfTree : public ConfSimple {
} }
}; };
/**
* Use several config files, trying to get values from each in order. Used to
* have a central config, with possible overrides from more specific
* (ie personal) ones.
*
* Notes: it's ok for some of the files in the list to not exist, but the last
* one must or we generate an error. We open all trees readonly.
*/
template <class T> class ConfStack {
public:
ConfStack(const std::list<string> &fns, bool ro = true) {
construct(fns, ro);
}
ConfStack(const char *nm, bool ro = true) {
std::list<string> fns;
fns.push_back(string(nm));
construct(fns, ro);
}
~ConfStack() {
erase();
m_ok = false;
}
ConfStack(const ConfStack &rhs) {
init_from(rhs);
}
ConfStack& operator=(const ConfStack &rhs) {
if (this != &rhs){
erase();
m_ok = rhs.m_ok;
if (m_ok)
init_from(rhs);
}
return *this;
}
int get(const std::string &name, string &value, const string &sk) {
typename std::list<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->get(name, value, sk))
return true;
}
return false;
}
int get(const char *name, string &value, const char *sk) {
return get(string(name), value, sk ? string(sk) : string(""));
}
std::list<string> getNames(const string &sk) {
std::list<string> nms;
typename std::list<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
std::list<string> lst;
lst = (*it)->getNames(sk);
nms.splice(nms.end(), lst);
}
return nms;
}
bool ok() {return m_ok;}
private:
bool m_ok;
std::list<T*> m_confs;
void erase() {
typename std::list<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
delete (*it);
}
m_confs.clear();
}
/// Common code to initialize from existing object
void init_from(const ConfStack &rhs) {
if ((m_ok = rhs.m_ok)) {
typename std::list<T*>::const_iterator it;
for (it = rhs.m_confs.begin();it != rhs.m_confs.end();it++) {
m_confs.push_back(new T(**it));
}
}
}
/// Common constructor code
void construct(const std::list<string> &fns, bool ro) {
if (!ro) {
m_ok = false;
return;
}
std::list<std::string>::const_iterator it;
bool lastok = false;
for (it = fns.begin();it != fns.end();it++) {
T* p = new T(it->c_str(), true);
if (p && p->ok()) {
m_confs.push_back(p);
lastok = true;
} else
lastok = false;
}
m_ok = lastok;
}
};
#endif /*_CONFTREE_H_ */ #endif /*_CONFTREE_H_ */