+ // If the backup indicator is flipped, then this was a remote load and we need a backup.
+ // This is the best place to take a backup, since it's superficially "correct" metadata.
+ string backupKey;
+ if (m_backupIndicator) {
+ // 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 (exception& ex) {
+ m_log.crit("exception while backing up metadata: %s", ex.what());
+ backupKey.erase();
+ }
+ }
+
+ try {
+ doFilters(*xmlObject.get());
+ }
+ catch (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");
+ }
+