From 4dbc5747881c12b8c31742d29e9128b7b5ea3b10 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Sun, 26 Nov 2017 20:03:00 +0100 Subject: #19 hotloader should resolve symlinks --- src/hotload.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/hotload.h | 2 +- 2 files changed, 54 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/hotload.c b/src/hotload.c index 8677cc2..4bb0aa0 100644 --- a/src/hotload.c +++ b/src/hotload.c @@ -1,4 +1,5 @@ #include "hotload.h" +#include #include #include #include @@ -40,6 +41,40 @@ file_name(const char *file) return name; } +internal char * +resolve_symlink(char *file, bool *is_symlink, bool *success) +{ + struct stat buffer; + if (lstat(file, &buffer) != 0) { + goto fail; + } + + if (S_ISLNK(buffer.st_mode)) { + ssize_t size = buffer.st_size + 1; + char *result = malloc(size); + ssize_t read = readlink(file, result, size); + + if (read == -1) { + free(result); + goto fail; + } + + result[read] = '\0'; + *is_symlink = true; + *success = true; + return result; + } else { + *is_symlink = false; + *success = true; + return file; + } + +fail: + *success = false; + return NULL; +} + + internal struct watched_file * hotloader_watched_file(struct hotloader *hotloader, char *absolutepath) { @@ -78,14 +113,26 @@ internal FSEVENT_CALLBACK(hotloader_handler) } } -void hotloader_add_file(struct hotloader *hotloader, const char *file) +void hotloader_add_file(struct hotloader *hotloader, char *file) { if (!hotloader->enabled) { - struct watched_file watch_info; - watch_info.directory = file_directory(file); - watch_info.filename = file_name(file); - hotloader->watch_list[hotloader->watch_count++] = watch_info; - printf("hotload: watching file '%s' in directory '%s'\n", watch_info.filename, watch_info.directory); + bool is_symlink, success; + char *real_path = resolve_symlink(file, &is_symlink, &success); + + if (success) { + struct watched_file watch_info; + watch_info.directory = file_directory(real_path); + watch_info.filename = file_name(real_path); + + if (is_symlink) { + free(real_path); + } + + hotloader->watch_list[hotloader->watch_count++] = watch_info; + printf("hotload: watching file '%s' in directory '%s'\n", watch_info.filename, watch_info.directory); + } else { + fprintf(stderr, "hotload: could not watch file '%s'\n", file); + } } } diff --git a/src/hotload.h b/src/hotload.h index 5948a21..ad782fb 100644 --- a/src/hotload.h +++ b/src/hotload.h @@ -27,6 +27,6 @@ struct hotloader bool hotloader_begin(struct hotloader *hotloader, hotloader_callback *callback); void hotloader_end(struct hotloader *hotloader); -void hotloader_add_file(struct hotloader *hotloader, const char *file); +void hotloader_add_file(struct hotloader *hotloader, char *file); #endif -- cgit v1.2.3 From 518927fbde35947a163c7a4800b298e16c50867e Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Tue, 12 Dec 2017 14:30:31 +0100 Subject: #19 - cleanup code for resolving symlinks --- src/hotload.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/hotload.c b/src/hotload.c index 4bb0aa0..28d7e72 100644 --- a/src/hotload.c +++ b/src/hotload.c @@ -41,40 +41,33 @@ file_name(const char *file) return name; } -internal char * -resolve_symlink(char *file, bool *is_symlink, bool *success) +internal bool +resolve_symlink(char *file, char **real_path) { struct stat buffer; if (lstat(file, &buffer) != 0) { - goto fail; + return false; } - if (S_ISLNK(buffer.st_mode)) { - ssize_t size = buffer.st_size + 1; - char *result = malloc(size); - ssize_t read = readlink(file, result, size); + if (!S_ISLNK(buffer.st_mode)) { + *real_path = file; + return true; + } - if (read == -1) { - free(result); - goto fail; - } + ssize_t size = buffer.st_size + 1; + char *result = malloc(size); + ssize_t read = readlink(file, result, size); + if (read != -1) { result[read] = '\0'; - *is_symlink = true; - *success = true; - return result; - } else { - *is_symlink = false; - *success = true; - return file; + *real_path = result; + return true; } -fail: - *success = false; - return NULL; + free(result); + return false; } - internal struct watched_file * hotloader_watched_file(struct hotloader *hotloader, char *absolutepath) { @@ -116,15 +109,16 @@ internal FSEVENT_CALLBACK(hotloader_handler) void hotloader_add_file(struct hotloader *hotloader, char *file) { if (!hotloader->enabled) { - bool is_symlink, success; - char *real_path = resolve_symlink(file, &is_symlink, &success); + char *real_path; + bool success = resolve_symlink(file, &real_path); if (success) { struct watched_file watch_info; watch_info.directory = file_directory(real_path); watch_info.filename = file_name(real_path); - if (is_symlink) { + if (real_path != file) { + printf("hotload: symlink resolved, '%s' -> '%s'\n", file, real_path); free(real_path); } -- cgit v1.2.3 From 925320065202f6a4b234171ba6b3311e811f72d3 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Tue, 12 Dec 2017 14:42:09 +0100 Subject: #19 - cleanup code for resolving symlinks --- src/hotload.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/hotload.c b/src/hotload.c index 28d7e72..ad02d21 100644 --- a/src/hotload.c +++ b/src/hotload.c @@ -41,17 +41,16 @@ file_name(const char *file) return name; } -internal bool -resolve_symlink(char *file, char **real_path) +internal char * +resolve_symlink(char *file) { struct stat buffer; if (lstat(file, &buffer) != 0) { - return false; + return NULL; } if (!S_ISLNK(buffer.st_mode)) { - *real_path = file; - return true; + return file; } ssize_t size = buffer.st_size + 1; @@ -60,12 +59,11 @@ resolve_symlink(char *file, char **real_path) if (read != -1) { result[read] = '\0'; - *real_path = result; - return true; + return result; } free(result); - return false; + return NULL; } internal struct watched_file * @@ -109,16 +107,13 @@ internal FSEVENT_CALLBACK(hotloader_handler) void hotloader_add_file(struct hotloader *hotloader, char *file) { if (!hotloader->enabled) { - char *real_path; - bool success = resolve_symlink(file, &real_path); - - if (success) { + char *real_path = resolve_symlink(file); + if (real_path) { struct watched_file watch_info; watch_info.directory = file_directory(real_path); watch_info.filename = file_name(real_path); if (real_path != file) { - printf("hotload: symlink resolved, '%s' -> '%s'\n", file, real_path); free(real_path); } -- cgit v1.2.3 From 707e47323a08332d936d33ae291005e632e1d8df Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Tue, 19 Dec 2017 17:56:35 +0100 Subject: #21 - fix minor memory leak on parse-failure --- src/parse.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/parse.c b/src/parse.c index 6a12963..c19cebb 100644 --- a/src/parse.c +++ b/src/parse.c @@ -155,7 +155,7 @@ parse_hotkey(struct parser *parser) if (parser_match(parser, Token_Modifier)) { hotkey->flags = parse_modifier(parser); if (parser->error) { - return NULL; + goto err; } found_modifier = 1; } else { @@ -165,7 +165,7 @@ parse_hotkey(struct parser *parser) if (found_modifier) { if (!parser_match(parser, Token_Dash)) { parser_report_error(parser, "expected '-'"); - return NULL; + goto err; } } @@ -177,7 +177,7 @@ parse_hotkey(struct parser *parser) parse_key_literal(parser, hotkey); } else { parser_report_error(parser, "expected key-literal"); - return NULL; + goto err; } if (parser_match(parser, Token_Arrow)) { @@ -188,12 +188,16 @@ parse_hotkey(struct parser *parser) hotkey->command = parse_command(parser); } else { parser_report_error(parser, "expected ':' followed by command"); - return NULL; + goto err; } printf("}\n"); return hotkey; + +err: + free(hotkey); + return NULL; } void parse_config(struct parser *parser, struct table *hotkey_map) @@ -204,12 +208,12 @@ void parse_config(struct parser *parser, struct table *hotkey_map) (parser_check(parser, Token_Literal)) || (parser_check(parser, Token_Key_Hex)) || (parser_check(parser, Token_Key))) { - hotkey = parse_hotkey(parser); - if (parser->error) { + if ((hotkey = parse_hotkey(parser))) { + table_add(hotkey_map, hotkey, hotkey); + } else if (parser->error) { free_hotkeys(hotkey_map); return; } - table_add(hotkey_map, hotkey, hotkey); } else { parser_report_error(parser, "expected modifier or key-literal"); return; -- cgit v1.2.3 From 8b8f85402dff702c5f85e97dee56251e948e6223 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Fri, 22 Dec 2017 17:38:35 +0100 Subject: changed printing of linenumber --- src/parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/parse.c b/src/parse.c index c19cebb..14b1d14 100644 --- a/src/parse.c +++ b/src/parse.c @@ -150,7 +150,7 @@ parse_hotkey(struct parser *parser) struct hotkey *hotkey = malloc(sizeof(struct hotkey)); int found_modifier; - printf("(#%d) hotkey :: {\n", parser->current_token.line); + printf("hotkey :: #%d {\n", parser->current_token.line); if (parser_match(parser, Token_Modifier)) { hotkey->flags = parse_modifier(parser); @@ -269,7 +269,7 @@ void parser_report_error(struct parser *parser, const char *format, ...) { va_list args; va_start(args, format); - fprintf(stderr, "(#%d:%d) ", parser->current_token.line, parser->current_token.cursor); + fprintf(stderr, "#%d:%d error: ", parser->current_token.line, parser->current_token.cursor); vfprintf(stderr, format, args); fprintf(stderr, ", but got '%.*s'\n", parser->current_token.length, parser->current_token.text); va_end(args); -- cgit v1.2.3