From 9459ec93132e3efd0d154b6dcb5a7b03ef6e7b0f Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 26 Sep 2024 11:51:42 +0900 Subject: [PATCH] Reload config file if cache is older than config --- src/html_cache.c | 23 ++++++++++++++++++++++- src/html_cache.h | 2 +- src/http.c | 2 +- src/http.h | 2 +- src/test.c | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/html_cache.c b/src/html_cache.c index 212ccba..4ab6fab 100644 --- a/src/html_cache.c +++ b/src/html_cache.c @@ -27,6 +27,7 @@ // Third-party includes. #include +#include #include // Local includes. @@ -206,7 +207,7 @@ int c_simple_http_cache_path( const char *path, const char *config_filename, const char *cache_dir, - const C_SIMPLE_HTTP_HTTPTemplates *templates, + C_SIMPLE_HTTP_HTTPTemplates *templates, char **buf_out) { if (!path) { fprintf(stderr, "ERROR cache_path function: path is NULL!\n"); @@ -262,6 +263,7 @@ int c_simple_http_cache_path( // Get "stat" info on the cache filename. uint_fast8_t force_cache_update = 0; struct stat cache_file_stat; + memset(&cache_file_stat, 0, sizeof(struct stat)); ret = stat(cache_filename_full, &cache_file_stat); if (ret == -1) { if (errno == ENOENT) { @@ -279,6 +281,7 @@ int c_simple_http_cache_path( // Get "stat" info on config file. struct stat config_file_stat; + memset(&config_file_stat, 0, sizeof(struct stat)); ret = stat(config_filename, &config_file_stat); if (ret == -1) { if (errno == ENOENT) { @@ -395,6 +398,24 @@ CACHE_FILE_WRITE_CHECK: && cache_file_stat.st_mtim.tv_nsec < config_file_stat.st_mtim.tv_nsec)) { // Cache file is out of date. + + if (cache_file_stat.st_mtim.tv_sec < config_file_stat.st_mtim.tv_sec + || (cache_file_stat.st_mtim.tv_sec == config_file_stat.st_mtim.tv_sec + && cache_file_stat.st_mtim.tv_nsec + < config_file_stat.st_mtim.tv_nsec)) + { + // Reload config file. + C_SIMPLE_HTTP_HTTPTemplates new_parsed_config = + c_simple_http_parse_config( + config_filename, + "PATH", + NULL); + if (new_parsed_config.hash_map) { + simple_archiver_hash_map_free(&templates->hash_map); + *templates = new_parsed_config; + } + } + __attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) FILE *cache_fd = fopen(cache_filename_full, "w"); if (fwrite("--- CACHE ENTRY ---\n", 1, 20, cache_fd) != 20) { diff --git a/src/html_cache.h b/src/html_cache.h index 95a36ab..ebaa385 100644 --- a/src/html_cache.h +++ b/src/html_cache.h @@ -36,7 +36,7 @@ int c_simple_http_cache_path( const char *path, const char *config_filename, const char *cache_dir, - const C_SIMPLE_HTTP_HTTPTemplates *templates, + C_SIMPLE_HTTP_HTTPTemplates *templates, char **buf_out); #endif diff --git a/src/http.c b/src/http.c index e32e54b..9ced398 100644 --- a/src/http.c +++ b/src/http.c @@ -62,7 +62,7 @@ const char *c_simple_http_response_code_error_to_response( char *c_simple_http_request_response( const char *request, uint32_t size, - const C_SIMPLE_HTTP_HTTPTemplates *templates, + C_SIMPLE_HTTP_HTTPTemplates *templates, size_t *out_size, enum C_SIMPLE_HTTP_ResponseCode *out_response_code, const char *cache_dir, diff --git a/src/http.h b/src/http.h index 725fbf8..87618c4 100644 --- a/src/http.h +++ b/src/http.h @@ -46,7 +46,7 @@ const char *c_simple_http_response_code_error_to_response( char *c_simple_http_request_response( const char *request, uint32_t size, - const C_SIMPLE_HTTP_HTTPTemplates *templates, + C_SIMPLE_HTTP_HTTPTemplates *templates, size_t *out_size, enum C_SIMPLE_HTTP_ResponseCode *out_response_code, const char *cache_dir, diff --git a/src/test.c b/src/test.c index 8430b10..b397679 100644 --- a/src/test.c +++ b/src/test.c @@ -908,6 +908,40 @@ int main(void) { ASSERT_TRUE(cache_file_exists); CHECK_TRUE(cache_file_size_2 == cache_file_size_3); + // Edit config file. + test_file = fopen(test_http_template_filename5, "w"); + ASSERT_TRUE(test_file); + + ASSERT_TRUE( + fwrite( + "PATH=/\nHTML='''

{{{VAR_FILE}}}

'''\n", + 1, + 42, + test_file) + == 42); + ASSERT_TRUE( + fwrite( + "VAR_FILE=/tmp/c_simple_http_template_test_var2.html\n", + 1, + 52, + test_file) + == 52); + + fclose(test_file); + + // Re-run cache function, checking that it is invalidated. + int_ret = c_simple_http_cache_path( + "/", + test_http_template_filename5, + "/tmp/c_simple_http_cache_dir", + &templates, + &buf); + CHECK_TRUE(int_ret > 0); + ASSERT_TRUE(buf); + CHECK_TRUE(strcmp(buf, "

Alternate test text.
Yep.

") == 0); + free(buf); + buf = NULL; + // Cleanup. remove("/tmp/c_simple_http_cache_dir/ROOT"); rmdir("/tmp/c_simple_http_cache_dir");