refactor: Add clang-tidy checks and simplify codebase
This commit is contained in:
parent
f18100c2ee
commit
c166ffe0b4
17 changed files with 130 additions and 78 deletions
|
|
@ -8,20 +8,16 @@
|
|||
#include "block.h"
|
||||
#include "util.h"
|
||||
|
||||
watcher watcher_new(const block* const blocks,
|
||||
const unsigned short block_count) {
|
||||
watcher watcher = {
|
||||
.blocks = blocks,
|
||||
.block_count = block_count,
|
||||
};
|
||||
|
||||
return watcher;
|
||||
static bool watcher_fd_is_readable(const watcher_fd* const watcher_fd) {
|
||||
return (watcher_fd->revents & POLLIN) != 0;
|
||||
}
|
||||
|
||||
int watcher_init(watcher* const watcher, const int signal_fd) {
|
||||
int watcher_init(watcher* const watcher, const block* const blocks,
|
||||
const unsigned short block_count, const int signal_fd) {
|
||||
if (signal_fd == -1) {
|
||||
fprintf(stderr,
|
||||
"error: invalid signal file descriptor passed to watcher\n");
|
||||
(void)fprintf(
|
||||
stderr,
|
||||
"error: invalid signal file descriptor passed to watcher\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -29,10 +25,10 @@ int watcher_init(watcher* const watcher, const int signal_fd) {
|
|||
fd->fd = signal_fd;
|
||||
fd->events = POLLIN;
|
||||
|
||||
for (unsigned short i = 0; i < watcher->block_count; ++i) {
|
||||
const int block_fd = watcher->blocks[i].pipe[READ_END];
|
||||
for (unsigned short i = 0; i < block_count; ++i) {
|
||||
const int block_fd = blocks[i].pipe[READ_END];
|
||||
if (block_fd == -1) {
|
||||
fprintf(
|
||||
(void)fprintf(
|
||||
stderr,
|
||||
"error: invalid block file descriptors passed to watcher\n");
|
||||
return 1;
|
||||
|
|
@ -47,17 +43,27 @@ int watcher_init(watcher* const watcher, const int signal_fd) {
|
|||
}
|
||||
|
||||
int watcher_poll(watcher* watcher, const int timeout_ms) {
|
||||
const int event_count = poll(watcher->fds, LEN(watcher->fds), timeout_ms);
|
||||
int event_count = poll(watcher->fds, LEN(watcher->fds), timeout_ms);
|
||||
|
||||
// Don't return non-zero status for signal interruptions.
|
||||
if (event_count == -1 && errno != EINTR) {
|
||||
(void)fprintf(stderr, "error: watcher could not poll blocks\n");
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return event_count;
|
||||
}
|
||||
watcher->got_signal = watcher_fd_is_readable(&watcher->fds[SIGNAL_FD]);
|
||||
|
||||
bool watcher_fd_is_readable(const watcher_fd* const watcher_fd) {
|
||||
return (watcher_fd->revents & POLLIN) != 0;
|
||||
watcher->active_block_count = event_count - (int)watcher->got_signal;
|
||||
unsigned short i = 0;
|
||||
unsigned short j = 0;
|
||||
while (i < event_count && j < LEN(watcher->active_blocks)) {
|
||||
if (watcher_fd_is_readable(&watcher->fds[j])) {
|
||||
watcher->active_blocks[i] = j;
|
||||
++i;
|
||||
}
|
||||
|
||||
++j;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue