Debian Codegraph @

node/@parcel/watcher/src/DirTree.cc

raw
1 #include "DirTree.hh"
2 #include <inttypes.h>
3
4 // "Meyer's singleton", construction is ordered by use, likewise (reverse) for destruction.
5 // https://stackoverflow.com/a/17713799
6 // https://laristra.github.io/flecsi/src/developer-guide/patterns/meyers_singleton.html
7 static std::mutex& mDirCacheMutex() {
8 static std::mutex mutex;
9 return mutex;
10 }
11
12 static std::unordered_map<std::string, std::weak_ptr<DirTree>>& dirTreeCache() {
13 static std::unordered_map<std::string, std::weak_ptr<DirTree>> cache;
14 return cache;
15 }
16
17 struct DirTreeDeleter {
18 void operator()(DirTree *tree) {
19 std::lock_guard<std::mutex> lock(mDirCacheMutex());
20 std::unordered_map<std::string, std::weak_ptr<DirTree>> &cache = dirTreeCache();
21 cache.erase(tree->root);
22 delete tree;
23
24 // Free up memory.
25 if (cache.size() == 0) {
26 cache.rehash(0);
27 }
28 }
29 };
30
31 std::shared_ptr<DirTree> DirTree::getCached(std::string root) {
32 std::lock_guard<std::mutex> lock(mDirCacheMutex());
33 std::unordered_map<std::string, std::weak_ptr<DirTree>> &cache = dirTreeCache();
34
35 auto found = cache.find(root);
36 std::shared_ptr<DirTree> tree;
37
38 // Use cached tree, or create an empty one.
39 if (found != cache.end()) {
40 tree = found->second.lock();
41 } else {
42 tree = std::shared_ptr<DirTree>(new DirTree(root), DirTreeDeleter());
43 cache.emplace(root, tree);
44 }
45
46 return tree;
47 }
48
49 DirTree::DirTree(std::string root, FILE *f) : root(root), isComplete(true) {
50 size_t size;
51 if (fscanf(f, "%zu", &size)) {
52 for (size_t i = 0; i < size; i++) {
53 DirEntry entry(f);
54 entries.emplace(entry.path, entry);
55 }
56 }
57 }
58
59 // Internal find method that has no lock
60 DirEntry *DirTree::_find(std::string path) {
61 auto found = entries.find(path);
62 if (found == entries.end()) {
63 return NULL;
64 }
65
66 return &found->second;
67 }
68
69 DirEntry *DirTree::add(std::string path, uint64_t mtime, bool isDir) {
70 std::lock_guard<std::mutex> lock(mDirCacheMutex());
71
72 DirEntry entry(path, mtime, isDir);
73 auto it = entries.emplace(entry.path, entry);
74 return &it.first->second;
75 }
76
77 DirEntry *DirTree::find(std::string path) {
78 std::lock_guard<std::mutex> lock(mDirCacheMutex());
79 return _find(path);
80 }
81
82 DirEntry *DirTree::update(std::string path, uint64_t mtime) {
83 std::lock_guard<std::mutex> lock(mDirCacheMutex());
84
85 DirEntry *found = _find(path);
86 if (found) {
87 found->mtime = mtime;
88 }
89
90 return found;
91 }
92
93 void DirTree::remove(std::string path) {
94 std::lock_guard<std::mutex> lock(mDirCacheMutex());
95
96 DirEntry *found = _find(path);
97
98 // Remove all sub-entries if this is a directory
99 if (found && found->isDir) {
100 std::string pathStart = path + DIR_SEP;
101 for (auto it = entries.begin(); it != entries.end();) {
102 if (it->first.rfind(pathStart, 0) == 0) {
103 it = entries.erase(it);
104 } else {
105 it++;
106 }
107 }
108 }
109
110 entries.erase(path);
111 }
112
113 void DirTree::write(FILE *f) {
114 std::lock_guard<std::mutex> lock(mDirCacheMutex());
115
116 fprintf(f, "%zu\n", entries.size());
117 for (auto it = entries.begin(); it != entries.end(); it++) {
118 it->second.write(f);
119 }
120 }
121
122 void DirTree::getChanges(DirTree *snapshot, EventList &events) {
123 std::lock_guard<std::mutex> lock(mDirCacheMutex());
124 std::lock_guard<std::mutex> snapshotLock(snapshot->mMutex);
125
126 for (auto it = entries.begin(); it != entries.end(); it++) {
127 auto found = snapshot->entries.find(it->first);
128 if (found == snapshot->entries.end()) {
129 events.create(it->second.path);
130 } else if (found->second.mtime != it->second.mtime && !found->second.isDir && !it->second.isDir) {
131 events.update(it->second.path);
132 }
133 }
134
135 for (auto it = snapshot->entries.begin(); it != snapshot->entries.end(); it++) {
136 size_t count = entries.count(it->first);
137 if (count == 0) {
138 events.remove(it->second.path);
139 }
140 }
141 }
142
143 DirEntry::DirEntry(std::string p, uint64_t t, bool d) {
144 path = p;
145 mtime = t;
146 isDir = d;
147 state = NULL;
148 }
149
150 DirEntry::DirEntry(FILE *f) {
151 size_t size;
152 if (fscanf(f, "%zu", &size)) {
153 path.resize(size);
154 if (fread(&path[0], sizeof(char), size, f)) {
155 int d = 0;
156 fscanf(f, "%" PRIu64 " %d\n", &mtime, &d);
157 isDir = d == 1;
158 }
159 }
160 }
161
162 void DirEntry::write(FILE *f) const {
163 fprintf(f, "%zu%s%" PRIu64 " %d\n", path.size(), path.c_str(), mtime, isDir);
164 }