From b5cd798a44c555950708f6edb202b3c19f8b4931 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 26 Dec 2009 10:46:26 +0000 Subject: [PATCH] improved file list saving by saving into a temporary file before renaming (Should be ported to trunk) git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.4.x@1936 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dbase/fimonitor.cc | 60 ++++++++++++++++++---------- libretroshare/src/dbase/findex.cc | 3 ++ 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/libretroshare/src/dbase/fimonitor.cc b/libretroshare/src/dbase/fimonitor.cc index 7580ed936..09823dfa9 100644 --- a/libretroshare/src/dbase/fimonitor.cc +++ b/libretroshare/src/dbase/fimonitor.cc @@ -556,7 +556,7 @@ void FileIndexMonitor::updateCycle() if (fiMods) { /* store to the cacheDirectory */ - fiMutex.lock(); { /* LOCKED DIRS */ + RsStackMutex mt(fiMutex) ;/* LOCKED DIRS */ std::string path = getCacheDir(); std::ostringstream out; @@ -575,34 +575,54 @@ void FileIndexMonitor::updateCycle() std::string calchash; uint64_t size; - fi.saveIndex(fname, calchash, size); + std::string fnametmp = fname+".tmp" ; + + if(fi.saveIndex(fnametmp, calchash, size)) + { + std::cerr << "renaming " << fnametmp.c_str() << " to " << fname.c_str() << std::endl ; + + if(!RsDirUtil::renameFile(fnametmp,fname)) + { + std::ostringstream errlog; +#ifdef WIN32 + errlog << "Error " << GetLastError() ; +#else + errlog << "Error " << errno ; +#endif + std::cerr << "fimonitor::updateCycle(): ERROR: File rename error: got error "+errlog.str() << std::endl ; + return ; + } + + std::cerr << "Successfully wrote p3config file " << fname.c_str() << std::endl ; + #ifdef FIM_DEBUG - std::cerr << "FileIndexMonitor::updateCycle() saved with hash:" << calchash; - std::cerr << std::endl; + std::cerr << "FileIndexMonitor::updateCycle() saved with hash:" << calchash; + std::cerr << std::endl; #endif - /* should clean up the previous cache.... */ + /* should clean up the previous cache.... */ - /* flag as new info */ - CacheData data; - data.pid = fi.root->id; - data.cid.type = getCacheType(); - data.cid.subid = 0; - data.path = path; - data.name = tmpname; - data.hash = calchash; - data.size = size; - data.recvd = time(NULL); + /* flag as new info */ + CacheData data; + data.pid = fi.root->id; + data.cid.type = getCacheType(); + data.cid.subid = 0; + data.path = path; + data.name = tmpname; + data.hash = calchash; + data.size = size; + data.recvd = time(NULL); - updateCache(data); + updateCache(data); #ifdef FIM_DEBUG - std::cerr << "FileIndexMonitor::updateCycle() called updateCache()"; - std::cerr << std::endl; + std::cerr << "FileIndexMonitor::updateCycle() called updateCache()"; + std::cerr << std::endl; #endif - - } fiMutex.unlock(); /* UNLOCKED DIRS */ + } + else + std::cerr << "fimonitor::updateCycle(): ERROR: Could not save file index file!! Check permissions and disk availability" << std::endl ; } { diff --git a/libretroshare/src/dbase/findex.cc b/libretroshare/src/dbase/findex.cc index 4d5235311..361591c6d 100644 --- a/libretroshare/src/dbase/findex.cc +++ b/libretroshare/src/dbase/findex.cc @@ -913,6 +913,9 @@ int FileIndex::saveIndex(std::string filename, std::string &fileHash, uint64_t & /* finally, save to file */ file << oss.str(); + if(file.fail()) + return 0 ; + /* get the size out */ size=file.tellp(); file.close();