-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
256 lines (200 loc) · 6.19 KB
/
main.c
File metadata and controls
256 lines (200 loc) · 6.19 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
/* Prograam code originally taken from:
http://man7.org/linux/man-pages/man7/inotify.7.html */
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
#include "filemanager.h"
#include "mm.h"
#include "strings.h"
static RecoveryDatabase_t rd;
/* Read all available inotify events from the file descriptor 'fd'.
wd is the table of watch descriptors for the directories in argv.
argc is the length of wd and argv.
argv is the list of watched directories.
Entry 0 of wd and argv is unused. */
static void handle_events(int fd, int **wd)
{
/* Some systems cannot read integer variables if they are not
properly aligned. On other systems, incorrect alignment may
decrease performance. Hence, the buffer used for reading from
the inotify file descriptor should have the same alignment as
struct inotify_event. */
char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *event;
size_t i;
ssize_t len;
char *ptr;
char *path;
/* Loop while events can be read from inotify file descriptor. */
for (;;)
{
/* Read some events. */
len = read(fd, buf, sizeof buf);
if (len == -1 && errno != EAGAIN)
{
perror("read");
exit(EXIT_FAILURE);
}
/* If the nonblocking read() found no events to read, then
it returns -1 with errno set to EAGAIN. In that case,
we exit the loop. */
if (len <= 0)
break;
/* Loop over all events in the buffer */
for (ptr = buf; ptr < buf + len;
ptr += sizeof(struct inotify_event) + event->len)
{
event = (const struct inotify_event *)ptr;
/* Print event type */
for (i = 0; i < rd.dirs->count; ++i)
if ((*wd)[i] == event->wd)
break;
if (i >= rd.dirs->count)
{
inotify_rm_watch(fd, event->wd);
continue;
}
path = SCatM(3, (char *)rd.dirs->storage[i], "/", event->name);
if (event->mask & IN_OPEN)
{
//printf("IN_OPEN: ");
}
if (event->mask & IN_CLOSE_NOWRITE)
{
//printf("IN_CLOSE_NOWRITE: ");
}
if (event->mask & IN_CLOSE_WRITE)
{
//printf("IN_CLOSE_WRITE: ");
FMCheckFile(&rd, path);
}
if (event->mask & IN_MOVED_FROM)
{
//printf("IN_MOVED_FROM: ");
FMCheckFile(&rd, path);
}
if (event->mask & IN_MOVED_TO)
{
//printf("IN_MOVED_TO: ");
FMCheckFile(&rd, path);
}
if (event->mask & IN_DELETE)
{
//printf("IN_DELETE: ");
FMCheckFile(&rd, path);
}
if (event->mask & IN_CREATE)
{
//printf("IN_CREATE: ");
FMCheckFile(&rd, path);
}
if (event->mask & IN_DELETE_SELF)
{
//printf("IN_DELETE_SELF: ");
FMCheckFile(&rd, path);
}
Mfree(path);
/* Print the name of the watched directory */
//printf("%s/", (char *)rd.dirs->storage[i]);
/* Print the name of the file */
//if (event->len)
// printf("%s", event->name);
/* Print type of filesystem object */
//if (event->mask & IN_ISDIR)
// printf(" [directory]\n");
//else
// printf(" [file]\n");
}
}
}
int main(int argc, char *argv[])
{
//char buf;
int fd, poll_num;
int *wd;
nfds_t nfds;
struct pollfd fds[2];
if (argc != 3)
{
printf("Usage: %s [recovery source folder] [folder path to be protected]\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("Press ENTER key to terminate.\n");
/* Create the file descriptor for accessing the inotify API */
fd = inotify_init1(IN_NONBLOCK);
if (fd == -1)
{
perror("inotify_init1");
exit(EXIT_FAILURE);
}
/**/
memset(&rd, 0, sizeof(rd));
FMCreateDatabase(&rd, argv[1], argv[2]);
rd.wd = &wd;
rd.fd = fd;
/* Allocate memory for watch descriptors */
wd = calloc(rd.dirs->count, sizeof(int));
if (wd == NULL)
{
perror("calloc");
exit(EXIT_FAILURE);
}
/* Mark directories for events
- file was opened
- file was closed */
for (size_t i = 0; i < rd.dirs->count; i++)
{
wd[i] = inotify_add_watch(fd, (char *)(rd.dirs->storage[i]), IN_OPEN | IN_CLOSE | IN_MOVE | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
if (wd[i] == -1)
{
fprintf(stderr, "Cannot watch '%s'\n", (char *)(rd.dirs->storage[i]));
perror("inotify_add_watch");
exit(EXIT_FAILURE);
}
}
/* Prepare for polling */
nfds = 1;
/* Console input */
//fds[0].fd = STDIN_FILENO;
//fds[0].events = POLLIN;
/* Inotify input */
fds[0].fd = fd;
fds[0].events = POLLIN;
/* Wait for events and/or terminal input */
printf("Listening for events.\n");
while (1)
{
poll_num = poll(fds, nfds, -1);
if (poll_num == -1)
{
if (errno == EINTR)
continue;
perror("poll");
exit(EXIT_FAILURE);
}
if (poll_num > 0)
{
/*if (fds[0].revents & POLLIN)
{
// Console input is available. Empty stdin and quit
while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\n')
continue;
break;
}*/
if (fds[0].revents & POLLIN)
{
/* Inotify events are available */
handle_events(fd, &wd);
}
}
}
printf("Listening for events stopped.\n");
/* Close inotify file descriptor */
close(fd);
free(wd);
exit(EXIT_SUCCESS);
}