Skip to content

Commit 044eaf3

Browse files
committed
FileLister: small cleanup
1 parent 70d62c4 commit 044eaf3

1 file changed

Lines changed: 68 additions & 62 deletions

File tree

cli/filelister.cpp

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,8 @@
4141
// When compiling Unicode targets WinAPI automatically uses *W Unicode versions
4242
// of called functions. Thus, we explicitly call *A versions of the functions.
4343

44-
std::string FileLister::recursiveAddFiles(std::list<FileWithDetails>&files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
44+
static std::string addFiles2(std::list<FileWithDetails>&files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
4545
{
46-
return addFiles(files, path, extra, true, ignored);
47-
}
48-
49-
std::string FileLister::addFiles(std::list<FileWithDetails>&files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
50-
{
51-
if (path.empty())
52-
return "no path specified";
53-
5446
const std::string cleanedPath = Path::toNativeSeparators(path);
5547

5648
// basedir is the base directory which is used to form pathnames.
@@ -110,22 +102,24 @@ std::string FileLister::addFiles(std::list<FileWithDetails>&files, const std::st
110102
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
111103
// File
112104
if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.match(fname)) {
113-
const std::string nativename = Path::fromNativeSeparators(fname);
105+
std::string nativename = Path::fromNativeSeparators(fname);
114106

115107
// Limitation: file sizes are assumed to fit in a 'size_t'
116108
#ifdef _WIN64
117-
files.emplace_back(nativename, (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow);
109+
const std::size_t filesize = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
118110
#else
119-
files.emplace_back(nativename, ffd.nFileSizeLow);
111+
const std::size_t filesize = ffd.nFileSizeLow;
112+
120113
#endif
114+
files.emplace_back(std::move(nativename), filesize);
121115
}
122116
} else {
123117
// Directory
124118
if (recursive) {
125119
if (!ignored.match(fname)) {
126120
std::list<FileWithDetails> filesSorted;
127121

128-
std::string err = FileLister::recursiveAddFiles(filesSorted, fname, extra, ignored);
122+
std::string err = addFiles2(filesSorted, fname, extra, recursive, ignored);
129123
if (!err.empty())
130124
return err;
131125

@@ -152,6 +146,14 @@ std::string FileLister::addFiles(std::list<FileWithDetails>&files, const std::st
152146
return "";
153147
}
154148

149+
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
150+
{
151+
if (path.empty())
152+
return "no path specified";
153+
154+
return addFiles2(files, path, extra, recursive, ignored);
155+
}
156+
155157
#else
156158

157159
///////////////////////////////////////////////////////////////////////////////
@@ -177,68 +179,67 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
177179
return "";
178180

179181
struct stat file_stat;
180-
if (stat(path.c_str(), &file_stat) != -1) {
181-
if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
182-
DIR * dir = opendir(path.c_str());
183-
if (!dir) {
184-
const int err = errno;
185-
return "could not open directory '" + path + "' (errno: " + std::to_string(err) + ")";
186-
}
187-
std::unique_ptr<DIR, decltype(&closedir)> dir_deleter(dir, closedir);
182+
if (stat(path.c_str(), &file_stat) == -1)
183+
return ""; // TODO: return error?
184+
if ((file_stat.st_mode & S_IFMT) != S_IFDIR)
185+
{
186+
files.emplace_back(path, file_stat.st_size);
187+
return "";
188+
}
189+
190+
// process directory entry
191+
192+
DIR * dir = opendir(path.c_str());
193+
if (!dir) {
194+
const int err = errno;
195+
return "could not open directory '" + path + "' (errno: " + std::to_string(err) + ")";
196+
}
197+
std::unique_ptr<DIR, decltype(&closedir)> dir_deleter(dir, closedir);
188198

189-
std::string new_path = path;
190-
new_path += '/';
199+
std::string new_path = path;
200+
new_path += '/';
191201

192-
std::list<FileWithDetails> filesSorted;
202+
std::list<FileWithDetails> filesSorted;
193203

194-
while (const dirent* dir_result = readdir(dir)) {
195-
if ((std::strcmp(dir_result->d_name, ".") == 0) ||
196-
(std::strcmp(dir_result->d_name, "..") == 0))
197-
continue;
204+
while (const dirent* dir_result = readdir(dir)) {
205+
if ((std::strcmp(dir_result->d_name, ".") == 0) ||
206+
(std::strcmp(dir_result->d_name, "..") == 0))
207+
continue;
198208

199-
new_path.erase(path.length() + 1);
200-
new_path += dir_result->d_name;
209+
new_path.erase(path.length() + 1);
210+
new_path += dir_result->d_name;
201211

202212
#if defined(_DIRENT_HAVE_D_TYPE) || defined(_BSD_SOURCE)
203-
const bool path_is_directory = (dir_result->d_type == DT_DIR || (dir_result->d_type == DT_UNKNOWN && Path::isDirectory(new_path)));
213+
const bool path_is_directory = (dir_result->d_type == DT_DIR || (dir_result->d_type == DT_UNKNOWN && Path::isDirectory(new_path)));
204214
#else
205-
const bool path_is_directory = Path::isDirectory(new_path);
215+
const bool path_is_directory = Path::isDirectory(new_path);
206216
#endif
207-
if (path_is_directory) {
208-
if (recursive && !ignored.match(new_path)) {
209-
std::string err = addFiles2(filesSorted, new_path, extra, recursive, ignored);
210-
if (!err.empty()) {
211-
return err;
212-
}
213-
}
214-
} else {
215-
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
216-
if (stat(new_path.c_str(), &file_stat) != -1) {
217-
filesSorted.emplace_back(new_path, file_stat.st_size);
218-
}
219-
else {
220-
const int err = errno;
221-
return "could not stat file '" + new_path + "' (errno: " + std::to_string(err) + ")";
222-
}
223-
}
217+
if (path_is_directory) {
218+
if (recursive && !ignored.match(new_path)) {
219+
std::string err = addFiles2(filesSorted, new_path, extra, recursive, ignored);
220+
if (!err.empty()) {
221+
return err;
222+
}
223+
}
224+
} else {
225+
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
226+
if (stat(new_path.c_str(), &file_stat) == -1) {
227+
const int err = errno;
228+
return "could not stat file '" + new_path + "' (errno: " + std::to_string(err) + ")";
224229
}
230+
filesSorted.emplace_back(new_path, file_stat.st_size);
225231
}
232+
}
233+
}
226234

227-
// files inside directories need to be sorted as the filesystem doesn't provide a stable order
228-
filesSorted.sort([](const FileWithDetails& a, const FileWithDetails& b) {
229-
return a.path() < b.path();
230-
});
235+
// files inside directories need to be sorted as the filesystem doesn't provide a stable order
236+
filesSorted.sort([](const FileWithDetails& a, const FileWithDetails& b) {
237+
return a.path() < b.path();
238+
});
231239

232-
files.insert(files.end(), std::make_move_iterator(filesSorted.begin()), std::make_move_iterator(filesSorted.end()));
233-
} else
234-
files.emplace_back(path, file_stat.st_size);
235-
}
236-
return "";
237-
}
240+
files.insert(files.end(), std::make_move_iterator(filesSorted.begin()), std::make_move_iterator(filesSorted.end()));
238241

239-
std::string FileLister::recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
240-
{
241-
return addFiles(files, path, extra, true, ignored);
242+
return "";
242243
}
243244

244245
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
@@ -254,3 +255,8 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
254255
}
255256

256257
#endif
258+
259+
std::string FileLister::recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
260+
{
261+
return addFiles(files, path, extra, true, ignored);
262+
}

0 commit comments

Comments
 (0)