mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-24 14:23:36 -05:00
replaced explicit old code in cleanupDirectory() by new code based on FolderIterator
This commit is contained in:
parent
957d48b5fc
commit
4e48005047
@ -34,6 +34,7 @@
|
|||||||
#include "util/rsstring.h"
|
#include "util/rsstring.h"
|
||||||
#include "util/rsrandom.h"
|
#include "util/rsrandom.h"
|
||||||
#include "util/rsmemory.h"
|
#include "util/rsmemory.h"
|
||||||
|
#include "util/folderiterator.h"
|
||||||
#include "retroshare/rstypes.h"
|
#include "retroshare/rstypes.h"
|
||||||
#include "rsthreads.h"
|
#include "rsthreads.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -437,200 +438,13 @@ bool RsDirUtil::checkCreateDirectory(const std::string& dir)
|
|||||||
|
|
||||||
bool RsDirUtil::cleanupDirectory(const std::string& cleandir, const std::set<std::string> &keepFiles)
|
bool RsDirUtil::cleanupDirectory(const std::string& cleandir, const std::set<std::string> &keepFiles)
|
||||||
{
|
{
|
||||||
|
for(librs::util::FolderIterator it(cleandir);it.isValid();it.next())
|
||||||
|
if(it.file_type() == librs::util::FolderIterator::TYPE_FILE && (keepFiles.end() == std::find(keepFiles.begin(), keepFiles.end(), it.file_name())))
|
||||||
|
remove( (cleandir + "/" + it.file_name()).c_str() ) ;
|
||||||
|
|
||||||
/* check for the dir existance */
|
return true;
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
std::wstring wcleandir;
|
|
||||||
librs::util::ConvertUtf8ToUtf16(cleandir, wcleandir);
|
|
||||||
_WDIR *dir = _wopendir(wcleandir.c_str());
|
|
||||||
#else
|
|
||||||
DIR *dir = opendir(cleandir.c_str());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
if (!dir)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
struct _wdirent *dent;
|
|
||||||
struct _stat buf;
|
|
||||||
|
|
||||||
while(NULL != (dent = _wreaddir(dir)))
|
|
||||||
#else
|
|
||||||
struct dirent *dent;
|
|
||||||
struct stat buf;
|
|
||||||
|
|
||||||
while(NULL != (dent = readdir(dir)))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
/* check entry type */
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
const std::wstring &wfname = dent -> d_name;
|
|
||||||
std::wstring wfullname = wcleandir + L"/" + wfname;
|
|
||||||
#else
|
|
||||||
const std::string &fname = dent -> d_name;
|
|
||||||
std::string fullname = cleandir + "/" + fname;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
if (-1 != _wstat(wfullname.c_str(), &buf))
|
|
||||||
#else
|
|
||||||
if (-1 != stat(fullname.c_str(), &buf))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
/* only worry about files */
|
|
||||||
if (S_ISREG(buf.st_mode))
|
|
||||||
{
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
std::string fname;
|
|
||||||
librs::util::ConvertUtf16ToUtf8(wfname, fname);
|
|
||||||
#endif
|
|
||||||
/* check if we should keep it */
|
|
||||||
if (keepFiles.end() == std::find(keepFiles.begin(), keepFiles.end(), fname))
|
|
||||||
{
|
|
||||||
/* can remove */
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
_wremove(wfullname.c_str());
|
|
||||||
#else
|
|
||||||
remove(fullname.c_str());
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* close directory */
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
_wclosedir(dir);
|
|
||||||
#else
|
|
||||||
closedir(dir);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* faster cleanup - first construct two sets - then iterate over together */
|
|
||||||
bool RsDirUtil::cleanupDirectoryFaster(const std::string& cleandir, const std::set<std::string> &keepFiles)
|
|
||||||
{
|
|
||||||
|
|
||||||
/* check for the dir existance */
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
std::map<std::string, std::wstring> fileMap;
|
|
||||||
std::map<std::string, std::wstring>::const_iterator fit;
|
|
||||||
|
|
||||||
std::wstring wcleandir;
|
|
||||||
librs::util::ConvertUtf8ToUtf16(cleandir, wcleandir);
|
|
||||||
_WDIR *dir = _wopendir(wcleandir.c_str());
|
|
||||||
#else
|
|
||||||
std::map<std::string, std::string> fileMap;
|
|
||||||
std::map<std::string, std::string>::const_iterator fit;
|
|
||||||
|
|
||||||
DIR *dir = opendir(cleandir.c_str());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!dir)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
struct _wdirent *dent;
|
|
||||||
struct _stat buf;
|
|
||||||
|
|
||||||
while(NULL != (dent = _wreaddir(dir)))
|
|
||||||
{
|
|
||||||
const std::wstring &wfname = dent -> d_name;
|
|
||||||
std::wstring wfullname = wcleandir + L"/" + wfname;
|
|
||||||
|
|
||||||
if (-1 != _wstat(wfullname.c_str(), &buf))
|
|
||||||
{
|
|
||||||
/* only worry about files */
|
|
||||||
if (S_ISREG(buf.st_mode))
|
|
||||||
{
|
|
||||||
std::string fname;
|
|
||||||
librs::util::ConvertUtf16ToUtf8(wfname, fname);
|
|
||||||
fileMap[fname] = wfullname;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
struct dirent *dent;
|
|
||||||
struct stat buf;
|
|
||||||
|
|
||||||
while(NULL != (dent = readdir(dir)))
|
|
||||||
{
|
|
||||||
const std::string &fname = dent -> d_name;
|
|
||||||
std::string fullname = cleandir + "/" + fname;
|
|
||||||
|
|
||||||
if (-1 != stat(fullname.c_str(), &buf))
|
|
||||||
{
|
|
||||||
/* only worry about files */
|
|
||||||
if (S_ISREG(buf.st_mode))
|
|
||||||
{
|
|
||||||
fileMap[fname] = fullname;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::set<std::string>::const_iterator kit;
|
|
||||||
|
|
||||||
fit = fileMap.begin();
|
|
||||||
kit = keepFiles.begin();
|
|
||||||
|
|
||||||
while(fit != fileMap.end() && kit != keepFiles.end())
|
|
||||||
{
|
|
||||||
if (fit->first < *kit) // fit is not in keep list;
|
|
||||||
{
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
_wremove(fit->second.c_str());
|
|
||||||
#else
|
|
||||||
remove(fit->second.c_str());
|
|
||||||
#endif
|
|
||||||
++fit;
|
|
||||||
}
|
|
||||||
else if (*kit < fit->first) // keepitem doesn't exist.
|
|
||||||
{
|
|
||||||
++kit;
|
|
||||||
}
|
|
||||||
else // in keep list.
|
|
||||||
{
|
|
||||||
++fit;
|
|
||||||
++kit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// cleanup extra that aren't in keep list.
|
|
||||||
while(fit != fileMap.end())
|
|
||||||
{
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
_wremove(fit->second.c_str());
|
|
||||||
#else
|
|
||||||
remove(fit->second.c_str());
|
|
||||||
#endif
|
|
||||||
++fit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* close directory */
|
|
||||||
#ifdef WINDOWS_SYS
|
|
||||||
_wclosedir(dir);
|
|
||||||
#else
|
|
||||||
closedir(dir);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* slightly nicer helper function */
|
/* slightly nicer helper function */
|
||||||
bool RsDirUtil::hashFile(const std::string& filepath,
|
bool RsDirUtil::hashFile(const std::string& filepath,
|
||||||
std::string &name, RsFileHash &hash, uint64_t &size)
|
std::string &name, RsFileHash &hash, uint64_t &size)
|
||||||
|
Loading…
Reference in New Issue
Block a user