| 1 |
#include <string> |
| 2 |
|
| 3 |
// weird error on linux |
| 4 |
#ifdef __THROW |
| 5 |
#undef __THROW |
| 6 |
#endif |
| 7 |
#define __THROW |
| 8 |
|
| 9 |
#include <fts.h> |
| 10 |
#include <sys/stat.h> |
| 11 |
#include "../DirTree.hh" |
| 12 |
#include "../shared/BruteForceBackend.hh" |
| 13 |
|
| 14 |
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec) |
| 15 |
#if __APPLE__ |
| 16 |
#define st_mtim st_mtimespec |
| 17 |
#endif |
| 18 |
|
| 19 |
void BruteForceBackend::readTree(WatcherRef watcher, std::shared_ptr<DirTree> tree) { |
| 20 |
char *paths[2] {(char *)watcher->mDir.c_str(), NULL}; |
| 21 |
FTS *fts = fts_open(paths, FTS_NOCHDIR | FTS_PHYSICAL, NULL); |
| 22 |
if (!fts) { |
| 23 |
throw WatcherError(strerror(errno), watcher); |
| 24 |
} |
| 25 |
|
| 26 |
FTSENT *node; |
| 27 |
bool isRoot = true; |
| 28 |
|
| 29 |
while ((node = fts_read(fts)) != NULL) { |
| 30 |
if (node->fts_errno) { |
| 31 |
fts_close(fts); |
| 32 |
throw WatcherError(strerror(node->fts_errno), watcher); |
| 33 |
} |
| 34 |
|
| 35 |
if (isRoot && !(node->fts_info & FTS_D)) { |
| 36 |
fts_close(fts); |
| 37 |
throw WatcherError(strerror(ENOTDIR), watcher); |
| 38 |
} |
| 39 |
|
| 40 |
if (watcher->isIgnored(std::string(node->fts_path))) { |
| 41 |
fts_set(fts, node, FTS_SKIP); |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
tree->add(node->fts_path, CONVERT_TIME(node->fts_statp->st_mtim), (node->fts_info & FTS_D) == FTS_D); |
| 46 |
isRoot = false; |
| 47 |
} |
| 48 |
|
| 49 |
fts_close(fts); |
| 50 |
} |