Fixed file rename by using a common rsDirUtil function

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1103 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2009-03-29 13:58:28 +00:00
parent b3f433b5f2
commit 9b59f89cdf
6 changed files with 82 additions and 48 deletions

View File

@ -23,6 +23,7 @@
*
*/
#include "util/rsdir.h"
#include "rsiface/rspeers.h"
#include "pqi/p3cfgmgr.h"
#include "pqi/p3authmgr.h"
@ -507,22 +508,13 @@ bool p3Config::saveConfiguration()
std::cerr << "renaming " << fnametmp.c_str() << " to " << fname.c_str() << std::endl ;
#ifdef WIN32
std::wstring from,to ;
for(std::string::const_iterator it = fnametmp.begin(); it!=fnametmp.end();++it) from += *it;
for(std::string::const_iterator it = fname .begin(); it!=fname .end();++it) to += *it;
int err = !MoveFileEx(from.c_str(), to.c_str(), MOVEFILE_REPLACE_EXISTING) ;
#else
int err = rename(fnametmp.c_str(),fname.c_str());
#endif
if(0 != err)
if(!RsDirUtil::renameFile(fnametmp,fname))
{
std::ostringstream errlog;
#ifdef WIN32
errlog << err << ", " << errno ;
errlog << "Error " << GetLastError() ;
#else
errlog << err << :", " << GetLastError() ;
errlog << "Error " << errno ;
#endif
getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + fname + ": got error "+errlog.str());
return false ;

View File

@ -23,7 +23,9 @@
*
*/
#include "util/rsdir.h"
#include "pqi/pqibin.h"
#include "pqi/pqinotify.h"
#include "pqi/pqiarchive.h"
#include "services/p3chatservice.h"
@ -459,16 +461,11 @@ bool p3ChatService::saveConfiguration()
setHash(out->gethash());
delete pa_out;
#ifdef WIN32
std::wstring from,to ;
for(std::string::const_iterator it = msgfiletmp.begin(); it!=msgfiletmp.end();++it) from += *it;
for(std::string::const_iterator it = msgfile .begin(); it!=msgfile .end();++it) to += *it;
if(!MoveFileEx(from.c_str(), to.c_str(), MOVEFILE_REPLACE_EXISTING))
#else
if(0 != rename(msgfiletmp.c_str(),msgfile.c_str()))
#endif
if(!RsDirUtil::renameFile(msgfiletmp,msgfile))
{
getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + msgfile) ;
return false ;
}
return true;
}

View File

@ -31,6 +31,8 @@
#include "services/p3distrib.h"
#include "serialiser/rsdistribitems.h"
#include "util/rsdir.h"
#include "pqi/pqinotify.h"
#include "pqi/pqibin.h"
/*****
@ -788,18 +790,16 @@ void p3GroupDistrib::locked_publishPendingMsgs()
mPendingPublish.clear();
delete streamer;
if(!RsDirUtil::renameFile(filenametmp,filename))
{
std::ostringstream errlog;
#ifdef WIN32
std::wstring from,to ;
for(std::string::const_iterator it = filenametmp.begin(); it!=filenametmp.end();++it) from += *it;
for(std::string::const_iterator it = filename .begin(); it!=filename .end();++it) to += *it;
if(!MoveFileEx(from.c_str(), to.c_str(), MOVEFILE_REPLACE_EXISTING))
errlog << "Error " << GetLastError() ;
#else
if(0 != rename(filenametmp.c_str(),filename.c_str()))
errlog << "Error " << errno ;
#endif
std::cerr << "Could not rename file " << filenametmp << " into " << filename << std::endl ;
else
std::cerr << "Successfull wrote file " << filename << std::endl ;
getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + filename + ": got error "+errlog.str());
}
/* indicate not to save for a while */
mLastPublishTime = now;
@ -933,18 +933,16 @@ void p3GroupDistrib::publishDistribGroups()
/* cleanup */
delete streamer;
if(!RsDirUtil::renameFile(filenametmp,filename))
{
std::ostringstream errlog;
#ifdef WIN32
std::wstring from,to ;
for(std::string::const_iterator it = filenametmp.begin(); it!=filenametmp.end();++it) from += *it;
for(std::string::const_iterator it = filename .begin(); it!=filename .end();++it) to += *it;
if(!MoveFileEx(from.c_str(), to.c_str(), MOVEFILE_REPLACE_EXISTING))
errlog << "Error " << GetLastError() ;
#else
if(0 != rename(filenametmp.c_str(),filename.c_str()))
errlog << "Error " << errno ;
#endif
std::cerr << "Could not rename file " << filenametmp << " into " << filename << std::endl ;
else
std::cerr << "Successfull wrote file " << filename << std::endl ;
getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + filename + ": got error "+errlog.str());
}
/* push file to CacheSource */
refreshCache(newCache);

View File

@ -289,16 +289,11 @@ bool p3MsgService::saveConfiguration()
if(!written)
return false ;
#ifdef WIN32
std::wstring from,to ;
for(std::string::const_iterator it = msgfiletmp.begin(); it!=msgfiletmp.end();++it) from += *it;
for(std::string::const_iterator it = msgfile .begin(); it!=msgfile .end();++it) to += *it;
if(!MoveFileEx(from.c_str(), to.c_str(), MOVEFILE_REPLACE_EXISTING))
#else
if(0 != rename(msgfiletmp.c_str(),msgfile.c_str()))
#endif
if(!RsDirUtil::renameFile(msgfiletmp,msgfile))
{
getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + msgfile) ;
return false ;
}
return true;
}

View File

@ -34,6 +34,13 @@
#include <iostream>
#include <algorithm>
#if defined(WIN32) || defined(__CYGWIN__)
#include "wtypes.h"
#include <winioctl.h>
#else
#include <errno.h>
#endif
/****
* #define RSDIR_DEBUG 1
****/
@ -379,3 +386,44 @@ bool RsDirUtil::getFileHash(std::string filepath,
return true;
}
bool RsDirUtil::renameFile(const std::string& from, const std::string& to)
{
int loops = 0;
#ifdef WIN32
#ifdef MINGW
std::string f(from),t(to) ;
#else
std::wstring f,t ;
for(std::string::const_iterator it = from.begin(); it!=from.end();++it) f += *it;
for(std::string::const_iterator it = to .begin(); it!=to .end();++it) t += *it;
#endif
while (!MoveFileEx(f.c_str(), t.c_str(), MOVEFILE_REPLACE_EXISTING))
#else
while (rename(from.c_str(), to.c_str()) < 0)
#endif
{
#ifdef WIN32
if (GetLastError() != ERROR_ACCESS_DENIED)
#else
if (errno != EACCES)
#endif
/* set errno? */
return false ;
#ifdef WIN32
Sleep(100000); /* us */
#else
usleep(100000); /* us */
#endif
if (loops >= 30)
return false ;
loops++;
}
return true ;
}

View File

@ -40,6 +40,10 @@ std::string removeTopDir(std::string dir);
std::string removeRootDirs(std::string path, std::string root);
// Renames file from to file to. Files should be on the same file system.
// returns true if succeed, false otherwise.
bool renameFile(const std::string& from,const std::string& to) ;
int breakupDirList(std::string path,
std::list<std::string> &subdirs);