Update dmon from septag/dmon with fix for linux

Update from https://github.com/septag/dmon, commit: 48234fc2 to
include a fix for linux.
This commit is contained in:
Francesco Abbate 2021-07-14 12:36:17 +02:00
parent d7bdca3f48
commit 3101cfff7b
1 changed files with 36 additions and 48 deletions

View File

@ -763,19 +763,6 @@ _DMON_PRIVATE void dmon__watch_recursive(const char* dirname, int fd, uint32_t m
closedir(dir); closedir(dir);
} }
_DMON_PRIVATE const char* dmon__find_subdir(const dmon__watch_state* watch, int wd)
{
const int* wds = watch->wds;
for (int i = 0, c = stb_sb_count(wds); i < c; i++) {
if (wd == wds[i]) {
return watch->subdirs[i].rootdir;
}
}
DMON_ASSERT(0);
return NULL;
}
_DMON_PRIVATE void dmon__inotify_process_events(void) _DMON_PRIVATE void dmon__inotify_process_events(void)
{ {
for (int i = 0, c = stb_sb_count(_dmon.events); i < c; i++) { for (int i = 0, c = stb_sb_count(_dmon.events); i < c; i++) {
@ -811,10 +798,9 @@ _DMON_PRIVATE void dmon__inotify_process_events(void)
break; break;
} }
} }
} else if (check_ev->mask == IN_MODIFY && } else if (check_ev->mask == IN_MODIFY && strcmp(ev->filepath, check_ev->filepath) == 0) {
strcmp(ev->filepath, check_ev->filepath) == 0) {
// Another case is that file is copied. CREATE and MODIFY happens sequentially // Another case is that file is copied. CREATE and MODIFY happens sequentially
// so we ignore modify event // so we ignore MODIFY event
check_ev->skip = true; check_ev->skip = true;
} }
} }
@ -867,12 +853,10 @@ _DMON_PRIVATE void dmon__inotify_process_events(void)
switch (ev->mask) { switch (ev->mask) {
case IN_CREATE: case IN_CREATE:
watch->watch_cb(ev->watch_id, DMON_ACTION_CREATE, watch->rootdir, ev->filepath, NULL, watch->watch_cb(ev->watch_id, DMON_ACTION_CREATE, watch->rootdir, ev->filepath, NULL, watch->user_data);
watch->user_data);
break; break;
case IN_MODIFY: case IN_MODIFY:
watch->watch_cb(ev->watch_id, DMON_ACTION_MODIFY, watch->rootdir, ev->filepath, NULL, watch->watch_cb(ev->watch_id, DMON_ACTION_MODIFY, watch->rootdir, ev->filepath, NULL, watch->user_data);
watch->user_data);
break; break;
case IN_MOVED_FROM: { case IN_MOVED_FROM: {
for (int j = i + 1; j < c; j++) { for (int j = i + 1; j < c; j++) {
@ -885,8 +869,7 @@ _DMON_PRIVATE void dmon__inotify_process_events(void)
} }
} break; } break;
case IN_DELETE: case IN_DELETE:
watch->watch_cb(ev->watch_id, DMON_ACTION_DELETE, watch->rootdir, ev->filepath, NULL, watch->watch_cb(ev->watch_id, DMON_ACTION_DELETE, watch->rootdir, ev->filepath, NULL, watch->user_data);
watch->user_data);
break; break;
} }
} }
@ -921,38 +904,43 @@ static void* dmon__thread(void* arg)
continue; continue;
} }
// Create read FD set
fd_set rfds;
FD_ZERO(&rfds);
for (int i = 0; i < _dmon.num_watches; i++) { for (int i = 0; i < _dmon.num_watches; i++) {
dmon__watch_state* watch = &_dmon.watches[i]; dmon__watch_state* watch = &_dmon.watches[i];
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(watch->fd, &rfds); FD_SET(watch->fd, &rfds);
timeout.tv_sec = 0; }
timeout.tv_usec = 100000;
if (select(FD_SETSIZE, &rfds, NULL, NULL, &timeout)) { timeout.tv_sec = 0;
ssize_t offset = 0; timeout.tv_usec = 100000;
ssize_t len = read(watch->fd, buff, _DMON_TEMP_BUFFSIZE); if (select(FD_SETSIZE, &rfds, NULL, NULL, &timeout)) {
if (len <= 0) { for (int i = 0; i < _dmon.num_watches; i++) {
continue; dmon__watch_state* watch = &_dmon.watches[i];
} if (FD_ISSET(watch->fd, &rfds)) {
ssize_t offset = 0;
while (offset < len) { ssize_t len = read(watch->fd, buff, _DMON_TEMP_BUFFSIZE);
struct inotify_event* iev = (struct inotify_event*)&buff[offset]; if (len <= 0) {
continue;
char filepath[DMON_MAX_PATH];
dmon__strcpy(filepath, sizeof(filepath), dmon__find_subdir(watch, iev->wd));
dmon__strcat(filepath, sizeof(filepath), iev->name);
// TODO: ignore directories if flag is set
if (stb_sb_count(_dmon.events) == 0) {
usecs_elapsed = 0;
} }
dmon__inotify_event dev = { { 0 }, iev->mask, iev->cookie, watch->id, false };
dmon__strcpy(dev.filepath, sizeof(dev.filepath), filepath);
stb_sb_push(_dmon.events, dev);
offset += sizeof(struct inotify_event) + iev->len; while (offset < len) {
struct inotify_event* iev = (struct inotify_event*)&buff[offset];
char filepath[DMON_MAX_PATH];
dmon__strcpy(filepath, sizeof(filepath), iev->name);
// TODO: ignore directories if flag is set
if (stb_sb_count(_dmon.events) == 0) {
usecs_elapsed = 0;
}
dmon__inotify_event dev = { { 0 }, iev->mask, iev->cookie, watch->id, false };
dmon__strcpy(dev.filepath, sizeof(dev.filepath), filepath);
stb_sb_push(_dmon.events, dev);
offset += sizeof(struct inotify_event) + iev->len;
}
} }
} }
} }