forked from sprewellkobe/dmirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatcher.cpp
More file actions
322 lines (309 loc) · 9.5 KB
/
Watcher.cpp
File metadata and controls
322 lines (309 loc) · 9.5 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <dirent.h>
#include "Watcher.hpp"
//-------------------------------------------------------------------------------------------------
void pipe_read_handler(aeEventLoop* el,int fd,void* arg,int mask)
{
Watcher* watcher=(Watcher*)arg;
char c;
int rv=read(fd,&c,1);
if(rv!=1)
return;
watcher->OnPipeRead();
}
//-------------------------------------------------------------------------------------------------
void Watcher::OnPipeRead()
{
if(watch_dir_list.empty()==true)
return;
const string& path=watch_dir_list.front();
if(path.empty()==false&&path[path.size()-1]=='/')
OnScanDir(path);
else
OnScanDir(path+"/");
watch_dir_count++;
struct dirent** list=NULL;
int ne=scandir(path.c_str(),&list,NULL,NULL);
if(ne<0)
return;
for(int i=0;i<ne;i++)
{
if(strcmp(list[i]->d_name,".")==0||
strcmp(list[i]->d_name,"..")==0)
{
free(list[i]);
list[i]=NULL;
continue;
}
if(list[i]->d_type==DT_DIR)
{
static char buf[4096];
if(path[path.size()-1]!='/')
snprintf(buf,4095,"%s/%s",path.c_str(),list[i]->d_name);
else
snprintf(buf,4095,"%s%s",path.c_str(),list[i]->d_name);
watch_dir_list.push_back(buf);
//write(pipe_pair[1],"x",1);
}
free(list[i]);
list[i]=NULL;
}//end for i
free(list);
watch_dir_list.pop_front();
if(watch_dir_list.empty())
{
kobe_printf("%s\tprepare finished(%d), dir_count:%d, time_cost: %.3f sec\n",
GetCurrentTime().c_str(),int(addwatch),watch_dir_count,EndTiming(watch_init_tv));
aeDeleteFileEvent(main_el,pipe_pair[0],AE_READABLE);
close(pipe_pair[0]);
close(pipe_pair[1]);
pipe_pair[0]=0;
pipe_pair[1]=0;
}
else
write(pipe_pair[1],"x",1);
}
//-------------------------------------------------------------------------------------------------
bool Watcher::Prepare(int& err)
{
if(inotify_fd>0)
{
close(inotify_fd);
inotify_fd=0;
}
wd2path.clear();
path2wd.clear();
if(inotify_fd<=0)
{
inotify_fd=inotify_init();
if(inotify_fd==-1)
{
kobe_printf("%s\tERROR: failed to init inotify %d\n",GetCurrentTime().c_str(),errno);
return false;
}
}
addwatch=true;
watch_init_tv=BeginTiming();
watch_dir_count=0;
watch_dir_list.clear();
if(pipe(pipe_pair)!=0)
{
err=errno;
return false;
}
int rv=aeCreateFileEvent(main_el,pipe_pair[0],AE_READABLE,pipe_read_handler,this);
if(rv<0)
{
kobe_printf("%s\tERROR: failed to create pipe file event [%d]\n",
GetCurrentTime().c_str(),rv);
return false;
}
watch_dir_list.push_back(conf.local_dir);
write(pipe_pair[1],"x",1);
kobe_printf("%s\twatcher preparing...\n",GetCurrentTime().c_str());
/*
if(VisitPath(conf.local_dir,dir_count,this)==false)
return false;
if(addwatch==false)
return false;
kobe_printf("%s\tprepare finished, dir_count:%d, time_cost: %.3f sec\n",
GetCurrentTime().c_str(),dir_count,EndTiming(tv));
*/
return true;
}
//-------------------------------------------------------------------------------------------------
void Watcher::OnScanDir(const string& dirname)
{
int ret=inotify_add_watch(inotify_fd,dirname.c_str(),INOTIFY_EVENTS);
if(ret==-1)
{
kobe_printf("%s\tERROR: inotify_add_watch failed %s,%d,%d\n",
GetCurrentTime().c_str(),dirname.c_str(),inotify_fd,errno);
addwatch=false;
}
/*#ifdef MYDEBUG
kobe_printf("%s\tadd new watch %s,%d,%d\n",GetCurrentTime().c_str(),
dirname.c_str(),inotify_fd,ret);
#endif*/
watcherstatus.watch_wd_number++;
wd2path[ret]=dirname.substr(conf.local_dir.size());//to save memory
//path2wd[dirname]=ret;
}
//-------------------------------------------------------------------------------------------------
bool Watcher::Start(aeEventLoop* main_el)
{
if(main_el==NULL)
return false;
if(inotify_fd<=0)
{
inotify_fd=inotify_init();
if(inotify_fd==-1)
{
kobe_printf("%s\tERROR: failed to init inotify %d\n",GetCurrentTime().c_str(),errno);
return false;
}
}
aeCreateFileEvent(main_el,inotify_fd,AE_READABLE,on_inotify_read,this);
return true;
}
//-------------------------------------------------------------------------------------------------
bool Watcher::Stop(aeEventLoop* main_el)
{
if(main_el==NULL||inotify_fd<=0)
return false;
aeDeleteFileEvent(main_el,inotify_fd,AE_READABLE);
close(inotify_fd);
inotify_fd=0;
wd2path.clear();
path2wd.clear();
return true;
}
//-------------------------------------------------------------------------------------------------
void Watcher::OnInotifyRead(int fd)
{
static char buffer[INOTIFY_READ_BUFFER_SIZE];
ssize_t rl=read(fd,buffer,INOTIFY_READ_BUFFER_SIZE);
if(rl<=0)
return;
ssize_t buffer_index=0;
while(buffer_index<rl)
{
struct inotify_event* ievent=NULL;
ievent=(inotify_event*)&(buffer[buffer_index]);
DoEvent(ievent);
buffer_index+=sizeof(struct inotify_event)+ievent->len;
}//end while
}
//-------------------------------------------------------------------------------------------------
bool Watcher::AddDir(const string& fullname)
{
int dir_count=0;
if(VisitPath(fullname,dir_count,this)==false)
return false;
return true;
}
//-------------------------------------------------------------------------------------------------
bool Watcher::RemoveDir(const string& fullname)
{
//delete automatically
/*map<string,int>::iterator mi2=path2wd.find(fullname);
if(mi2==path2wd.end())
{
kobe_printf("%s\tERROR: RemoveDir failed cannot find %s\n",
GetCurrentTime().c_str(),fullname.c_str());
return false;
}
int ret=inotify_rm_watch(inotify_fd,mi2->second);
if(ret==-1)
{
kobe_printf("%s\tERROR: inotify_rm_watch %s failed %d,%d,%d\n",
GetCurrentTime().c_str(),fullname.c_str(),errno,inotify_fd,mi2->second);
return false;
}
#ifdef MYDEBUG
kobe_printf("%s\trm existed watch %s\n",GetCurrentTime().c_str(),fullname.c_str());
#endif
map<int,string>::iterator mi=wd2path.find(mi2->second);
if(mi!=wd2path.end())
wd2path.erase(mi);
path2wd.erase(mi2);*/
watcherstatus.watch_wd_number--;
return true;
}
//-------------------------------------------------------------------------------------------------
bool Watcher::DoEvent(struct inotify_event* ievent)
{
bool isdir=false;
if(ievent->len<=0)
return false;
string fullname=ievent->name;
int wd=ievent->wd;
if(ievent->mask&IN_ISDIR)
{
isdir=true;
fullname+="/";
}
if(ievent->mask&IN_Q_OVERFLOW)
rlog->safe_mode=SYNC_DANGER_INOTIFY_OVERFLOW;
map<int,string>::iterator mi=wd2path.find(wd);
if(mi==wd2path.end())
{
kobe_printf("%s\tERROR: Doevent cannot find %d\n",GetCurrentTime().c_str(),wd);
return false;
}
fullname=conf.local_dir+mi->second+fullname;
int err=0;
switch(ievent->mask&INOTIFY_EVENTS)
{
case IN_CREATE:
#ifdef MYDEBUG
kobe_printf("%s\tcreate %s,%d\n",
GetCurrentTime().c_str(),fullname.c_str(),ievent->wd);
#endif
if(isdir&&AddDir(fullname)==false)
return false;
rlog->Write(IN_CREATE,fullname,err);
break;
case IN_DELETE:
#ifdef MYDEBUG
kobe_printf("%s\tdelete %s\n",GetCurrentTime().c_str(),fullname.c_str());
#endif
if(isdir&&RemoveDir(fullname)==false)
return false;
rlog->Write(IN_DELETE,fullname,err);
break;
case IN_DELETE_SELF:
#ifdef MYDEBUG
kobe_printf("%s\tdelete_self %s\n",GetCurrentTime().c_str(),fullname.c_str());
#endif
rlog->Write(IN_DELETE_SELF,fullname,err);
break;
case IN_CLOSE_WRITE:
//case IN_MODIFY:
#ifdef MYDEBUG
kobe_printf("%s\tmodified %s,%d\n",
GetCurrentTime().c_str(),fullname.c_str(),ievent->wd);
#endif
rlog->Write(IN_CLOSE_WRITE,fullname,err);
break;
case IN_MOVE_SELF:
#ifdef MYDEBUG
kobe_printf("%s\tmove_self %s\n",GetCurrentTime().c_str(),fullname.c_str());
#endif
rlog->Write(IN_MOVE_SELF,fullname,err);
break;
case IN_MOVED_FROM:
#ifdef MYDEBUG
kobe_printf("%s\tmoved_from %s\n",GetCurrentTime().c_str(),fullname.c_str());
#endif
if(isdir&&RemoveDir(fullname)==false)
return false;
rlog->Write(IN_MOVED_FROM,fullname,err);
break;
case IN_MOVED_TO:
if(isdir&&AddDir(fullname)==false)
{
#ifdef MYDEBUG
kobe_printf("%s\tERROR: failed to add %s %d\n",GetCurrentTime().c_str(),
fullname.c_str(),errno);
#endif
return false;
}
rlog->Write(IN_MOVED_TO,fullname,err);
break;
default:
#ifdef MYDEBUG
kobe_printf("%s\tunknown event_type\n",GetCurrentTime().c_str());
#endif
break;
}//end switch
return true;
}
//-------------------------------------------------------------------------------------------------
void on_inotify_read(aeEventLoop* main_el,int fd,void* arg,int mask)
{
if(arg==NULL)
return;
Watcher* watcher=(Watcher*)arg;
watcher->OnInotifyRead(fd);
}
//-------------------------------------------------------------------------------------------------