0c2e7edc594f2486f064a6e25ec61206a91d614cd897a4f54f93d0552ae1093bd52f130ffcbcdf07d8000feafbe35f500915d0bb5acf31fccc990cf5cee953 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <string>
  2. #include "../DirTree.hh"
  3. #include "../Event.hh"
  4. #include "./BruteForceBackend.hh"
  5. std::shared_ptr<DirTree> BruteForceBackend::getTree(WatcherRef watcher, bool shouldRead) {
  6. auto tree = DirTree::getCached(watcher->mDir);
  7. // If the tree is not complete, read it if needed.
  8. if (!tree->isComplete && shouldRead) {
  9. readTree(watcher, tree);
  10. tree->isComplete = true;
  11. }
  12. return tree;
  13. }
  14. void BruteForceBackend::writeSnapshot(WatcherRef watcher, std::string *snapshotPath) {
  15. std::unique_lock<std::mutex> lock(mMutex);
  16. auto tree = getTree(watcher);
  17. FILE *f = fopen(snapshotPath->c_str(), "w");
  18. if (!f) {
  19. throw std::runtime_error(std::string("Unable to open snapshot file: ") + strerror(errno));
  20. }
  21. tree->write(f);
  22. fclose(f);
  23. }
  24. void BruteForceBackend::getEventsSince(WatcherRef watcher, std::string *snapshotPath) {
  25. std::unique_lock<std::mutex> lock(mMutex);
  26. FILE *f = fopen(snapshotPath->c_str(), "r");
  27. if (!f) {
  28. throw std::runtime_error(std::string("Unable to open snapshot file: ") + strerror(errno));
  29. }
  30. DirTree snapshot{watcher->mDir, f};
  31. auto now = getTree(watcher);
  32. now->getChanges(&snapshot, watcher->mEvents);
  33. fclose(f);
  34. }