Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ReloadableXMLFile.cpp
index ca8ad07..f8c4b00 100644 (file)
@@ -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_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
@@ -129,7 +129,7 @@ ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log)
                 auto_ptr_char temp2(source);
                 m_backing=temp2.get();
                 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_backing, PathResolver::XMLTOOLING_RUN_FILE);
-                log.debug("backup remote resource with (%s)", m_backing.c_str());
+                log.debug("backup remote resource to (%s)", m_backing.c_str());
             }
             source = e->getAttributeNS(NULL,reloadInterval);
             if (source && *source) {
@@ -308,6 +308,8 @@ pair<bool,DOMElement*> ReloadableXMLFile::load(bool backup)
             DOMDocument* doc=NULL;
             if (m_local || backup) {
                 auto_ptr_XMLCh widenit(backup ? m_backing.c_str() : m_source.c_str());
+                // Use library-wide lock for now, nothing else is using it anyway.
+                Locker locker(backup ? getBackupLock() : NULL);
                 LocalFileInputSource src(widenit.get());
                 Wrapper4InputSource dsrc(&src, false);
                 if (m_validate)
@@ -340,13 +342,22 @@ pair<bool,DOMElement*> ReloadableXMLFile::load(bool backup)
             m_log.infoStream() << "loaded XML resource (" << (backup ? m_backing : m_source) << ")" << logging::eol;
 
             if (!backup && !m_backing.empty()) {
-                m_log.debug("backing up remote resource to (%s)", m_backing.c_str());
-                try {
-                    ofstream backer(m_backing.c_str());
-                    backer << *doc;
+                // 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;
                 }
             }
 
@@ -379,7 +390,13 @@ pair<bool,DOMElement*> 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();
 }
+
+Lockable* ReloadableXMLFile::getBackupLock()
+{
+    return &XMLToolingConfig::getConfig();
+}