Skip to content

Commit

Permalink
undo using openat, it's not that much faster and adds complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper committed Dec 16, 2019
1 parent db1cb20 commit 5fb1ac8
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions src/unix/legacy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
#endif
#define __THROW

#ifdef _LIBC
# include <include/sys/stat.h>
#else
# include <sys/stat.h>
#endif
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

#include "../DirTree.hh"
#include "../shared/BruteForceBackend.hh"
Expand All @@ -23,42 +18,34 @@
#endif
#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))

void iterateDir(Watcher &watcher, const std::shared_ptr <DirTree> tree, const char *relative, int parent_fd, std::string dirname) {
int open_flags = (O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
int new_fd = openat(parent_fd, relative, open_flags);

if (DIR *dir = fdopendir(new_fd)) {
void iterateDir(Watcher &watcher, const std::shared_ptr <DirTree> tree, const char *dirname) {
if (DIR *dir = opendir(dirname)) {
while (struct dirent *ent = (errno = 0, readdir(dir))) {
if (ISDOT(ent->d_name)) continue;

std::string fullPath = dirname + "/" + ent->d_name;
std::string fullPath = dirname + std::string("/") + ent->d_name;

if (watcher.mIgnore.count(fullPath) == 0) {
struct stat attrib;
fstatat(new_fd, ent->d_name, &attrib, AT_SYMLINK_NOFOLLOW);
stat(fullPath.c_str(), &attrib);
bool isDir = ent->d_type == DT_DIR;

tree->add(fullPath, CONVERT_TIME(attrib.st_mtim), isDir);

if (isDir) {
iterateDir(watcher, tree, ent->d_name, new_fd, fullPath);
iterateDir(watcher, tree, fullPath.c_str());
}
}
}
}

close(new_fd);
closedir(dir);
}

if (errno) {
throw WatcherError(strerror(errno), &watcher);
}
}

void BruteForceBackend::readTree(Watcher &watcher, std::shared_ptr <DirTree> tree) {
int fd = open(watcher.mDir.c_str(), O_RDONLY);
if (fd) {
iterateDir(watcher, tree, ".", fd, watcher.mDir);
}

close(fd);
}
return iterateDir(watcher, tree, watcher.mDir.c_str());
}

0 comments on commit 5fb1ac8

Please sign in to comment.