From b89b1d4d2cd1430c5efa5ffcb0f515365ba11f75 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Wed, 31 Mar 2010 21:51:05 +0000 Subject: [PATCH] Multi-line svn commit, see body. https://issues.shibboleth.net/jira/browse/CPPOST-44 https://issues.shibboleth.net/jira/browse/CPPOST-45 --- xmltooling/util/ReloadableXMLFile.cpp | 29 +++++++++++++++++++---------- xmltooling/util/ReloadableXMLFile.h | 7 +++++-- xmltooling/util/Threads.h | 11 +++++++---- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/xmltooling/util/ReloadableXMLFile.cpp b/xmltooling/util/ReloadableXMLFile.cpp index dfeab59..f8c4b00 100644 --- a/xmltooling/util/ReloadableXMLFile.cpp +++ b/xmltooling/util/ReloadableXMLFile.cpp @@ -62,7 +62,7 @@ static const XMLCh backingFilePath[] = UNICODE_LITERAL_15(b,a,c,k,i,n,g,F,i,l,e 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 @@ -341,15 +341,23 @@ pair ReloadableXMLFile::load(bool backup) 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; } } @@ -382,7 +390,8 @@ pair ReloadableXMLFile::background_load() { // 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(); } diff --git a/xmltooling/util/ReloadableXMLFile.h b/xmltooling/util/ReloadableXMLFile.h index 68a2b94..bf32fcf 100644 --- a/xmltooling/util/ReloadableXMLFile.h +++ b/xmltooling/util/ReloadableXMLFile.h @@ -126,8 +126,11 @@ namespace xmltooling { /** 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; diff --git a/xmltooling/util/Threads.h b/xmltooling/util/Threads.h index 443009c..d184d5c 100644 --- a/xmltooling/util/Threads.h +++ b/xmltooling/util/Threads.h @@ -287,14 +287,16 @@ namespace xmltooling * @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: @@ -314,7 +316,7 @@ namespace xmltooling * @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(); } @@ -322,7 +324,8 @@ namespace xmltooling * Unlocks the wrapped shared lock. */ ~SharedLock() { - rwlock->unlock(); + if (rwlock) + rwlock->unlock(); } private: -- 2.1.4