From 2c76ddae507a6e2b53396cf477563a82b3bf7bc3 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 13 Aug 2014 07:08:04 +0200 Subject: [PATCH] Removed special case handling for !(showDirectories || showFiles) The special case is not necessary for correctness. It would help performance in theory, but it seems very unlikely that any code would request a directory scan when it interested in neither the subdirs nor the files in that directory. --- src/filelister.cpp | 110 ++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/src/filelister.cpp b/src/filelister.cpp index d86f616..4a8e927 100644 --- a/src/filelister.cpp +++ b/src/filelister.cpp @@ -83,83 +83,81 @@ void FileLister::browse(const string& path, bool clean) directories.clear(); files.clear(); - if (showDirectories || showFiles) { - DIR *dirp; - string slashedPath = path; - if (path[path.length() - 1] != '/') - slashedPath.push_back('/'); + DIR *dirp; + string slashedPath = path; + if (path[path.length() - 1] != '/') + slashedPath.push_back('/'); - if ((dirp = opendir(slashedPath.c_str())) == NULL) { - ERROR("Unable to open directory: %s\n", slashedPath.c_str()); - return; + if ((dirp = opendir(slashedPath.c_str())) == NULL) { + ERROR("Unable to open directory: %s\n", slashedPath.c_str()); + return; + } + + string filepath, file; + struct stat st; + struct dirent *dptr; + + while ((dptr = readdir(dirp))) { + file = dptr->d_name; + + if (file[0] == '.' && file != "..") + continue; + + filepath = slashedPath + file; + int statRet = stat(filepath.c_str(), &st); + if (statRet == -1) { + ERROR("Stat failed on '%s' with error '%s'\n", filepath.c_str(), strerror(errno)); + continue; } + if (find(excludes.begin(), excludes.end(), file) != excludes.end()) + continue; - string filepath, file; - struct stat st; - struct dirent *dptr; - - while ((dptr = readdir(dirp))) { - file = dptr->d_name; - - if (file[0] == '.' && file != "..") + if (S_ISDIR(st.st_mode)) { + if (!showDirectories) continue; - filepath = slashedPath + file; - int statRet = stat(filepath.c_str(), &st); - if (statRet == -1) { - ERROR("Stat failed on '%s' with error '%s'\n", filepath.c_str(), strerror(errno)); + directorySet.insert(file); + } else { + if (!showFiles) + continue; + + if (filter.empty()) { + fileSet.insert(file); continue; } - if (find(excludes.begin(), excludes.end(), file) != excludes.end()) - continue; - if (S_ISDIR(st.st_mode)) { - if (!showDirectories) - continue; + for (vector::iterator it = filter.begin(); it != filter.end(); ++it) { + if (file.find('.') == string::npos) { + if (!it->empty()) + continue; - directorySet.insert(file); - } else { - if (!showFiles) - continue; - - if (filter.empty()) { fileSet.insert(file); - continue; + break; } - for (vector::iterator it = filter.begin(); it != filter.end(); ++it) { - if (file.find('.') == string::npos) { - if (!it->empty()) - continue; + if (it->length() < file.length()) { + if (file[file.length() - it->length() - 1] != '.') + continue; + string file_lowercase = + file.substr(file.length() - it->length()); + + /* XXX: This won't accept UTF-8 codes. + * Thanksfully file extensions shouldn't contain any. */ + transform(file_lowercase.begin(), file_lowercase.end(), + file_lowercase.begin(), ::tolower); + + if (file_lowercase.compare(0, it->length(), *it) == 0) { fileSet.insert(file); break; } - - if (it->length() < file.length()) { - if (file[file.length() - it->length() - 1] != '.') - continue; - - string file_lowercase = - file.substr(file.length() - it->length()); - - /* XXX: This won't accept UTF-8 codes. - * Thanksfully file extensions shouldn't contain any. */ - transform(file_lowercase.begin(), file_lowercase.end(), - file_lowercase.begin(), ::tolower); - - if (file_lowercase.compare(0, it->length(), *it) == 0) { - fileSet.insert(file); - break; - } - } } } } - - closedir(dirp); } + closedir(dirp); + moveNames(move(directorySet), directories); moveNames(move(fileSet), files); }