// Third-party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h>
+#include <SimpleArchiver/src/data_structures/hash_map.h>
#include <SimpleArchiver/src/helpers.h>
// Local includes.
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");
// 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) {
// 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) {
&& 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) {
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='''<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.
remove("/tmp/c_simple_http_cache_dir/ROOT");
rmdir("/tmp/c_simple_http_cache_dir");