forked from iizukanao/picam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.c
246 lines (218 loc) · 5.95 KB
/
hooks.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
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <pthread.h>
#include <signal.h>
#include <dirent.h>
#include "hooks.h"
#define NUM_EVENT_BUF 10
#define EVENT_NAME_BUF_LEN 32
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( NUM_EVENT_BUF * ( EVENT_SIZE + EVENT_NAME_BUF_LEN ) )
typedef struct watch_target {
char *dir;
void (*callback)(char *, char *);
int read_content;
} watch_target;
static int keep_watching = 1;
static pthread_t *watcher_thread;
void sig_handler(int signum) {
}
// Create hooks dir if it does not exist
int hooks_create_dir(char *dir) {
struct stat st;
int err;
err = stat(dir, &st);
if (err == -1) {
if (errno == ENOENT) {
// create directory
if (mkdir(dir, 0755) == 0) { // success
fprintf(stderr, "created hooks dir: ./%s\n", dir);
} else { // error
fprintf(stderr, "error creating hooks dir (./%s): %s\n",
dir, strerror(errno));
return -1;
}
} else {
perror("stat hooks dir");
return -1;
}
} else {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "hooks dir (./%s) is not a directory\n",
dir);
return -1;
}
}
if (access(dir, R_OK) != 0) {
fprintf(stderr, "Can't access hooks dir (./%s): %s\n",
dir, strerror(errno));
return -1;
}
return 0;
}
int clear_hooks(char *dirname) {
DIR *dir;
struct dirent *ent;
char *path = NULL;
char *new_path;
int path_len;
int dirname_len;
int status = 0;
dirname_len = strlen(dirname);
if ((dir = opendir(dirname)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 ||
strcmp(ent->d_name, "..") == 0) {
continue;
}
path_len = dirname_len + strlen(ent->d_name) + 2;
if (path == NULL) {
path = malloc(path_len);
if (path == NULL) {
perror("malloc path failed");
closedir(dir);
return -1;
}
} else {
new_path = realloc(path, path_len);
if (new_path == NULL) {
perror("realloc path failed");
closedir(dir);
return -1;
}
path = new_path;
}
snprintf(path, path_len, "%s/%s", dirname, ent->d_name);
if (unlink(path) != 0) {
perror("unlink failed");
status = -1;
}
}
closedir(dir);
if (path != NULL) {
free(path);
}
} else {
status = -1;
}
return status;
}
void *watch_for_file_creation(watch_target *target) {
int length, i;
int fd;
int wd;
int dir_strlen;
char buffer[EVENT_BUF_LEN];
char *dir = target->dir;
void (*callback)(char *, char *) = target->callback;
int read_content = target->read_content;
free(target);
dir_strlen = strlen(dir);
struct sigaction term_handler = {.sa_handler = sig_handler};
sigaction(SIGTERM, &term_handler, NULL);
fd = inotify_init();
if (fd < 0) {
perror("inotify_init error");
exit(EXIT_FAILURE);
}
struct stat st;
int err = stat(dir, &st);
if (err == -1) {
if (errno == ENOENT) {
fprintf(stderr, "Error: %s directory does not exist\n", dir);
} else {
perror("stat error");
}
exit(EXIT_FAILURE);
} else {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "Error: %s is not a directory\n", dir);
exit(EXIT_FAILURE);
}
}
if (access(dir, R_OK) != 0) {
perror("Can't access hook target directory");
exit(EXIT_FAILURE);
}
uint32_t inotify_mask;
if (read_content) {
inotify_mask = IN_CLOSE_WRITE;
} else {
inotify_mask = IN_CREATE;
}
wd = inotify_add_watch(fd, dir, inotify_mask);
while (keep_watching) {
length = read(fd, buffer, EVENT_BUF_LEN);
if (length < 0) {
break;
}
i = 0;
while (i < length) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if (event->len) {
if (event->mask & inotify_mask) {
if (!(event->mask & IN_ISDIR)) { // file
int path_len = dir_strlen + strlen(event->name) + 2;
char *path = malloc(path_len);
if (path == NULL) {
perror("malloc for file path failed");
} else {
snprintf(path, path_len, "%s/%s", dir, event->name);
if (read_content) {
// Read file contents
FILE *fp = fopen(path, "rb");
char *content = NULL;
if (fp) {
fseek(fp, 0, SEEK_END);
long content_len = ftell(fp);
fseek(fp, 0, SEEK_SET);
content = malloc(content_len + 1);
if (content) {
fread(content, 1, content_len, fp);
content[content_len] = '\0';
} else {
perror("malloc for file content failed");
}
fclose(fp);
} else {
perror("fopen failed");
}
callback(event->name, content);
free(content);
} else {
callback(event->name, NULL);
}
// Delete that file
if (unlink(path) != 0) {
perror("unlink failed");
}
free(path);
}
}
}
}
i += EVENT_SIZE + event->len;
}
}
inotify_rm_watch(fd, wd);
close(fd);
pthread_exit(0);
}
void start_watching_hooks(pthread_t *thread, char *dir, void (*callback)(char *, char *), int read_content) {
watch_target *target = malloc(sizeof(watch_target));
target->dir = dir;
target->callback = callback;
target->read_content = read_content;
pthread_create(thread, NULL, (void * (*)(void *))watch_for_file_creation, target);
watcher_thread = thread;
}
void stop_watching_hooks() {
keep_watching = 0;
pthread_kill(*watcher_thread, SIGTERM);
}