diff --git a/nfq/desync.c b/nfq/desync.c index 06196b7..f61d982 100644 --- a/nfq/desync.c +++ b/nfq/desync.c @@ -367,7 +367,8 @@ static void auto_hostlist_failed(struct desync_profile *dp, const char *hostname DLOG_PERROR("write to auto hostlist:"); return; } - dp->hostlist_auto->mod_time = file_mod_time(dp->hostlist_auto->filename); + if (!file_mod_signature(dp->hostlist_auto->filename, &dp->hostlist_auto->mod_sig)) + DLOG_PERROR("file_mod_signature"); } else { diff --git a/nfq/helpers.c b/nfq/helpers.c index aba018c..03f130c 100644 --- a/nfq/helpers.c +++ b/nfq/helpers.c @@ -300,6 +300,14 @@ time_t file_mod_time(const char *filename) struct stat st; return stat(filename,&st)==-1 ? 0 : st.st_mtime; } +bool file_mod_signature(const char *filename, file_mod_sig *ms) +{ + struct stat st; + if (stat(filename,&st)==-1) return false; + ms->mod_time=st.st_mtime; + ms->size=st.st_size; + return true; +} bool pf_in_range(uint16_t port, const port_filter *pf) { diff --git a/nfq/helpers.h b/nfq/helpers.h index 756bab6..5e8e80c 100644 --- a/nfq/helpers.h +++ b/nfq/helpers.h @@ -60,6 +60,14 @@ void fill_pattern(uint8_t *buf,size_t bufsize,const void *pattern,size_t patsize int fprint_localtime(FILE *F); +typedef struct +{ + time_t mod_time; + off_t size; +} file_mod_sig; +#define FILE_MOD_COMPARE(ms1,ms2) (((ms1)->mod_time==(ms2)->mod_time) && ((ms1)->size==(ms2)->size)) +#define FILE_MOD_RESET(ms) memset(ms,0,sizeof(file_mod_sig)) +bool file_mod_signature(const char *filename, file_mod_sig *ms); time_t file_mod_time(const char *filename); typedef struct diff --git a/nfq/hostlist.c b/nfq/hostlist.c index 9b75758..34e25cf 100644 --- a/nfq/hostlist.c +++ b/nfq/hostlist.c @@ -105,21 +105,21 @@ static bool LoadHostList(struct hostlist_file *hfile) { if (hfile->filename) { - time_t t = file_mod_time(hfile->filename); - if (!t) + file_mod_sig fsig; + if (!file_mod_signature(hfile->filename, &fsig)) { // stat() error DLOG_ERR("cannot access hostlist file '%s'. in-memory content remains unchanged.\n",hfile->filename); return true; } - if (t==hfile->mod_time) return true; // up to date + if (FILE_MOD_COMPARE(&hfile->mod_sig,&fsig)) return true; // up to date StrPoolDestroy(&hfile->hostlist); if (!AppendHostList(&hfile->hostlist, hfile->filename)) { StrPoolDestroy(&hfile->hostlist); return false; } - hfile->mod_time=t; + hfile->mod_sig=fsig; } return true; } diff --git a/nfq/ipset.c b/nfq/ipset.c index 86970b0..a4d1ebe 100644 --- a/nfq/ipset.c +++ b/nfq/ipset.c @@ -126,21 +126,21 @@ static bool LoadIpset(struct ipset_file *hfile) { if (hfile->filename) { - time_t t = file_mod_time(hfile->filename); - if (!t) + file_mod_sig fsig; + if (!file_mod_signature(hfile->filename, &fsig)) { // stat() error DLOG_ERR("cannot access ipset file '%s'. in-memory content remains unchanged.\n",hfile->filename); return true; } - if (t==hfile->mod_time) return true; // up to date + if (FILE_MOD_COMPARE(&hfile->mod_sig,&fsig)) return true; // up to date ipsetDestroy(&hfile->ipset); if (!AppendIpset(&hfile->ipset, hfile->filename)) { ipsetDestroy(&hfile->ipset); return false; } - hfile->mod_time=t; + hfile->mod_sig=fsig; } return true; } diff --git a/nfq/pools.c b/nfq/pools.c index ac9cb14..4cce2f6 100644 --- a/nfq/pools.c +++ b/nfq/pools.c @@ -169,7 +169,7 @@ struct hostlist_file *hostlist_files_add(struct hostlist_files_head *head, const } else entry->filename = NULL; - entry->mod_time = 0; + FILE_MOD_RESET(&entry->mod_sig); entry->hostlist = NULL; LIST_INSERT_HEAD(head, entry, next); } @@ -206,7 +206,7 @@ void hostlist_files_reset_modtime(struct hostlist_files_head *list) struct hostlist_file *hfile; LIST_FOREACH(hfile, list, next) - hfile->mod_time=0; + FILE_MOD_RESET(&hfile->mod_sig); } struct hostlist_item *hostlist_collection_add(struct hostlist_collection_head *head, struct hostlist_file *hfile) @@ -390,7 +390,7 @@ struct ipset_file *ipset_files_add(struct ipset_files_head *head, const char *fi } else entry->filename = NULL; - entry->mod_time = 0; + FILE_MOD_RESET(&entry->mod_sig); memset(&entry->ipset,0,sizeof(entry->ipset)); LIST_INSERT_HEAD(head, entry, next); } @@ -427,7 +427,7 @@ void ipset_files_reset_modtime(struct ipset_files_head *list) struct ipset_file *hfile; LIST_FOREACH(hfile, list, next) - hfile->mod_time=0; + FILE_MOD_RESET(&hfile->mod_sig); } struct ipset_item *ipset_collection_add(struct ipset_collection_head *head, struct ipset_file *hfile) diff --git a/nfq/pools.h b/nfq/pools.h index 41bf1d6..aeda63f 100644 --- a/nfq/pools.h +++ b/nfq/pools.h @@ -50,7 +50,7 @@ void strlist_destroy(struct str_list_head *head); struct hostlist_file { char *filename; - time_t mod_time; + file_mod_sig mod_sig; strpool *hostlist; LIST_ENTRY(hostlist_file) next; }; @@ -112,7 +112,7 @@ void ipsetPrint(ipset *ipset); struct ipset_file { char *filename; - time_t mod_time; + file_mod_sig mod_sig; ipset ipset; LIST_ENTRY(ipset_file) next; }; diff --git a/tpws/helpers.c b/tpws/helpers.c index 8d71403..b4f4ba3 100644 --- a/tpws/helpers.c +++ b/tpws/helpers.c @@ -314,6 +314,14 @@ time_t file_mod_time(const char *filename) struct stat st; return stat(filename, &st) == -1 ? 0 : st.st_mtime; } +bool file_mod_signature(const char *filename, file_mod_sig *ms) +{ + struct stat st; + if (stat(filename,&st)==-1) return false; + ms->mod_time=st.st_mtime; + ms->size=st.st_size; + return true; +} bool pf_in_range(uint16_t port, const port_filter *pf) { diff --git a/tpws/helpers.h b/tpws/helpers.h index 3c8a372..1b74430 100644 --- a/tpws/helpers.h +++ b/tpws/helpers.h @@ -62,6 +62,14 @@ static inline void phton16(uint8_t *p, uint16_t v) { int fprint_localtime(FILE *F); +typedef struct +{ + time_t mod_time; + off_t size; +} file_mod_sig; +#define FILE_MOD_COMPARE(ms1,ms2) (((ms1)->mod_time==(ms2)->mod_time) && ((ms1)->size==(ms2)->size)) +#define FILE_MOD_RESET(ms) memset(ms,0,sizeof(file_mod_sig)) +bool file_mod_signature(const char *filename, file_mod_sig *ms); time_t file_mod_time(const char *filename); typedef struct diff --git a/tpws/hostlist.c b/tpws/hostlist.c index 6fbbd7d..c10aca6 100644 --- a/tpws/hostlist.c +++ b/tpws/hostlist.c @@ -105,21 +105,21 @@ static bool LoadHostList(struct hostlist_file *hfile) { if (hfile->filename) { - time_t t = file_mod_time(hfile->filename); - if (!t) + file_mod_sig fsig; + if (!file_mod_signature(hfile->filename, &fsig)) { // stat() error DLOG_ERR("cannot access hostlist file '%s'. in-memory content remains unchanged.\n",hfile->filename); return true; } - if (t==hfile->mod_time) return true; // up to date + if (FILE_MOD_COMPARE(&hfile->mod_sig,&fsig)) return true; // up to date StrPoolDestroy(&hfile->hostlist); if (!AppendHostList(&hfile->hostlist, hfile->filename)) { StrPoolDestroy(&hfile->hostlist); return false; } - hfile->mod_time=t; + hfile->mod_sig=fsig; } return true; } diff --git a/tpws/ipset.c b/tpws/ipset.c index b803205..b486eb4 100644 --- a/tpws/ipset.c +++ b/tpws/ipset.c @@ -126,21 +126,21 @@ static bool LoadIpset(struct ipset_file *hfile) { if (hfile->filename) { - time_t t = file_mod_time(hfile->filename); - if (!t) + file_mod_sig fsig; + if (!file_mod_signature(hfile->filename, &fsig)) { // stat() error DLOG_ERR("cannot access ipset file '%s'. in-memory content remains unchanged.\n",hfile->filename); return true; } - if (t==hfile->mod_time) return true; // up to date + if (FILE_MOD_COMPARE(&hfile->mod_sig,&fsig)) return true; // up to date ipsetDestroy(&hfile->ipset); if (!AppendIpset(&hfile->ipset, hfile->filename)) { ipsetDestroy(&hfile->ipset); return false; } - hfile->mod_time=t; + hfile->mod_sig=fsig; } return true; } diff --git a/tpws/pools.c b/tpws/pools.c index ac9cb14..4cce2f6 100644 --- a/tpws/pools.c +++ b/tpws/pools.c @@ -169,7 +169,7 @@ struct hostlist_file *hostlist_files_add(struct hostlist_files_head *head, const } else entry->filename = NULL; - entry->mod_time = 0; + FILE_MOD_RESET(&entry->mod_sig); entry->hostlist = NULL; LIST_INSERT_HEAD(head, entry, next); } @@ -206,7 +206,7 @@ void hostlist_files_reset_modtime(struct hostlist_files_head *list) struct hostlist_file *hfile; LIST_FOREACH(hfile, list, next) - hfile->mod_time=0; + FILE_MOD_RESET(&hfile->mod_sig); } struct hostlist_item *hostlist_collection_add(struct hostlist_collection_head *head, struct hostlist_file *hfile) @@ -390,7 +390,7 @@ struct ipset_file *ipset_files_add(struct ipset_files_head *head, const char *fi } else entry->filename = NULL; - entry->mod_time = 0; + FILE_MOD_RESET(&entry->mod_sig); memset(&entry->ipset,0,sizeof(entry->ipset)); LIST_INSERT_HEAD(head, entry, next); } @@ -427,7 +427,7 @@ void ipset_files_reset_modtime(struct ipset_files_head *list) struct ipset_file *hfile; LIST_FOREACH(hfile, list, next) - hfile->mod_time=0; + FILE_MOD_RESET(&hfile->mod_sig); } struct ipset_item *ipset_collection_add(struct ipset_collection_head *head, struct ipset_file *hfile) diff --git a/tpws/pools.h b/tpws/pools.h index 41bf1d6..aeda63f 100644 --- a/tpws/pools.h +++ b/tpws/pools.h @@ -50,7 +50,7 @@ void strlist_destroy(struct str_list_head *head); struct hostlist_file { char *filename; - time_t mod_time; + file_mod_sig mod_sig; strpool *hostlist; LIST_ENTRY(hostlist_file) next; }; @@ -112,7 +112,7 @@ void ipsetPrint(ipset *ipset); struct ipset_file { char *filename; - time_t mod_time; + file_mod_sig mod_sig; ipset ipset; LIST_ENTRY(ipset_file) next; }; diff --git a/tpws/tamper.c b/tpws/tamper.c index 7492728..c06613e 100644 --- a/tpws/tamper.c +++ b/tpws/tamper.c @@ -443,7 +443,8 @@ static void auto_hostlist_failed(struct desync_profile *dp, const char *hostname DLOG_PERROR("write to auto hostlist:"); return; } - dp->hostlist_auto->mod_time = file_mod_time(dp->hostlist_auto->filename); + if (!file_mod_signature(dp->hostlist_auto->filename, &dp->hostlist_auto->mod_sig)) + DLOG_PERROR("file_mod_signature"); } else {