+ // This is the best place to take a backup, since it's superficially "correct" metadata.
+ string backupKey;
+ if (!backup && !m_backing.empty()) {
+ // We compute a random filename extension to the "real" location.
+ SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
+ backupKey = m_backing + '.' + SAMLArtifact::toHex(backupKey);
+ m_log.debug("backing up remote metadata resource to (%s)", backupKey.c_str());
+ try {
+ ofstream backer(backupKey.c_str());
+ backer << *(raw.second->getOwnerDocument());
+ }
+ catch (std::exception& ex) {
+ m_log.crit("exception while backing up metadata: %s", ex.what());
+ backupKey.erase();
+ }
+ }
+
+ try {
+ doFilters(*xmlObject);
+ }
+ catch (std::exception&) {
+ if (!backupKey.empty())
+ remove(backupKey.c_str());
+ throw;
+ }
+
+ if (!backupKey.empty()) {
+ m_log.debug("committing backup file to permanent location (%s)", m_backing.c_str());
+ Locker locker(getBackupLock());
+ remove(m_backing.c_str());
+ if (rename(backupKey.c_str(), m_backing.c_str()) != 0)
+ m_log.crit("unable to rename metadata backup file");
+ preserveCacheTag();
+ }
+
+ if (m_dropDOM) {
+ xmlObject->releaseThisAndChildrenDOM();
+ xmlObject->setDocument(nullptr);
+ }
+
+ // Swap it in after acquiring write lock if necessary.
+ if (m_lock)
+ m_lock->wrlock();
+ SharedLock locker(m_lock, false);
+ bool changed = m_object!=nullptr;
+ m_object.swap(xmlObject);
+ m_lastValidUntil = SAMLTIME_MAX;
+ index(m_lastValidUntil);
+ if (m_discoveryFeed)
+ generateFeed();