Merge branch 'improve-safety'
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 7 Apr 2021 10:33:43 +0000 (12:33 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 7 Apr 2021 10:33:43 +0000 (12:33 +0200)
CMakeLists.txt
src/dns/dnscache.cpp
test/data/dns_cache_compatibility_test.xml

index d36b471..b5e89d8 100644 (file)
@@ -1,7 +1,7 @@
 # project: definitions
 project(pingcheck)
-set(VERSION 0.7)
-set(VERSION_REVISION 1)
+set(VERSION 0.8)
+set(VERSION_REVISION 0)
 set(TARGET ${PROJECT_NAME})
 
 # cmake: build options
index 37d62c9..0e9a270 100644 (file)
@@ -26,6 +26,7 @@
 #include <fstream>
 #include <logfunc.hpp>
 #include <filefunc.hxx>   // I2n::file_exists
+#include <tmpfstream.hpp>
 #include <boost/foreach.hpp>
 #include <boost/bind.hpp>
 #include <boost/asio/placeholders.hpp>
@@ -137,7 +138,7 @@ void DnsCache::save_to_cachefile()
             std::string cache_save_time_str = boost::posix_time::to_iso_string(
                             boost::posix_time::second_clock::universal_time() );
 
-            std::ofstream ofs( CacheFile.c_str() );
+            I2n::tmpofcopystream ofs( CacheFile.c_str() );
             boost::archive::xml_oarchive oa(ofs);
             oa & BOOST_SERIALIZATION_NVP(IpCache);
             oa & BOOST_SERIALIZATION_NVP(CnameCache);
@@ -171,16 +172,22 @@ void DnsCache::load_from_cachefile()
             std::ifstream ifs( CacheFile.c_str() );
             boost::archive::xml_iarchive ia(ifs);
 
+            ip_map_type new_IpCache;
+            cname_map_type new_CnameCache;
             std::string cache_save_time_str;
 
-            ia & BOOST_SERIALIZATION_NVP(IpCache);
-            ia & BOOST_SERIALIZATION_NVP(CnameCache);
+            ia & BOOST_SERIALIZATION_NVP(new_IpCache);
+            ia & BOOST_SERIALIZATION_NVP(new_CnameCache);
             ia & BOOST_SERIALIZATION_NVP(cache_save_time_str);
 
-            boost::posix_time::ptime cache_save_time
+            const boost::posix_time::ptime cache_save_time
                     = boost::posix_time::from_iso_string(cache_save_time_str);
             GlobalLogger.info() << "DnsCache: loaded from file " << CacheFile;
 
+            // atomic switch over
+            IpCache.swap(new_IpCache);
+            CnameCache.swap(new_CnameCache);
+
             check_timestamps(cache_save_time);
         }
         catch (boost::archive::archive_exception &exc)
index 6a1e045..eeaf240 100644 (file)
@@ -66,5 +66,5 @@
                </second>
        </item>
 </CnameCache>
+<cache_save_time_str>20150522T084242</cache_save_time_str>
 </boost_serialization>
-