6a2934d5c0a1fc54f3edba4b37eda29baeda26a6214c1a77923f048e40a835c2ac8b3b59bb0ed609434eca460200a011387b2f6bc280e26bf7afdc6cc7ae4d 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. }