-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadaptive.c
181 lines (150 loc) · 5.65 KB
/
adaptive.c
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
/************************************************************************
* fspy - experimental POC linux filesystem activity monitor *
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *
* it's based on the new linux kernel inotify interface (merged into *
* the 2.6.13 linux kernel) *
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *
* this code was tested on 2.6.18.4 and newer kernels *
************************************************************************
* Copyright (C) 2007 Richard Sammet (e-axe) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* http://www.gnu.org/licenses/gpl.txt *
************************************************************************
* Contact, Bugs & Infos: *
************************************************************************
* Some infos: *
* - tabstop size: *
* set ts=2 *
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h> /* definition of UINT_MAX */
#include <sys/inotify.h>
#include <sys/stat.h> /* struct stat */
#include <errno.h>
#include "fspy.h"
#include "fsevents.h"
#include "adaptive.h"
#include "enumdirs.h" /* needed for max_element_count */
#include "stating.h"
extern unsigned int max_element_count; /* from enumdirs.c */
extern unsigned int elc_oa; /* from enumdirs.c */
unsigned int *free_wds; /* holds the freed wds - faster reuse */
unsigned int fd; /* module global - the inotify file descriptor */
void init_free_wds(unsigned int fdin) {
unsigned int i;
fd = fdin;
if((free_wds = (unsigned int *) malloc(max_element_count * sizeof(unsigned int))) == NULL) {
fprintf(stderr, "ERROR: could not allocate mem for initial free wds list!\n");
exit(EXIT_FAILURE);
}
for(i=0; i <= max_element_count; i++) {
free_wds[i] = UINT_MAX;
}
#ifdef _DEBUG
printf("INIT_FREE_WDS_LIST: %i\n", max_element_count);
#endif
}
void adaptive_add(const char *path, struct felement *lsptr, struct stat *statdat) {
unsigned int wd, i, id = UINT_MAX;
if(isdir(path, statdat) != TRUE)
return;
/* searching the empty element list for a reusable entry */
for(i=0; i <= max_element_count; i++) {
if(free_wds[i] != UINT_MAX) {
id = free_wds[i];
break;
}
}
if(id == UINT_MAX)
id = ++elc_oa;
#ifdef _DEBUG
printf("ADDING: %s %i\n", path, elc_oa);
#endif
if((wd = inotify_add_watch(fd, path, IN_ALL_EVENTS)) < 0) {
perror("inotify_add_watch()");
exit(EXIT_FAILURE);
}
memcpy((&lsptr[id])->path, path, strlen(path));
//memcpy(&lsptr[id].festat, statdat, sizeof(struct stat));
lsptr[id].id = id;
lsptr[id].wd = wd;
}
void adaptive_delete(const char *path, struct felement *lsptr, unsigned int wd) {
if(wd == 1) /* dirty workaround! */
return;
unsigned int i, id;
/* getting the element id */
for(i=0; i <= elc_oa; i++) {
if(lsptr[i].wd == wd) {
id = lsptr[i].id;
break;
}
}
#ifdef _DEBUG
printf("removing wd -> %i path -> %s\n", wd, path);
#endif
if(inotify_rm_watch(fd, wd) != 0) {
if(errno == EBADF) {
perror("inotify_rm_watch()");
exit(EXIT_FAILURE);
}else{
#ifdef _DEBUG
fprintf(stderr, "WARNING: inotify_rm_watch(): Invalid argument: maybe a symlink issue?\n");
#endif
}
}
/* adding the empty element list entry to the available entries list */
for(i=0; i <= max_element_count; i++) {
if(free_wds[i] == UINT_MAX) {
free_wds[i] = id;
break;
}
}
//elc_oa--;
return;
}
int adaptive_action(int event_mask, const char *path, struct felement *lsptr, struct stat *statdat, unsigned int wd) {
switch(event_mask) {
case FSPY_IN_DIR_CREATE:
adaptive_add(path, lsptr, statdat);
break;
/*case FSPY_IN_DIR_DELETE:
adaptive_delete(path, wd);
break;*/
case FSPY_IN_DELETE_SELF:
adaptive_delete(path, lsptr, wd);
break;
/*case FSPY_IN_MOVE_SELF:
break;
case FSPY_IN_MOVED_FROM:
adaptive_delete(path, lsptr, statdat);
break;
case FSPY_IN_MOVED_TO:
adaptive_add(path, lsptr, statdat);
break;*/
default :
return FALSE;
break;
}
return TRUE; /* foo */
}
int adaptive_check(int event_mask, const char *path, struct felement *lsptr, struct stat *statdat, unsigned int wd) {
if(event_mask & FSPY_IN_NEED_ACTION) {
if(adaptive_action(event_mask, path, lsptr, statdat, wd) == TRUE)
return TRUE;
}
return FALSE;
}