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

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