3147d494577e5db6b5442145b5b9e0d6ce4fb3a9741a797348bfa7d9abeea2ab7c1c06f0e872449eb11b6be8605dc106aaf9434eec03f2226b105950f19633 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef DIR_TREE_H
  2. #define DIR_TREE_H
  3. #include <string>
  4. #include <unordered_map>
  5. #include <memory>
  6. #include "Event.hh"
  7. #ifdef _WIN32
  8. #define DIR_SEP "\\"
  9. #else
  10. #define DIR_SEP "/"
  11. #endif
  12. struct DirEntry {
  13. std::string path;
  14. uint64_t mtime;
  15. bool isDir;
  16. mutable void *state;
  17. DirEntry(std::string p, uint64_t t, bool d);
  18. DirEntry(FILE *f);
  19. void write(FILE *f) const;
  20. bool operator==(const DirEntry &other) const {
  21. return path == other.path;
  22. }
  23. };
  24. class DirTree {
  25. public:
  26. static std::shared_ptr<DirTree> getCached(std::string root);
  27. DirTree(std::string root) : root(root), isComplete(false) {}
  28. DirTree(std::string root, FILE *f);
  29. DirEntry *add(std::string path, uint64_t mtime, bool isDir);
  30. DirEntry *find(std::string path);
  31. DirEntry *update(std::string path, uint64_t mtime);
  32. void remove(std::string path);
  33. void write(FILE *f);
  34. void getChanges(DirTree *snapshot, EventList &events);
  35. std::mutex mMutex;
  36. std::string root;
  37. bool isComplete;
  38. std::unordered_map<std::string, DirEntry> entries;
  39. private:
  40. DirEntry *_find(std::string path);
  41. };
  42. #endif