2018-04-08 00:10:54 +00:00
|
|
|
#include "filesystem.hh"
|
2018-05-13 20:30:24 +00:00
|
|
|
using namespace llvm;
|
2018-04-08 00:10:54 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
2018-05-20 06:40:07 +00:00
|
|
|
#include <set>
|
2018-05-17 06:49:21 +00:00
|
|
|
#include <vector>
|
2018-04-08 00:10:54 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void GetFilesInFolder(std::string folder, bool recursive, bool dir_prefix,
|
|
|
|
const std::function<void(const std::string &)> &handler) {
|
2018-05-14 02:09:26 +00:00
|
|
|
EnsureEndsInSlash(folder);
|
2018-05-20 06:40:07 +00:00
|
|
|
sys::fs::file_status Status;
|
|
|
|
if (sys::fs::status(folder, Status, true))
|
|
|
|
return;
|
|
|
|
sys::fs::UniqueID ID;
|
|
|
|
std::vector<std::string> curr{folder};
|
|
|
|
std::vector<std::pair<std::string, sys::fs::file_status>> succ;
|
|
|
|
std::set<sys::fs::UniqueID> seen{Status.getUniqueID()};
|
|
|
|
while (curr.size() || succ.size()) {
|
|
|
|
if (curr.empty()) {
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &it : succ)
|
2018-05-20 06:40:07 +00:00
|
|
|
if (!seen.count(it.second.getUniqueID()))
|
|
|
|
curr.push_back(std::move(it.first));
|
|
|
|
succ.clear();
|
|
|
|
} else {
|
|
|
|
std::error_code ec;
|
|
|
|
std::string folder1 = curr.back();
|
|
|
|
curr.pop_back();
|
|
|
|
for (sys::fs::directory_iterator I(folder1, ec, false), E; I != E && !ec;
|
|
|
|
I.increment(ec)) {
|
|
|
|
std::string path = I->path(), filename = sys::path::filename(path);
|
|
|
|
if ((filename[0] == '.' && filename != ".ccls") ||
|
|
|
|
sys::fs::status(path, Status, false))
|
|
|
|
continue;
|
|
|
|
if (sys::fs::is_symlink_file(Status)) {
|
|
|
|
if (sys::fs::status(path, Status, true))
|
|
|
|
continue;
|
|
|
|
if (sys::fs::is_directory(Status)) {
|
|
|
|
if (recursive)
|
|
|
|
succ.emplace_back(path, Status);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sys::fs::is_regular_file(Status)) {
|
2018-05-17 06:49:21 +00:00
|
|
|
if (!dir_prefix)
|
|
|
|
path = path.substr(folder.size());
|
|
|
|
handler(path);
|
2018-05-20 06:40:07 +00:00
|
|
|
} else if (recursive && sys::fs::is_directory(Status) &&
|
|
|
|
!seen.count(ID = Status.getUniqueID())) {
|
|
|
|
curr.push_back(path);
|
|
|
|
seen.insert(ID);
|
|
|
|
}
|
2018-04-08 00:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-17 06:49:21 +00:00
|
|
|
}
|
2018-04-08 00:10:54 +00:00
|
|
|
}
|