-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Brute force tree walking implementation for linux
- Loading branch information
1 parent
346c4ad
commit 59abd82
Showing
4 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#ifndef DIR_TREE_H | ||
#define DIR_TREE_H | ||
|
||
#include <string> | ||
#include <unordered_set> | ||
#include <ostream> | ||
#include <istream> | ||
#include "Event.hh" | ||
|
||
struct DirEntry { | ||
std::string path; | ||
time_t mtime; | ||
|
||
DirEntry(const char *p, const struct stat *info) { | ||
path.assign(p); | ||
mtime = info->st_mtime; | ||
} | ||
|
||
DirEntry(std::istream &stream) { | ||
size_t size; | ||
|
||
if (stream >> size) { | ||
path.resize(size); | ||
if (stream.read(&path[0], size)) { | ||
stream >> mtime; | ||
} | ||
} | ||
} | ||
|
||
bool operator==(const DirEntry &other) const { | ||
return path == other.path; | ||
} | ||
|
||
void write(std::ostream &stream) const { | ||
stream << path.size() << path << mtime << "\n"; | ||
} | ||
}; | ||
|
||
namespace std { | ||
template <> | ||
struct hash<DirEntry> { | ||
std::size_t operator()(const DirEntry& k) const { | ||
return hash<string>()(k.path); | ||
} | ||
}; | ||
} | ||
|
||
struct DirTree { | ||
std::unordered_set<DirEntry> entries; | ||
DirTree() {} | ||
|
||
DirTree(std::istream &stream) { | ||
size_t size; | ||
if (stream >> size) { | ||
for (size_t i = 0; i < size; i++) { | ||
entries.insert(DirEntry(stream)); | ||
} | ||
} | ||
} | ||
|
||
void write(std::ostream &stream) { | ||
stream << entries.size() << "\n"; | ||
for (auto it = entries.begin(); it != entries.end(); it++) { | ||
it->write(stream); | ||
} | ||
} | ||
|
||
EventList *getChanges(DirTree *snapshot) { | ||
EventList *events = new EventList(); | ||
for (auto it = entries.begin(); it != entries.end(); it++) { | ||
auto found = snapshot->entries.find(*it); | ||
if (found == snapshot->entries.end()) { | ||
events->push(it->path, "create"); | ||
} else if (found->mtime != it->mtime) { | ||
events->push(it->path, "update"); | ||
} | ||
} | ||
|
||
for (auto it = snapshot->entries.begin(); it != snapshot->entries.end(); it++) { | ||
size_t count = entries.count(*it); | ||
if (count == 0) { | ||
events->push(it->path, "delete"); | ||
} | ||
} | ||
|
||
return events; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ public: | |
arr->Set(i, mEvents[i]->toJS()); | ||
} | ||
|
||
return scope.Escape(arr); | ||
return scope.Escape(arr); | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <string> | ||
#include <sstream> | ||
#include <ftw.h> | ||
#include "../DirTree.hh" | ||
#include "../Event.hh" | ||
|
||
static DirTree *tree; | ||
int addEntry(const char *path, const struct stat *info, const int flags, struct FTW *ftw) { | ||
tree->entries.insert(DirEntry(path, info)); | ||
return 0; | ||
} | ||
|
||
DirTree *getDirTree(std::string *dir) { | ||
tree = new DirTree(); | ||
int result = nftw(dir->c_str(), &addEntry, 15, FTW_PHYS); | ||
if (result < 0) { | ||
printf("error\n"); | ||
} | ||
|
||
return tree; | ||
} | ||
|
||
std::string getCurrentTokenImpl(std::string *dir) { | ||
auto tree = getDirTree(dir); | ||
std::ostringstream os; | ||
tree->write(os); | ||
return os.str(); | ||
} | ||
|
||
EventList *getEventsSinceImpl(std::string *dir, std::string *token) { | ||
std::istringstream is(*token); | ||
auto snapshot = new DirTree(is); | ||
auto now = getDirTree(dir); | ||
|
||
return now->getChanges(snapshot); | ||
} |