From 1125dfe6d1e07899e5f6c1f71bd5590e0d469a2d Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 25 Sep 2016 23:42:20 +0200 Subject: [PATCH] fixed folder iterator to properly handle broken symbolic links --- .../src/file_sharing/dir_hierarchy.h | 2 +- libretroshare/src/util/folderiterator.cc | 102 +++++++++++------- 2 files changed, 62 insertions(+), 42 deletions(-) diff --git a/libretroshare/src/file_sharing/dir_hierarchy.h b/libretroshare/src/file_sharing/dir_hierarchy.h index 9320f3102..7220aacc8 100644 --- a/libretroshare/src/file_sharing/dir_hierarchy.h +++ b/libretroshare/src/file_sharing/dir_hierarchy.h @@ -144,7 +144,7 @@ public: bool searchHash(const RsFileHash& hash,std::list& results); int searchBoolExp(RsRegularExpression::Expression * exp, std::list &results) const ; - int searchTerms(const std::list& terms, std::list &results) const ; + int searchTerms(const std::list& terms, std::list &results) const ; // does a logical OR between items of the list of terms bool check(std::string& error_string) const ;// checks consistency of storage. diff --git a/libretroshare/src/util/folderiterator.cc b/libretroshare/src/util/folderiterator.cc index 415aeb085..62d451e21 100644 --- a/libretroshare/src/util/folderiterator.cc +++ b/libretroshare/src/util/folderiterator.cc @@ -11,6 +11,8 @@ #include "folderiterator.h" #include "rsstring.h" +#define DEBUG_FOLDER_ITERATOR 1 + namespace librs { namespace util { @@ -59,57 +61,75 @@ FolderIterator::~FolderIterator() void FolderIterator::next() { - do { - if(!readdir()) - { - validity = false ; - break ; - } - + while(readdir()) + { d_name(mFileName); - } while(mFileName == "." || mFileName == "..") ; - mFullPath = mFolderName + "/" + mFileName ; + if(mFileName == "." || mFileName == "..") + continue ; - struct stat64 buf ; + mFullPath = mFolderName + "/" + mFileName ; + + struct stat64 buf ; + +#ifdef DEBUG_FOLDER_ITERATOR + std::cerr << "FolderIterator: next. Looking into file " << mFileName ; +#endif #ifdef WINDOWS_SYS - std::wstring wfullname; - librs::util::ConvertUtf8ToUtf16(mFullPath, wfullname); - if ( 0 == _wstati64(wfullname.c_str(), &buf)) + std::wstring wfullname; + librs::util::ConvertUtf8ToUtf16(mFullPath, wfullname); + if ( 0 == _wstati64(wfullname.c_str(), &buf)) #else - if ( 0 == stat64(mFullPath.c_str(), &buf)) + if ( 0 == stat64(mFullPath.c_str(), &buf)) #endif - { - mFileModTime = buf.st_mtime ; - mStatInfoOk = true; + { + mFileModTime = buf.st_mtime ; + mStatInfoOk = true; - if (S_ISDIR(buf.st_mode)) - { - mType = TYPE_DIR ; - mFileSize = 0 ; - mFileModTime = buf.st_mtime; - } - else if (S_ISREG(buf.st_mode)) - { - mType = TYPE_FILE ; - mFileSize = buf.st_size; - mFileModTime = buf.st_mtime; - } - else - { - mType = TYPE_UNKNOWN ; - mFileSize = 0 ; - mFileModTime = 0; + if (S_ISDIR(buf.st_mode)) + { +#ifdef DEBUG_FOLDER_ITERATOR + std::cerr << ": is a directory" << std::endl; +#endif + + mType = TYPE_DIR ; + mFileSize = 0 ; + mFileModTime = buf.st_mtime; + + return ; + } + + if (S_ISREG(buf.st_mode)) + { +#ifdef DEBUG_FOLDER_ITERATOR + std::cerr << ": is a file" << std::endl; +#endif + + mType = TYPE_FILE ; + mFileSize = buf.st_size; + mFileModTime = buf.st_mtime; + + return ; + } } + +#ifdef DEBUG_FOLDER_ITERATOR + std::cerr << ": is unknown skipping" << std::endl; +#endif + + mType = TYPE_UNKNOWN ; + mFileSize = 0 ; + mFileModTime = 0; } - else - { - mType = TYPE_UNKNOWN ; - mFileSize = 0 ; - mFileModTime = 0; - validity = false ; - } +#ifdef DEBUG_FOLDER_ITERATOR + std::cerr << "End of directory." << std::endl; +#endif + + mType = TYPE_UNKNOWN ; + mFileSize = 0 ; + mFileModTime = 0; + validity = false ; } bool FolderIterator::readdir()