fts.cc 1.2 KB

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