mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-20 12:24:24 -04:00
fixed possible uninitialised memory read in folderiterator for windows, and changed file folderiterator.cc to unix format
This commit is contained in:
parent
fb302002d6
commit
d433713bd0
2 changed files with 210 additions and 181 deletions
|
@ -19,6 +19,13 @@ namespace librs { namespace util {
|
||||||
FolderIterator::FolderIterator(const std::string& folderName)
|
FolderIterator::FolderIterator(const std::string& folderName)
|
||||||
: mFolderName(folderName)
|
: mFolderName(folderName)
|
||||||
{
|
{
|
||||||
|
is_open = false ;
|
||||||
|
validity = false ;
|
||||||
|
mFileModTime = 0;
|
||||||
|
mFolderModTime = 0;
|
||||||
|
mFileSize = 0;
|
||||||
|
mType = TYPE_UNKNOWN;
|
||||||
|
|
||||||
// Grab the last modification time for the directory
|
// Grab the last modification time for the directory
|
||||||
|
|
||||||
struct stat64 buf ;
|
struct stat64 buf ;
|
||||||
|
@ -45,9 +52,20 @@ FolderIterator::FolderIterator(const std::string& folderName)
|
||||||
|
|
||||||
utf16Name += L"/*.*";
|
utf16Name += L"/*.*";
|
||||||
|
|
||||||
|
// FindFirstFileW does the "next" operation, so after calling it, we need to read the information
|
||||||
|
// for the current entry.
|
||||||
|
|
||||||
handle = FindFirstFileW(utf16Name.c_str(), &fileInfo);
|
handle = FindFirstFileW(utf16Name.c_str(), &fileInfo);
|
||||||
is_open = validity = handle != INVALID_HANDLE_VALUE;
|
is_open = validity = handle != INVALID_HANDLE_VALUE;
|
||||||
|
|
||||||
|
bool should_skip ;
|
||||||
|
validity = validity && updateFileInfo(should_skip);
|
||||||
|
|
||||||
|
if(validity && should_skip)
|
||||||
|
next();
|
||||||
#else
|
#else
|
||||||
|
// On linux, we need to call "next()" once the dir is openned. next() will call updateFileInfo() itself.
|
||||||
|
|
||||||
handle = opendir(folderName.c_str());
|
handle = opendir(folderName.c_str());
|
||||||
is_open = validity = handle != NULL;
|
is_open = validity = handle != NULL;
|
||||||
next();
|
next();
|
||||||
|
@ -61,8 +79,25 @@ FolderIterator::~FolderIterator()
|
||||||
|
|
||||||
void FolderIterator::next()
|
void FolderIterator::next()
|
||||||
{
|
{
|
||||||
|
bool should_skip = false ;
|
||||||
|
|
||||||
while(readdir())
|
while(readdir())
|
||||||
{
|
if(updateFileInfo(should_skip) && !should_skip)
|
||||||
|
return ;
|
||||||
|
|
||||||
|
#ifdef DEBUG_FOLDER_ITERATOR
|
||||||
|
std::cerr << "End of directory." << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
mType = TYPE_UNKNOWN ;
|
||||||
|
mFileSize = 0 ;
|
||||||
|
mFileModTime = 0;
|
||||||
|
validity = false ;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FolderIterator::updateFileInfo(bool& should_skip)
|
||||||
|
{
|
||||||
|
should_skip = false;
|
||||||
#ifdef WINDOWS_SYS
|
#ifdef WINDOWS_SYS
|
||||||
ConvertUtf16ToUtf8(fileInfo.cFileName, mFileName) ;
|
ConvertUtf16ToUtf8(fileInfo.cFileName, mFileName) ;
|
||||||
#else
|
#else
|
||||||
|
@ -70,7 +105,10 @@ void FolderIterator::next()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(mFileName == "." || mFileName == "..")
|
if(mFileName == "." || mFileName == "..")
|
||||||
continue ;
|
{
|
||||||
|
should_skip = true ;
|
||||||
|
return true ;
|
||||||
|
}
|
||||||
|
|
||||||
mFullPath = mFolderName + "/" + mFileName ;
|
mFullPath = mFolderName + "/" + mFileName ;
|
||||||
|
|
||||||
|
@ -89,7 +127,6 @@ void FolderIterator::next()
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
mFileModTime = buf.st_mtime ;
|
mFileModTime = buf.st_mtime ;
|
||||||
mStatInfoOk = true;
|
|
||||||
|
|
||||||
if (S_ISDIR(buf.st_mode))
|
if (S_ISDIR(buf.st_mode))
|
||||||
{
|
{
|
||||||
|
@ -101,7 +138,7 @@ void FolderIterator::next()
|
||||||
mFileSize = 0 ;
|
mFileSize = 0 ;
|
||||||
mFileModTime = buf.st_mtime;
|
mFileModTime = buf.st_mtime;
|
||||||
|
|
||||||
return ;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (S_ISREG(buf.st_mode))
|
if (S_ISREG(buf.st_mode))
|
||||||
|
@ -114,7 +151,7 @@ void FolderIterator::next()
|
||||||
mFileSize = buf.st_size;
|
mFileSize = buf.st_size;
|
||||||
mFileModTime = buf.st_mtime;
|
mFileModTime = buf.st_mtime;
|
||||||
|
|
||||||
return ;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,15 +162,8 @@ void FolderIterator::next()
|
||||||
mType = TYPE_UNKNOWN ;
|
mType = TYPE_UNKNOWN ;
|
||||||
mFileSize = 0 ;
|
mFileSize = 0 ;
|
||||||
mFileModTime = 0;
|
mFileModTime = 0;
|
||||||
}
|
|
||||||
#ifdef DEBUG_FOLDER_ITERATOR
|
|
||||||
std::cerr << "End of directory." << std::endl;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mType = TYPE_UNKNOWN ;
|
return false;
|
||||||
mFileSize = 0 ;
|
|
||||||
mFileModTime = 0;
|
|
||||||
validity = false ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FolderIterator::readdir()
|
bool FolderIterator::readdir()
|
||||||
|
|
|
@ -59,9 +59,8 @@ private:
|
||||||
DIR* handle;
|
DIR* handle;
|
||||||
struct dirent* ent;
|
struct dirent* ent;
|
||||||
#endif
|
#endif
|
||||||
void updateStatsInfo() ;
|
bool updateFileInfo(bool &should_skip) ;
|
||||||
|
|
||||||
bool mStatInfoOk ;
|
|
||||||
time_t mFileModTime ;
|
time_t mFileModTime ;
|
||||||
time_t mFolderModTime ;
|
time_t mFolderModTime ;
|
||||||
uint64_t mFileSize ;
|
uint64_t mFileSize ;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue