-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfs.c
141 lines (115 loc) · 2.75 KB
/
fs.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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "callbacks.h"
#include "err.h"
#include "fs.h"
int
is_directory(const char * name) {
struct stat st;
int ret;
ret = stat(name, &st);
if (ret == -1)
return ret;
return S_ISDIR(st.st_mode);
}
int
prepare_fs_event_fd() {
int fd;
fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (fd == -1) {
perror("inotify_init1");
exit(1);
}
return fd;
}
enum nd_err
read_log_file(struct fs_watch * watch) {
ssize_t max_line_length;
const char * line;
ssize_t ret;
char * end;
if (watch->fd == -1)
return ND_FILE;
while ((ret = read(watch->fd, watch->buf + watch->buffered, sizeof watch->buf - watch->buffered)) > 0) {
line = watch->buf;
ret += watch->buffered;
watch->buffered = 0;
for (;;) {
max_line_length = ret - (line - watch->buf);
end = memchr(line, '\n', max_line_length);
if (end) {
*end = '\0';
if (watch->skip == DO_NOT_SKIP)
watch->func->process(line, watch->data);
else
watch->skip = DO_NOT_SKIP;
line = end + 1;
} else {
if (max_line_length > 0 && max_line_length < sizeof watch->buf) {
watch->buffered = max_line_length;
memmove(watch->buf, line, watch->buffered);
} else if (max_line_length == sizeof watch->buf) {
watch->buf[sizeof watch->buf - 1] = '\0';
if (watch->skip == DO_NOT_SKIP)
watch->func->process(line, watch->data);
watch->skip = SKIP_THE_REST;
}
break;
}
}
}
return ND_SUCCESS;
}
static
void
reopen_log_file(struct fs_watch * watch) {
char file_name[BUFSIZ];
sprintf(file_name, "%s/%s", watch->dir_name, watch->file_name);
close(watch->fd);
watch->fd = open(file_name, O_RDONLY);
}
static
void
process_fs_event(const struct inotify_event * event, struct fs_watch * watchers, size_t watchers_length) {
struct fs_watch * item;
int i;
for (i = 0; i < watchers_length; i++) {
item = watchers + i;
if (event->wd == item->watch_dir) {
if (event->len) {
if (!strcmp(event->name, item->file_name)) {
read_log_file(item);
reopen_log_file(item);
}
}
}
}
}
void
process_fs_event_queue(const int fd, struct fs_watch * watchers, size_t watchers_length) {
const struct inotify_event * event;
char buf[BUFSIZ];
ssize_t len;
char * ptr;
for (;;) {
len = read(fd, buf, sizeof buf);
if (len == -1 && errno != EAGAIN) {
perror("E: Cannot read fs_event fd");
exit(1);
}
if (len <= 0)
break;
for (ptr = buf; ptr < buf + len; ptr += sizeof * event + event->len) {
event = (const struct inotify_event *)ptr;
process_fs_event(event, watchers, watchers_length);
}
}
}