diff options
author | louie <lshprung@yahoo.com> | 2021-01-01 17:19:36 -0800 |
---|---|---|
committer | louie <lshprung@yahoo.com> | 2021-01-01 17:19:36 -0800 |
commit | 747a0033329875be91edaee6d1c4ccea65e594dc (patch) | |
tree | d6f21f570f88f4256de7ab5879d7840446634f86 | |
parent | d7cc4b460523abbe3a4ed85066939cf89df41080 (diff) |
Fixed caching bug related to last modification time
-rw-r--r-- | cache.c | 16 | ||||
-rw-r--r-- | unix/cache.c | 1 | ||||
-rw-r--r-- | windows/cache.c | 1 |
3 files changed, 14 insertions, 4 deletions
@@ -2,12 +2,16 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> #include "include/cache.h" #include "include/read_cfg.h" void save_to_cache(int g_hover, int e_hover, int true_hover, char *cfg_name){ FILE *fp; + struct stat cfg_stat; //for determining last modification time of file char *path = get_cache_path(true); //ensure get_cache_path() did not return NULL @@ -20,8 +24,12 @@ void save_to_cache(int g_hover, int e_hover, int true_hover, char *cfg_name){ return; } + //determine last modification time for cfg file + stat(cfg_name, &cfg_stat); + //write to file fwrite(cfg_name, sizeof(char), BUF_LEN, fp); + fwrite(&cfg_stat.st_mtime, sizeof(long int), 1, fp); fwrite(&g_hover, sizeof(int), 1, fp); fwrite(&e_hover, sizeof(int), 1, fp); fwrite(&true_hover, sizeof(int), 1, fp); @@ -34,6 +42,8 @@ void load_cache(int *g_hover, int *e_hover, int *true_hover, char *new_cfg_name) FILE *fp; char *path = get_cache_path(false); char saved_cfg_name[BUF_LEN]; + long int saved_cfg_mtime; + struct stat new_cfg_stat; //ensure get_cache_path() did not return NULL if(path == NULL) return; @@ -45,10 +55,12 @@ void load_cache(int *g_hover, int *e_hover, int *true_hover, char *new_cfg_name) return; } - //check if cfg_name matches; if not, do not load from cache + //check that cfg_name matches and is not newer than the cached cfg; if not, do not load from cache fread(saved_cfg_name, sizeof(char), BUF_LEN, fp); + fread(&saved_cfg_mtime, sizeof(long int), 1, fp); + stat(new_cfg_name, &new_cfg_stat); - if(!(strcmp(saved_cfg_name, new_cfg_name))){ + if(!(strcmp(saved_cfg_name, new_cfg_name)) && saved_cfg_mtime >= new_cfg_stat.st_mtime){ fread(g_hover, sizeof(int), 1, fp); fread(e_hover, sizeof(int), 1, fp); fread(true_hover, sizeof(int), 1, fp); diff --git a/unix/cache.c b/unix/cache.c index 5aa14bf..efd7cdf 100644 --- a/unix/cache.c +++ b/unix/cache.c @@ -2,7 +2,6 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -//#include <string.h> #include <sys/stat.h> #include <sys/types.h> diff --git a/windows/cache.c b/windows/cache.c index 313b16a..a7dd81e 100644 --- a/windows/cache.c +++ b/windows/cache.c @@ -2,7 +2,6 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <sys/stat.h> #include <sys/types.h> |