9153c302ba4cd7415d8b145ba7a03aa58a663f9470aec9f521024c852e68c043319ec40333f875df1a3808b244222cd15559d9c42ca525fc47932c7d5ff9f1 2.2 KB

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