-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatCacheServerImpl.h
More file actions
266 lines (199 loc) · 4.86 KB
/
StatCacheServerImpl.h
File metadata and controls
266 lines (199 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// StatCacheServerImpl.h
#ifndef STAT_CACHE_SERVER_IMPL_H
#define STAT_CACHE_SERVER_IMPL_H
#include <hash_map>
#include <string>
#include <Entry.h>
#include <MessageQueue.h>
#include <Node.h>
#include <OS.h>
class Directory;
class Entry;
class Node;
class SymLink;
// NodeRefHash
struct NodeRefHash
{
size_t operator()(const node_ref &nodeRef) const;
};
// EntryRefHash
struct EntryRefHash
{
size_t operator()(const entry_ref &entryRef) const;
};
// Referencable
class Referencable {
public:
Referencable()
: fReferenceCount(1),
fReferenceBaseCount(0)
{
}
virtual ~Referencable()
{
}
void AddReference()
{
fReferenceCount++;
}
bool RemoveReference()
{
if (--fReferenceCount <= fReferenceBaseCount) {
Unreferenced();
return true;
}
return false;
}
int32 CountReferences() const
{
return fReferenceCount;
}
protected:
virtual void Unreferenced() {};
protected:
int32 fReferenceCount;
int32 fReferenceBaseCount;
bool fDeleteWhenUnreferenced;
};
// Entry
class Entry : public Referencable {
public:
Entry();
~Entry();
status_t SetTo(Directory *parent, const char *name);
Directory *GetParent() const;
const char *GetName() const;
void SetNode(Node *node);
Node *GetNode() const;
void SetPrevious(Entry *entry);
Entry *GetPrevious() const;
void SetNext(Entry *entry);
Entry *GetNext() const;
entry_ref GetEntryRef() const;
status_t GetPath(string& path);
protected:
virtual void Unreferenced();
private:
Directory *fParent;
string fName;
Node *fNode;
Entry *fPrevious;
Entry *fNext;
};
// Node
class Node : public Referencable {
public:
Node(const struct stat &st);
virtual ~Node();
virtual status_t SetTo(Entry *entry);
status_t GetPath(string& path);
const struct stat &GetStat() const;
status_t GetStat(struct stat *st);
status_t UpdateStat();
void MarkStatInvalid();
void SetEntry(Entry *entry);
Entry *GetEntry() const;
node_ref GetNodeRef() const;
protected:
virtual void Unreferenced();
protected:
Entry *fEntry;
struct stat fStat;
bool fStatValid;
};
// Directory
class Directory : public Node {
public:
Directory(const struct stat &st);
~Directory();
virtual status_t SetTo(Entry *entry);
status_t FindEntry(const char *name, Entry **entry);
Entry *GetFirstEntry() const;
Entry *GetNextEntry(Entry *entry) const;
status_t ReadAllEntries();
bool IsComplete() const;
void AddEntry(Entry *entry);
void RemoveEntry(Entry *entry);
private:
Entry *fFirstEntry;
Entry *fLastEntry;
bool fIsComplete;
};
// SymLink
class SymLink : public Node {
public:
SymLink(const struct stat &st);
~SymLink();
virtual status_t SetTo(Entry *entry);
const char *GetTarget() const;
private:
string fTarget;
};
// NodeMonitor
class NodeMonitor : public BLooper {
public:
NodeMonitor();
virtual ~NodeMonitor();
status_t Init();
virtual void MessageReceived(BMessage *message);
status_t StartWatching(Node *node);
status_t StopWatching(Node *node);
status_t GetNextMonitoringMessage(BMessage **message);
private:
int32 fCurrentNodeMonitorLimit;
BMessageQueue fMessageQueue;
sem_id fMessageCountSem;
};
// PathResolver
class PathResolver {
public:
PathResolver();
status_t FindEntry(const char *path, bool traverse, Entry **_entry);
status_t FindEntry(Entry *entry, const char *path, bool traverse,
Entry **_entry);
status_t FindNode(const char *path, bool traverse, Node **node);
status_t ResolveSymlink(Node *node, Node **_node);
status_t ResolveSymlink(Node *node, Entry **entry);
status_t ResolveSymlink(Entry *entry, Entry **_entry);
private:
int32 fSymLinkCounter;
};
// NodeManager
class NodeManager : public BLocker {
public:
NodeManager();
~NodeManager();
static NodeManager *GetDefault();
status_t Init();
Directory *GetRootDirectory() const;
Node *GetNode(const node_ref &nodeRef);
Entry *GetEntry(const entry_ref &entryRef);
status_t CreateEntry(const entry_ref &entryRef, const node_ref *nodeRef,
Entry **_entry);
status_t CreateDirectory(const node_ref &nodeRef, Directory **_dir);
void RemoveEntry(Entry *entry);
void MoveEntry(Entry *entry, const entry_ref &newRef,
const node_ref &nodeRef);
void EntryUnreferenced(Entry *entry);
void NodeUnreferenced(Node *node);
status_t StartWatching(Node *node);
status_t StopWatching(Node *node);
private:
static int32 _NodeMonitoringProcessorEntry(void *data);
int32 _NodeMonitoringProcessor();
status_t _CreateNode(Entry *entry, Node **_node);
void _EntryCreated(BMessage *message);
void _EntryRemoved(BMessage *message);
void _EntryMoved(BMessage *message);
void _StatChanged(BMessage *message);
private:
typedef hash_map<entry_ref, Entry*, EntryRefHash> EntryMap;
typedef hash_map<node_ref, Node*, NodeRefHash> NodeMap;
EntryMap fEntries;
NodeMap fNodes;
Directory *fRootDirectory;
NodeMonitor *fNodeMonitor;
thread_id fNodeMonitoringProcessor;
static NodeManager sManager;
};
#endif // STAT_CACHE_SERVER_IMPL_H