dynconf/query history: add readonly mode

This commit is contained in:
Jean-Francois Dockes 2017-12-06 10:14:04 +01:00
parent 7346105dcb
commit e55840c624
2 changed files with 46 additions and 26 deletions

View File

@ -16,9 +16,7 @@
*/ */
#ifndef TEST_HISTORY #ifndef TEST_HISTORY
#include <stdio.h> #include "safeunistd.h"
#include <time.h>
#include <cstdlib>
#include "dynconf.h" #include "dynconf.h"
#include "base64.h" #include "base64.h"
@ -33,10 +31,28 @@ const string allEdbsSk = "allExtDbs";
const string actEdbsSk = "actExtDbs"; const string actEdbsSk = "actExtDbs";
const string advSearchHistSk = "advSearchHist"; const string advSearchHistSk = "advSearchHist";
RclDynConf::RclDynConf(const std::string &fn)
: m_data(fn.c_str())
{
if (m_data.getStatus() != ConfSimple::STATUS_RW) {
// Maybe the config dir is readonly, in which case we try to
// open readonly, but we must also handle the case where the
// history file does not exist
if (access(fn.c_str(), 0) != 0) {
m_data = ConfSimple(string(), 1);
} else {
m_data = ConfSimple(fn.c_str(), 1);
}
}
}
bool RclDynConf::insertNew(const string &sk, DynConfEntry &n, DynConfEntry &s, bool RclDynConf::insertNew(const string &sk, DynConfEntry &n, DynConfEntry &s,
int maxlen) int maxlen)
{ {
if (!rw()) {
LOGDEB("RclDynConf::insertNew: not writable\n");
return false;
}
// Is this doc already in list ? If it is we remove the old entry // Is this doc already in list ? If it is we remove the old entry
vector<string> names = m_data.getNames(sk); vector<string> names = m_data.getNames(sk);
vector<string>::const_iterator it; vector<string>::const_iterator it;
@ -90,6 +106,10 @@ bool RclDynConf::insertNew(const string &sk, DynConfEntry &n, DynConfEntry &s,
bool RclDynConf::eraseAll(const string &sk) bool RclDynConf::eraseAll(const string &sk)
{ {
if (!rw()) {
LOGDEB("RclDynConf::eraseAll: not writable\n");
return false;
}
vector<string> names = m_data.getNames(sk); vector<string> names = m_data.getNames(sk);
vector<string>::const_iterator it; vector<string>::const_iterator it;
for (it = names.begin(); it != names.end(); it++) { for (it = names.begin(); it != names.end(); it++) {
@ -103,6 +123,10 @@ bool RclDynConf::eraseAll(const string &sk)
bool RclDynConf::enterString(const string sk, const string value, int maxlen) bool RclDynConf::enterString(const string sk, const string value, int maxlen)
{ {
if (!rw()) {
LOGDEB("RclDynConf::enterString: not writable\n");
return false;
}
RclSListEntry ne(value); RclSListEntry ne(value);
RclSListEntry scratch; RclSListEntry scratch;
return insertNew(sk, ne, scratch, maxlen); return insertNew(sk, ne, scratch, maxlen);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2004 J.F.Dockes /* Copyright (C) 2004-2017 J.F.Dockes
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -35,13 +35,12 @@
* are sequential numeric, so this basically manages a set of lists. * are sequential numeric, so this basically manages a set of lists.
* *
* The code ensures that a a given value (as defined by the * The code ensures that a a given value (as defined by the
* DynConfEntry::equal() method is only stored once. If undesirable, * DynConfEntry::equal() method) is only stored once. If this is
* equal() should always return false. * undesirable, equal() should always return false.
*/ */
#include <string> #include <string>
#include <list> #include <list>
#include <utility>
#include "conftree.h" #include "conftree.h"
#include "base64.h" #include "base64.h"
@ -61,26 +60,20 @@ class DynConfEntry {
/** Stored object specialization for generic string storage */ /** Stored object specialization for generic string storage */
class RclSListEntry : public DynConfEntry { class RclSListEntry : public DynConfEntry {
public: public:
RclSListEntry() RclSListEntry() {}
{
}
virtual ~RclSListEntry() {} virtual ~RclSListEntry() {}
RclSListEntry(const std::string& v) RclSListEntry(const std::string& v)
: value(v) : value(v) {
{
} }
virtual bool decode(const std::string &enc) virtual bool decode(const std::string &enc) {
{
base64_decode(enc, value); base64_decode(enc, value);
return true; return true;
} }
virtual bool encode(std::string& enc) virtual bool encode(std::string& enc) {
{
base64_encode(value, enc); base64_encode(value, enc);
return true; return true;
} }
virtual bool equal(const DynConfEntry& other) virtual bool equal(const DynConfEntry& other) {
{
const RclSListEntry& e = dynamic_cast<const RclSListEntry&>(other); const RclSListEntry& e = dynamic_cast<const RclSListEntry&>(other);
return e.value == value; return e.value == value;
} }
@ -91,16 +84,18 @@ class RclSListEntry : public DynConfEntry {
/** The dynamic configuration class */ /** The dynamic configuration class */
class RclDynConf { class RclDynConf {
public: public:
RclDynConf(const std::string &fn) RclDynConf(const std::string &fn);
: m_data(fn.c_str())
{ bool ro() {
return m_data.getStatus() == ConfSimple::STATUS_RO;
} }
bool ok() bool rw() {
{
return m_data.getStatus() == ConfSimple::STATUS_RW; return m_data.getStatus() == ConfSimple::STATUS_RW;
} }
std::string getFilename() bool ok() {
{ return m_data.getStatus() != ConfSimple::STATUS_ERROR;
}
std::string getFilename() {
return m_data.getFilename(); return m_data.getFilename();
} }
@ -119,7 +114,8 @@ class RclDynConf {
// Specialized methods for simple string lists, designated by the // Specialized methods for simple string lists, designated by the
// subkey value // subkey value
bool enterString(const std::string sk, const std::string value, int maxlen = -1); bool enterString(const std::string sk, const std::string value,
int maxlen = -1);
std::list<std::string> getStringList(const std::string sk); std::list<std::string> getStringList(const std::string sk);
private: private: