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