Reload config file if cache is older than config
Some checks failed
Run Unit Tests / build-and-run-unit-tests (push) Failing after 6s

This commit is contained in:
Stephen Seo 2024-09-26 11:51:42 +09:00
parent 037845e501
commit 9459ec9313
5 changed files with 59 additions and 4 deletions

View file

@ -27,6 +27,7 @@
// Third-party includes. // Third-party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h> #include <SimpleArchiver/src/data_structures/linked_list.h>
#include <SimpleArchiver/src/data_structures/hash_map.h>
#include <SimpleArchiver/src/helpers.h> #include <SimpleArchiver/src/helpers.h>
// Local includes. // Local includes.
@ -206,7 +207,7 @@ int c_simple_http_cache_path(
const char *path, const char *path,
const char *config_filename, const char *config_filename,
const char *cache_dir, const char *cache_dir,
const C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
char **buf_out) { char **buf_out) {
if (!path) { if (!path) {
fprintf(stderr, "ERROR cache_path function: path is NULL!\n"); 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. // Get "stat" info on the cache filename.
uint_fast8_t force_cache_update = 0; uint_fast8_t force_cache_update = 0;
struct stat cache_file_stat; struct stat cache_file_stat;
memset(&cache_file_stat, 0, sizeof(struct stat));
ret = stat(cache_filename_full, &cache_file_stat); ret = stat(cache_filename_full, &cache_file_stat);
if (ret == -1) { if (ret == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
@ -279,6 +281,7 @@ int c_simple_http_cache_path(
// Get "stat" info on config file. // Get "stat" info on config file.
struct stat config_file_stat; struct stat config_file_stat;
memset(&config_file_stat, 0, sizeof(struct stat));
ret = stat(config_filename, &config_file_stat); ret = stat(config_filename, &config_file_stat);
if (ret == -1) { if (ret == -1) {
if (errno == ENOENT) { 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_stat.st_mtim.tv_nsec < config_file_stat.st_mtim.tv_nsec))
{ {
// Cache file is out of date. // 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))) __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
FILE *cache_fd = fopen(cache_filename_full, "w"); FILE *cache_fd = fopen(cache_filename_full, "w");
if (fwrite("--- CACHE ENTRY ---\n", 1, 20, cache_fd) != 20) { if (fwrite("--- CACHE ENTRY ---\n", 1, 20, cache_fd) != 20) {

View file

@ -36,7 +36,7 @@ int c_simple_http_cache_path(
const char *path, const char *path,
const char *config_filename, const char *config_filename,
const char *cache_dir, const char *cache_dir,
const C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
char **buf_out); char **buf_out);
#endif #endif

View file

@ -62,7 +62,7 @@ const char *c_simple_http_response_code_error_to_response(
char *c_simple_http_request_response( char *c_simple_http_request_response(
const char *request, const char *request,
uint32_t size, uint32_t size,
const C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *out_size, size_t *out_size,
enum C_SIMPLE_HTTP_ResponseCode *out_response_code, enum C_SIMPLE_HTTP_ResponseCode *out_response_code,
const char *cache_dir, const char *cache_dir,

View file

@ -46,7 +46,7 @@ const char *c_simple_http_response_code_error_to_response(
char *c_simple_http_request_response( char *c_simple_http_request_response(
const char *request, const char *request,
uint32_t size, uint32_t size,
const C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *out_size, size_t *out_size,
enum C_SIMPLE_HTTP_ResponseCode *out_response_code, enum C_SIMPLE_HTTP_ResponseCode *out_response_code,
const char *cache_dir, const char *cache_dir,

View file

@ -908,6 +908,40 @@ int main(void) {
ASSERT_TRUE(cache_file_exists); ASSERT_TRUE(cache_file_exists);
CHECK_TRUE(cache_file_size_2 == cache_file_size_3); 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='''<h1>{{{VAR_FILE}}}</h1>'''\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, "<h1>Alternate test text.<br>Yep.</h1>") == 0);
free(buf);
buf = NULL;
// Cleanup. // Cleanup.
remove("/tmp/c_simple_http_cache_dir/ROOT"); remove("/tmp/c_simple_http_cache_dir/ROOT");
rmdir("/tmp/c_simple_http_cache_dir"); rmdir("/tmp/c_simple_http_cache_dir");