From e55840c624def32e26040e025e70e355e79d84f4 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Wed, 6 Dec 2017 10:14:04 +0100 Subject: [PATCH] dynconf/query history: add readonly mode --- src/query/dynconf.cpp | 30 +++++++++++++++++++++++++++--- src/query/dynconf.h | 42 +++++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/query/dynconf.cpp b/src/query/dynconf.cpp index 17e6338a..3b273628 100644 --- a/src/query/dynconf.cpp +++ b/src/query/dynconf.cpp @@ -16,9 +16,7 @@ */ #ifndef TEST_HISTORY -#include -#include -#include +#include "safeunistd.h" #include "dynconf.h" #include "base64.h" @@ -33,10 +31,28 @@ const string allEdbsSk = "allExtDbs"; const string actEdbsSk = "actExtDbs"; 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, 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 vector names = m_data.getNames(sk); vector::const_iterator it; @@ -90,6 +106,10 @@ bool RclDynConf::insertNew(const string &sk, DynConfEntry &n, DynConfEntry &s, bool RclDynConf::eraseAll(const string &sk) { + if (!rw()) { + LOGDEB("RclDynConf::eraseAll: not writable\n"); + return false; + } vector names = m_data.getNames(sk); vector::const_iterator 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) { + if (!rw()) { + LOGDEB("RclDynConf::enterString: not writable\n"); + return false; + } RclSListEntry ne(value); RclSListEntry scratch; return insertNew(sk, ne, scratch, maxlen); diff --git a/src/query/dynconf.h b/src/query/dynconf.h index e6885b79..1374b37f 100644 --- a/src/query/dynconf.h +++ b/src/query/dynconf.h @@ -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 * it under the terms of the GNU General Public License as published by * 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. * * The code ensures that a a given value (as defined by the - * DynConfEntry::equal() method is only stored once. If undesirable, - * equal() should always return false. + * DynConfEntry::equal() method) is only stored once. If this is + * undesirable, equal() should always return false. */ #include #include -#include #include "conftree.h" #include "base64.h" @@ -61,26 +60,20 @@ class DynConfEntry { /** Stored object specialization for generic string storage */ class RclSListEntry : public DynConfEntry { public: - RclSListEntry() - { - } + RclSListEntry() {} virtual ~RclSListEntry() {} 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); return true; } - virtual bool encode(std::string& enc) - { + virtual bool encode(std::string& enc) { base64_encode(value, enc); return true; } - virtual bool equal(const DynConfEntry& other) - { + virtual bool equal(const DynConfEntry& other) { const RclSListEntry& e = dynamic_cast(other); return e.value == value; } @@ -91,16 +84,18 @@ class RclSListEntry : public DynConfEntry { /** The dynamic configuration class */ class RclDynConf { public: - RclDynConf(const std::string &fn) - : m_data(fn.c_str()) - { + RclDynConf(const std::string &fn); + + bool ro() { + return m_data.getStatus() == ConfSimple::STATUS_RO; } - bool ok() - { + bool 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(); } @@ -119,7 +114,8 @@ class RclDynConf { // Specialized methods for simple string lists, designated by the // 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 getStringList(const std::string sk); private: