-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement POSIX polling dirmonitor #1926
Open
Jan200101
wants to merge
1
commit into
lite-xl:master
Choose a base branch
from
Jan200101:PR/polling-dirmonitor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
#include <time.h> | ||
#include <SDL.h> | ||
|
||
#define EVENT_MODIFIED (1 << 0) | ||
|
||
#if _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE > 700 || _POSIX_C_SOURCE >= 200809L | ||
#define STAT_HAS_TIM | ||
#endif | ||
|
||
struct file_info { | ||
int fd; | ||
dev_t dev; | ||
ino_t ino; | ||
struct timespec mtim; | ||
int events; | ||
}; | ||
|
||
struct dirmonitor_internal { | ||
struct file_info* fds; | ||
size_t fd_count; | ||
}; | ||
|
||
|
||
struct dirmonitor_internal* init_dirmonitor() { | ||
struct dirmonitor_internal* monitor = calloc(1, sizeof(struct dirmonitor_internal)); | ||
|
||
monitor->fd_count = 0; | ||
monitor->fds = NULL; | ||
return monitor; | ||
} | ||
|
||
void deinit_dirmonitor(struct dirmonitor_internal* monitor) { | ||
for (size_t i = 0; i < monitor->fd_count; ++i) | ||
close(monitor->fds[i].fd); | ||
free(monitor->fds); | ||
} | ||
|
||
int get_changes_dirmonitor(struct dirmonitor_internal* monitor, char* buffer, int len) | ||
{ | ||
struct stat new_stat; | ||
int c = 0; | ||
|
||
for (size_t i = 0; i < monitor->fd_count; ++i) { | ||
if (monitor->fds[i].fd < 0) | ||
continue; | ||
|
||
fstat(monitor->fds[i].fd, &new_stat); | ||
|
||
if ( | ||
new_stat.st_mtime > monitor->fds[i].mtim.tv_sec || | ||
#ifdef STAT_HAS_TIM | ||
(new_stat.st_mtim.tv_sec == monitor->fds[i].mtim.tv_sec && new_stat.st_mtim.tv_nsec > monitor->fds[i].mtim.tv_nsec) || | ||
#endif | ||
new_stat.st_dev != monitor->fds[i].dev || // device changed | ||
new_stat.st_ino != monitor->fds[i].ino || // inode changed | ||
0 | ||
) { | ||
monitor->fds[i].mtim.tv_sec = new_stat.st_mtime; | ||
#ifdef STAT_HAS_TIM | ||
monitor->fds[i].mtim.tv_nsec = new_stat.st_mtim.tv_nsec; | ||
#endif | ||
monitor->fds[i].dev = new_stat.st_dev; | ||
monitor->fds[i].ino = new_stat.st_ino; | ||
monitor->fds[i].events |= EVENT_MODIFIED; | ||
c += 1; | ||
} | ||
} | ||
|
||
if (!c) | ||
SDL_Delay(100); | ||
|
||
return c; | ||
} | ||
|
||
int translate_changes_dirmonitor(struct dirmonitor_internal* monitor, char* buffer, int count, int (*change_callback)(int, const char*, void*), void* data) { | ||
for (size_t i = 0; i < monitor->fd_count; ++i) { | ||
if (monitor->fds[i].events & EVENT_MODIFIED) { | ||
--count; | ||
monitor->fds[i].events = monitor->fds[i].events & ~ EVENT_MODIFIED; | ||
change_callback(monitor->fds[i].fd, NULL, data); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int add_dirmonitor(struct dirmonitor_internal* monitor, const char* path) | ||
{ | ||
int fd = open(path, O_RDONLY | O_NOFOLLOW); | ||
if (fd == -1) | ||
return -1; | ||
|
||
// Check if there is any space we can reuse | ||
for (size_t i = 0; i < monitor->fd_count; ++i) { | ||
if (monitor->fds[i].fd == -1) { | ||
monitor->fds[i].fd = fd; | ||
monitor->fds[i].mtim.tv_sec = 0; | ||
return fd; | ||
} | ||
} | ||
|
||
// There is no empty space, add a new entry; | ||
// Prepare a new list before switching it to prevent race conditions | ||
struct file_info* old_fds = monitor->fds; | ||
struct file_info* new_fds; | ||
new_fds = malloc(sizeof(*new_fds) * (monitor->fd_count + 1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still not too sure about just allocating one more space. |
||
if (old_fds) | ||
memcpy(new_fds, old_fds, sizeof(*new_fds) * (monitor->fd_count)); | ||
|
||
new_fds[monitor->fd_count].fd = fd; | ||
new_fds[monitor->fd_count].mtim.tv_sec = 0; | ||
|
||
monitor->fds = new_fds; | ||
free(old_fds); | ||
|
||
monitor->fd_count++; | ||
|
||
return fd; | ||
} | ||
|
||
void remove_dirmonitor(struct dirmonitor_internal* monitor, int fd) | ||
{ | ||
for (size_t i = 0; i < monitor->fd_count; ++i) { | ||
if (monitor->fds[i].fd == fd) { | ||
int fd = monitor->fds[i].fd; | ||
|
||
monitor->fds[i].fd = -1; | ||
monitor->fds[i].events = 0; | ||
close(fd); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
int get_mode_dirmonitor() { | ||
return 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could
fd_count
be raced over?Same with the
fds
structs.For example:
get_changes_dirmonitor
add_dirmonitor
add_dirmonitor
needs to allocate new space, so it does, but still doesn't switch it outget_changes_dirmonitor
savesfds
in a registeradd_dirmonitor
switches out the spaces, frees the oldfds
get_changes_dirmonitor
uses thefds
from the register, SIGSEGVs.