ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log)
- : m_root(e), m_local(true), m_validate(false), m_autocommit(true), m_filestamp(0), m_reloadInterval(0), m_lock(NULL), m_log(log),
+ : m_root(e), m_local(true), m_validate(false), m_backupIndicator(true), m_filestamp(0), m_reloadInterval(0), m_lock(NULL), m_log(log),
m_shutdown(false), m_reload_wait(NULL), m_reload_thread(NULL)
{
#ifdef _DEBUG
m_log.infoStream() << "loaded XML resource (" << (backup ? m_backing : m_source) << ")" << logging::eol;
- if (!backup && m_autocommit && !m_backing.empty()) {
- m_log.debug("backing up remote resource to (%s)", m_backing.c_str());
- try {
- Locker locker(getBackupLock());
- ofstream backer(m_backing.c_str());
- backer << *doc;
+ if (!backup && !m_backing.empty()) {
+ // If the indicator is true, we're responsible for the backup.
+ if (m_backupIndicator) {
+ m_log.debug("backing up remote resource to (%s)", m_backing.c_str());
+ try {
+ Locker locker(getBackupLock());
+ ofstream backer(m_backing.c_str());
+ backer << *doc;
+ }
+ catch (exception& ex) {
+ m_log.crit("exception while backing up resource: %s", ex.what());
+ }
}
- catch (exception& ex) {
- m_log.crit("exception while backing up resource: %s", ex.what());
+ else {
+ // If the indicator was false, set true to signal that a backup is needed.
+ // The caller will presumably flip it back to false once that's done.
+ m_backupIndicator = true;
}
}
{
// If this method isn't overridden, we acquire a write lock
// and just call the old override.
- m_lock->wrlock();
+ if (m_lock)
+ m_lock->wrlock();
SharedLock locker(m_lock, false);
return load();
}
/** Path to backup copy for remote resource. */
std::string m_backing;
- /** Indicates whether loading process needs to defer creation of backup file. */
- bool m_autocommit;
+ /**
+ * Before load, indicates whether the backup is handled by the base class,
+ * after load, will be true iff it started false and a backup needs to be done.
+ */
+ bool m_backupIndicator;
/** Last modification of local resource. */
time_t m_filestamp;
* @param mtx mutex to lock
*/
Lock(Mutex* mtx) : mutex(mtx) {
- mutex->lock();
+ if (mutex)
+ mutex->lock();
}
/**
* Unlocks the wrapped mutex.
*/
~Lock() {
- mutex->unlock();
+ if (mutex)
+ mutex->unlock();
}
private:
* @param lockit true if the lock should be acquired here, false if already acquired
*/
SharedLock(RWLock* lock, bool lockit=true) : rwlock(lock) {
- if (lockit)
+ if (rwlock && lockit)
rwlock->rdlock();
}
* Unlocks the wrapped shared lock.
*/
~SharedLock() {
- rwlock->unlock();
+ if (rwlock)
+ rwlock->unlock();
}
private: