|
@ -11,48 +11,33 @@ typedef void (Project::*ProjectReloadFn)(); |
|
|
class NotifyService { |
|
|
class NotifyService { |
|
|
public: |
|
|
public: |
|
|
NotifyService() { |
|
|
NotifyService() { |
|
|
this->inotify_fd = inotify_init1(IN_NONBLOCK); |
|
|
|
|
|
if (inotify_fd == -1) { |
|
|
|
|
|
perror("inotify_init1"); |
|
|
|
|
|
|
|
|
if (!inotifytools_initialize()) { |
|
|
|
|
|
fprintf(stderr, "inotifytools init error: %s\n", strerror(inotifytools_error())); |
|
|
exit(EXIT_FAILURE); |
|
|
exit(EXIT_FAILURE); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::cout << "inotify fd: " << inotify_fd << '\n'; |
|
|
|
|
|
|
|
|
|
|
|
this->poll_fd = {.fd = inotify_fd, .events = POLLIN, 0}; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void watch(Project &project) { |
|
|
void watch(Project &project) { |
|
|
int wd = inotify_add_watch(this->inotify_fd, project.basepath.str().c_str(), |
|
|
|
|
|
IN_MODIFY); |
|
|
|
|
|
|
|
|
|
|
|
if (wd == -1) { |
|
|
|
|
|
std::cerr << "Failed to watch project base directory: " << project.basepath.str().c_str() << '\n'; |
|
|
|
|
|
perror("inotify_add_watch"); |
|
|
|
|
|
exit(EXIT_FAILURE); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::cerr << "Watching " << project.basepath << " with wd: " << wd << '\n'; |
|
|
|
|
|
|
|
|
|
|
|
this->watch_ids.push_back(wd); |
|
|
|
|
|
|
|
|
inotifytools_watch_recursively(project.basepath.str().c_str(), |
|
|
|
|
|
IN_MODIFY | IN_MOVED_TO); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void _poll() { |
|
|
void _poll() { |
|
|
int rc = poll(&this->poll_fd, 1, 0); |
|
|
|
|
|
std::cout << "poll() -> " << rc << '\n'; |
|
|
|
|
|
if (rc == -1) { |
|
|
|
|
|
if (errno == EINTR) |
|
|
|
|
|
return; |
|
|
|
|
|
perror("poll"); |
|
|
|
|
|
|
|
|
struct inotify_event *event = inotifytools_next_event(0); |
|
|
|
|
|
|
|
|
|
|
|
if (inotifytools_error() == EINTR) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
if (inotifytools_error() != 0) { |
|
|
|
|
|
fprintf(stderr, "inotifytools event error: %s\n", strerror(inotifytools_error())); |
|
|
exit(EXIT_FAILURE); |
|
|
exit(EXIT_FAILURE); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (rc > 0) { |
|
|
|
|
|
std::cout << "poll data available!\n"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (event != NULL) { |
|
|
|
|
|
std::cout << "new inotify event! file: " << event->name << '\n'; |
|
|
|
|
|
std::cout << "filename: " << inotifytools_filename_from_wd(event->wd) << '\n'; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
private: |
|
|
private: |
|
|
int inotify_fd; |
|
|
|
|
|
struct pollfd poll_fd; |
|
|
|
|
|
std::vector<int> watch_ids; |
|
|
|
|
|
}; |
|
|
}; |