mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
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:
parent
b3f433b5f2
commit
9b59f89cdf
@ -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 ;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 ;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user