-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInotify.cpp
More file actions
176 lines (149 loc) · 4 KB
/
Copy pathInotify.cpp
File metadata and controls
176 lines (149 loc) · 4 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
/**
*Copyright:SCUT
*Author:kjs
*Date:2020/5/22
*Description: Real-time monitoring for course design project
*File: Inotify.cpp
*/
#include "Inotify.h"
Inotify::Inotify(){
fd = -1;
}
Inotify::~Inotify(){
}
int Inotify::watch_init(int mask, char *root){
if(fd == -1){
fd = inotify_init();
if (fd == -1) {
cout<< "Error: inotify_init" << endl;
return fd;
}
}
add_watch(root, mask);
return fd;
}
void Inotify::add_watch(char *dir, int mask){
char subdir[PATH_MAX_SIZE];
DIR *odir;
struct dirent *dent;
if ((odir = opendir(dir)) == NULL){
cout<< "dir: " << dir << endl;
cerr << "Error: fail to open root dir" << endl;
return;
}
int dir_wd;
dir_wd = inotify_add_watch(fd, dir, mask);
dirset.insert(make_pair(dir_wd, string(dir)));
if (dir_wd == -1) {
cerr << "Error: inotify_add_watch";
close(fd);
}
errno = 0;
while ((dent = readdir(odir)) != NULL)
{
if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
continue;
if (dent->d_type == DT_DIR)
{
sprintf(subdir, "%s/%s", dir, dent->d_name);
add_watch(subdir, mask);
}
}
if (errno != 0)
{
cerr << "Error: fail to read dir"<<endl;
exit(1);
}
}
bool Inotify::delete_watch(string prefix){
map<int, string>::reverse_iterator iter;
for(iter = dirset.rbegin(); iter != dirset.rend(); iter++){
if(iter->second.find(prefix) == 0){
int result = inotify_rm_watch(fd, iter->first);
if (result == -1) {
cerr << "Error: fail to remove watch." << endl;
exit(1);
}
if(dirset.erase(iter->first) == 0){
cerr << "Error: erase filed." << endl;
exit(1);
}
return false;
}
}
return true;
}
void Inotify::delete_watch_recursive(char* dir){
string prefix = dir;
while (true)
{
if(delete_watch(prefix))
break;
}
}
// Call the Rsync execl
// fixed: need to use the cofig class
void Inotify::CallRsync()
{
pid_t pid = fork();
if (pid < 0)
{
cout << "Error: failed to fork!" << endl;
}
else if (pid == 0)
{
//run the executable file
if (execl("./hello", "./hello", "1111", NULL) < 0)
cout << "Error: execl error!" << endl;
}
else
{
int status;
if (wait(&status) == -1)
cout << "Error: failed to wait!" << endl;
}
}
void Inotify::watch_loop(){
int len = 0;
char buf[EVENT_BUFSIZE], *cur = buf, *end;
// Rsync at first.
CallRsync();
while (true) {
len = read(fd, cur, EVENT_BUFSIZE - len);
if (len <= 0) {
cerr << "Error: read inotify event";
close(fd);
}
end = cur + len;
while (cur + sizeof(struct inotify_event) <= end) {
struct inotify_event* e = (struct inotify_event*)cur;
if (cur + sizeof(struct inotify_event) + e->len > end)
break;
if (e->mask & IN_MOVED_FROM || e->mask & IN_DELETE) {
if (e->mask & IN_ISDIR){
char new_dir[512];
sprintf(new_dir, "%s/%s", dirset.find(e->wd)->second.c_str(), e->name);
delete_watch_recursive(new_dir);
}
}
if (e->mask & IN_CREATE || e->mask & IN_MOVED_TO) {
if (e->mask & IN_ISDIR){
char new_dir[512];
sprintf(new_dir, "%s/%s", dirset.find(e->wd)->second.c_str(), e->name);
add_watch(new_dir, MASK);
}
}
cur += sizeof(struct inotify_event) + e->len;
}
if (cur >= end) {
cur = buf;
len = 0;
} else {
len = end - cur;
memmove(buf, cur, len);
cur = buf + len;
}
CallRsync();
}
close(fd);
}