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
This commit is contained in:
csoler 2009-12-26 10:46:26 +00:00
parent 4a39adc90a
commit b5cd798a44
2 changed files with 43 additions and 20 deletions

View File

@ -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 ;
}
{

View File

@ -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();