WasmBackend.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <sys/stat.h>
  2. #include "WasmBackend.hh"
  3. #define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
  4. void WasmBackend::start() {
  5. notifyStarted();
  6. }
  7. void WasmBackend::subscribe(WatcherRef watcher) {
  8. // Build a full directory tree recursively, and watch each directory.
  9. std::shared_ptr<DirTree> tree = getTree(watcher);
  10. for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
  11. if (it->second.isDir) {
  12. watchDir(watcher, it->second.path, tree);
  13. }
  14. }
  15. }
  16. void WasmBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
  17. int wd = wasm_backend_add_watch(path.c_str(), (void *)this);
  18. std::shared_ptr<WasmSubscription> sub = std::make_shared<WasmSubscription>();
  19. sub->tree = tree;
  20. sub->path = path;
  21. sub->watcher = watcher;
  22. mSubscriptions.emplace(wd, sub);
  23. }
  24. extern "C" void wasm_backend_event_handler(void *backend, int wd, int type, char *filename) {
  25. WasmBackend *b = (WasmBackend *)(backend);
  26. b->handleEvent(wd, type, filename);
  27. }
  28. void WasmBackend::handleEvent(int wd, int type, char *filename) {
  29. // Find the subscriptions for this watch descriptor
  30. auto range = mSubscriptions.equal_range(wd);
  31. std::unordered_set<std::shared_ptr<WasmSubscription>> set;
  32. for (auto it = range.first; it != range.second; it++) {
  33. set.insert(it->second);
  34. }
  35. for (auto it = set.begin(); it != set.end(); it++) {
  36. if (handleSubscription(type, filename, *it)) {
  37. (*it)->watcher->notify();
  38. }
  39. }
  40. }
  41. bool WasmBackend::handleSubscription(int type, char *filename, std::shared_ptr<WasmSubscription> sub) {
  42. // Build full path and check if its in our ignore list.
  43. WatcherRef watcher = sub->watcher;
  44. std::string path = std::string(sub->path);
  45. if (filename[0] != '\0') {
  46. path += "/" + std::string(filename);
  47. }
  48. if (watcher->isIgnored(path)) {
  49. return false;
  50. }
  51. if (type == 1) {
  52. struct stat st;
  53. stat(path.c_str(), &st);
  54. sub->tree->update(path, CONVERT_TIME(st.st_mtim));
  55. watcher->mEvents.update(path);
  56. } else if (type == 2) {
  57. // Determine if this is a create or delete depending on if the file exists or not.
  58. struct stat st;
  59. if (lstat(path.c_str(), &st)) {
  60. // If the entry being deleted/moved is a directory, remove it from the list of subscriptions
  61. DirEntry *entry = sub->tree->find(path);
  62. if (!entry) {
  63. return false;
  64. }
  65. if (entry->isDir) {
  66. std::string pathStart = path + DIR_SEP;
  67. for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
  68. if (it->second->path == path || it->second->path.rfind(pathStart, 0) == 0) {
  69. wasm_backend_remove_watch(it->first);
  70. it = mSubscriptions.erase(it);
  71. } else {
  72. ++it;
  73. }
  74. }
  75. // Remove all sub-entries
  76. for (auto it = sub->tree->entries.begin(); it != sub->tree->entries.end();) {
  77. if (it->first.rfind(pathStart, 0) == 0) {
  78. watcher->mEvents.remove(it->first);
  79. it = sub->tree->entries.erase(it);
  80. } else {
  81. it++;
  82. }
  83. }
  84. }
  85. watcher->mEvents.remove(path);
  86. sub->tree->remove(path);
  87. } else if (sub->tree->find(path)) {
  88. sub->tree->update(path, CONVERT_TIME(st.st_mtim));
  89. watcher->mEvents.update(path);
  90. } else {
  91. watcher->mEvents.create(path);
  92. // If this is a create, check if it's a directory and start watching if it is.
  93. DirEntry *entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
  94. if (entry->isDir) {
  95. watchDir(watcher, path, sub->tree);
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. void WasmBackend::unsubscribe(WatcherRef watcher) {
  102. // Find any subscriptions pointing to this watcher, and remove them.
  103. for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
  104. if (it->second->watcher.get() == watcher.get()) {
  105. if (mSubscriptions.count(it->first) == 1) {
  106. wasm_backend_remove_watch(it->first);
  107. }
  108. it = mSubscriptions.erase(it);
  109. } else {
  110. it++;
  111. }
  112. }
  113. }