| 1 |
#include <string> |
| 2 |
|
| 3 |
// weird error on linux |
| 4 |
#ifdef __THROW |
| 5 |
#undef __THROW |
| 6 |
#endif |
| 7 |
#define __THROW |
| 8 |
|
| 9 |
#ifdef _LIBC |
| 10 |
# include <include/sys/stat.h> |
| 11 |
#else |
| 12 |
# include <sys/stat.h> |
| 13 |
#endif |
| 14 |
#include <dirent.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <fcntl.h> |
| 17 |
|
| 18 |
#include "../DirTree.hh" |
| 19 |
#include "../shared/BruteForceBackend.hh" |
| 20 |
|
| 21 |
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec) |
| 22 |
#if __APPLE__ |
| 23 |
#define st_mtim st_mtimespec |
| 24 |
#endif |
| 25 |
#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) |
| 26 |
|
| 27 |
void iterateDir(WatcherRef watcher, const std::shared_ptr <DirTree> tree, const char *relative, int parent_fd, const std::string &dirname) { |
| 28 |
int open_flags = (O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW); |
| 29 |
int new_fd = openat(parent_fd, relative, open_flags); |
| 30 |
if (new_fd == -1) { |
| 31 |
if (errno == EACCES) { |
| 32 |
return; // ignore insufficient permissions |
| 33 |
} |
| 34 |
|
| 35 |
throw WatcherError(strerror(errno), watcher); |
| 36 |
} |
| 37 |
|
| 38 |
struct stat rootAttributes; |
| 39 |
fstatat(new_fd, ".", &rootAttributes, AT_SYMLINK_NOFOLLOW); |
| 40 |
tree->add(dirname, CONVERT_TIME(rootAttributes.st_mtim), true); |
| 41 |
|
| 42 |
if (DIR *dir = fdopendir(new_fd)) { |
| 43 |
while (struct dirent *ent = (errno = 0, readdir(dir))) { |
| 44 |
if (ISDOT(ent->d_name)) continue; |
| 45 |
|
| 46 |
std::string fullPath = dirname + "/" + ent->d_name; |
| 47 |
|
| 48 |
if (!watcher->isIgnored(fullPath)) { |
| 49 |
struct stat attrib; |
| 50 |
fstatat(new_fd, ent->d_name, &attrib, AT_SYMLINK_NOFOLLOW); |
| 51 |
bool isDir = ent->d_type == DT_DIR; |
| 52 |
|
| 53 |
if (isDir) { |
| 54 |
iterateDir(watcher, tree, ent->d_name, new_fd, fullPath); |
| 55 |
} else { |
| 56 |
tree->add(fullPath, CONVERT_TIME(attrib.st_mtim), isDir); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
closedir(dir); |
| 62 |
} else { |
| 63 |
close(new_fd); |
| 64 |
} |
| 65 |
|
| 66 |
if (errno) { |
| 67 |
throw WatcherError(strerror(errno), watcher); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void BruteForceBackend::readTree(WatcherRef watcher, std::shared_ptr <DirTree> tree) { |
| 72 |
int fd = open(watcher->mDir.c_str(), O_RDONLY); |
| 73 |
if (fd) { |
| 74 |
iterateDir(watcher, tree, ".", fd, watcher->mDir); |
| 75 |
close(fd); |
| 76 |
} |
| 77 |
} |