@@ -18,12 +18,20 @@ InotifyTree::InotifyTree(int inotifyInstance, std::string path):
18
18
watchName = path.substr (location + 1 );
19
19
}
20
20
21
+ struct stat file;
22
+ if (stat ((directory + " /" + watchName).c_str (), &file) < 0 ) {
23
+ mRoot = NULL ;
24
+ return ;
25
+ }
26
+
27
+ addInode (file.st_ino );
21
28
mRoot = new InotifyNode (
22
29
this ,
23
30
mInotifyInstance ,
24
31
NULL ,
25
32
directory,
26
- watchName
33
+ watchName,
34
+ file.st_ino
27
35
);
28
36
29
37
if (
@@ -114,6 +122,14 @@ void InotifyTree::setError(std::string error) {
114
122
mError = error;
115
123
}
116
124
125
+ bool InotifyTree::addInode (ino_t inodeNumber) {
126
+ return inodes.insert (inodeNumber).second ;
127
+ }
128
+
129
+ void InotifyTree::removeInode (ino_t inodeNumber) {
130
+ inodes.erase (inodeNumber);
131
+ }
132
+
117
133
InotifyTree::~InotifyTree () {
118
134
if (isRootAlive ()) {
119
135
delete mRoot ;
@@ -128,9 +144,11 @@ InotifyTree::InotifyNode::InotifyNode(
128
144
int inotifyInstance,
129
145
InotifyNode *parent,
130
146
std::string directory,
131
- std::string name
147
+ std::string name,
148
+ ino_t inodeNumber
132
149
):
133
150
mDirectory(directory),
151
+ mInodeNumber(inodeNumber),
134
152
mInotifyInstance(inotifyInstance),
135
153
mName(name),
136
154
mParent(parent),
@@ -170,7 +188,8 @@ InotifyTree::InotifyNode::InotifyNode(
170
188
171
189
if (
172
190
stat (filePath.c_str (), &file) < 0 ||
173
- !S_ISDIR (file.st_mode )
191
+ !S_ISDIR (file.st_mode ) ||
192
+ !mTree ->addInode (file.st_ino ) // Skip this inode if already watching
174
193
) {
175
194
continue ;
176
195
}
@@ -180,7 +199,8 @@ InotifyTree::InotifyNode::InotifyNode(
180
199
mInotifyInstance ,
181
200
this ,
182
201
mFullPath ,
183
- fileName
202
+ fileName,
203
+ file.st_ino
184
204
);
185
205
186
206
if (child->isAlive ()) {
@@ -198,6 +218,8 @@ InotifyTree::InotifyNode::InotifyNode(
198
218
}
199
219
200
220
InotifyTree::InotifyNode::~InotifyNode () {
221
+ mTree ->removeInode (mInodeNumber );
222
+
201
223
if (mWatchDescriptorInitialized ) {
202
224
inotify_rm_watch (mInotifyInstance , mWatchDescriptor );
203
225
mTree ->removeNodeReferenceByWD (mWatchDescriptor );
@@ -211,21 +233,26 @@ InotifyTree::InotifyNode::~InotifyNode() {
211
233
}
212
234
213
235
void InotifyTree::InotifyNode::addChild (std::string name) {
214
- InotifyNode *child = new InotifyNode (
215
- mTree ,
216
- mInotifyInstance ,
217
- this ,
218
- mFullPath ,
219
- name
220
- );
236
+ struct stat file;
221
237
222
- if (
223
- child->isAlive () &&
224
- child->inotifyInit ()
225
- ) {
226
- (*mChildren )[name] = child;
227
- } else {
228
- delete child;
238
+ if (stat (createFullPath (mFullPath , name).c_str (), &file) >= 0 && mTree ->addInode (file.st_ino )) {
239
+ InotifyNode *child = new InotifyNode (
240
+ mTree ,
241
+ mInotifyInstance ,
242
+ this ,
243
+ mFullPath ,
244
+ name,
245
+ file.st_ino
246
+ );
247
+
248
+ if (
249
+ child->isAlive () &&
250
+ child->inotifyInit ()
251
+ ) {
252
+ (*mChildren )[name] = child;
253
+ } else {
254
+ delete child;
255
+ }
229
256
}
230
257
}
231
258
0 commit comments