feat: add simple two-thread watch prototype

I feel like I'm crushing it today!
This commit is contained in:
Asger Gitz-Johansen 2024-08-03 11:13:09 +02:00
parent 9d1408c174
commit 121643be45
5 changed files with 61 additions and 10 deletions

View File

@ -20,6 +20,7 @@ CFLAGS += -Wall -Werror -std=c23 -g
# includes # includes
CFLAGS += -Iinclude CFLAGS += -Iinclude
# libraries # libraries
CFLAGS += -lpthread
.PHONY: all clean .PHONY: all clean

View File

@ -81,7 +81,7 @@ If you want `compile_commands.json` files, you should use [bear](https://github.
- [x] First things first, let's implement something that reacts when some provided file changes (not poll please). - [x] First things first, let's implement something that reacts when some provided file changes (not poll please).
- [x] Second things second, implement a simple logging system with differing levels of verbosity and configurable - [x] Second things second, implement a simple logging system with differing levels of verbosity and configurable
output file using cli options. output file using cli options.
- [ ] Third things third, implement a thing that simultaneously watches two different files (multithreading). - [x] Third things third, implement a thing that simultaneously watches two different files (multithreading).
it should be cancellable with ctrl+c, but it should just contiuously print event notifications. it should be cancellable with ctrl+c, but it should just contiuously print event notifications.
- [ ] Fourth things fourth, implement a prototype that reads a space-separated file and populates a struct. - [ ] Fourth things fourth, implement a prototype that reads a space-separated file and populates a struct.

View File

@ -4,7 +4,11 @@
#include "optional.h" #include "optional.h"
typedef struct { typedef struct {
// prototyping
optional_str file; optional_str file;
optional_str file2;
// actual
int verbosity; int verbosity;
bool help; bool help;
bool version; bool version;

View File

@ -9,6 +9,9 @@ cli_options new_options() {
result.file.has_value = false; result.file.has_value = false;
result.file.value = NULL; result.file.value = NULL;
result.file2.has_value = false;
result.file2.value = NULL;
result.verbosity = 1; result.verbosity = 1;
result.help = false; result.help = false;
@ -29,10 +32,12 @@ cli_options new_options() {
void free_options(cli_options v) { void free_options(cli_options v) {
if(v.file.has_value) if(v.file.has_value)
free(v.file.value); free(v.file.value);
if(v.file2.has_value)
free(v.file2.value);
} }
// <max // <max
const char* optstring = "f:v:Cl:hV"; const char* optstring = "f:F:v:Cl:hV";
const char* help_msg = const char* help_msg =
"Usage: %s [-v level] [-C] [-l file] [-h] [-V]\n" "Usage: %s [-v level] [-C] [-l file] [-h] [-V]\n"
"\n" "\n"
@ -42,7 +47,8 @@ const char* help_msg =
"ACTUALLY USEFUL YET\n" "ACTUALLY USEFUL YET\n"
"\n" "\n"
"OPTIONS:\n" "OPTIONS:\n"
" -f file set file\n" " -f file (WIP) set file\n"
" -F file (WIP) set another file\n"
" -v level Set verbosity level [0-4]\n" " -v level Set verbosity level [0-4]\n"
" -C Force color output, ignoring $NO_COLOR\n" " -C Force color output, ignoring $NO_COLOR\n"
" -l file Set log to output to a file\n" " -l file Set log to output to a file\n"
@ -64,6 +70,10 @@ cli_options parse(int argc, char** argv) {
options.file.value = strdup(optarg); options.file.value = strdup(optarg);
options.file.has_value = true; options.file.has_value = true;
break; break;
case 'F':
options.file2.value = strdup(optarg);
options.file2.has_value = true;
break;
case 'v': case 'v':
options.verbosity = atoi(optarg); options.verbosity = atoi(optarg);
break; break;

View File

@ -1,10 +1,13 @@
#include "cli.h" #include "cli.h"
#include "log.h" #include "log.h"
#include "notify.h" #include "notify.h"
#include "util.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
void on_event(struct inotify_event* const e) { void on_event(struct inotify_event* const e) {
const char* msg = const char* msg =
"got an event:\n" "got an event:\n"
@ -17,6 +20,12 @@ void on_event(struct inotify_event* const e) {
log_info(msg, e->wd, e->mask, e->cookie, e->len, e->name); log_info(msg, e->wd, e->mask, e->cookie, e->len, e->name);
} }
void* listen_for_changes_thread(void* data) {
const char* f = (const char*)data;
listen_for_changes(f, &on_event);
return NULL;
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
cli_options args = parse(argc, argv); cli_options args = parse(argc, argv);
log_settings settings; log_settings settings;
@ -29,19 +38,46 @@ int main(int argc, char** argv) {
print_help(stdout, argv[0]); print_help(stdout, argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if(args.version) { if(args.version) {
fprintf(stdout, SCI_VERSION "\n"); fprintf(stdout, SCI_VERSION "\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if(args.file.has_value) { if(!args.file.has_value) {
if(access(args.file.value, F_OK) != 0) { fprintf(stderr, "no file provided see -h for usage\n");
fprintf(stderr, "no such file or directory %s\n", args.file.value); exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
}
listen_for_changes(args.file.value, &on_event);
} }
if(!args.file2.has_value) {
fprintf(stderr, "no second file provided see -h for usage\n");
exit(EXIT_FAILURE);
}
if(access(args.file.value, F_OK) != 0) {
fprintf(stderr, "no such file or directory %s\n", args.file.value);
exit(EXIT_FAILURE);
}
if(access(args.file2.value, F_OK) != 0) {
fprintf(stderr, "no such file or directory %s\n", args.file2.value);
exit(EXIT_FAILURE);
}
log_trace("spawning trigger thread for %s", args.file.value);
pthread_t file1_thread;
pthread_attr_t attr1;
ASSERT_SYSCALL_SUCCESS(pthread_attr_init(&attr1));
ASSERT_SYSCALL_SUCCESS(pthread_create(&file1_thread, &attr1, &listen_for_changes_thread, (void*)args.file.value));
ASSERT_SYSCALL_SUCCESS(pthread_attr_destroy(&attr1));
log_trace("spawning trigger thread for %s", args.file2.value);
pthread_t file2_thread;
pthread_attr_t attr2;
ASSERT_SYSCALL_SUCCESS(pthread_attr_init(&attr2));
ASSERT_SYSCALL_SUCCESS(pthread_create(&file2_thread, &attr1, &listen_for_changes_thread, (void*)args.file2.value));
ASSERT_SYSCALL_SUCCESS(pthread_attr_destroy(&attr2));
pthread_join(file1_thread, NULL);
pthread_join(file2_thread, NULL);
free_options(args); free_options(args);
} }