tpws,nfqws: track file size in addition to mod time

This commit is contained in:
bol-van 2025-01-24 12:50:17 +03:00
parent 02c76a4fb6
commit aba1fdeb04
14 changed files with 64 additions and 30 deletions

View File

@ -367,7 +367,8 @@ static void auto_hostlist_failed(struct desync_profile *dp, const char *hostname
DLOG_PERROR("write to auto hostlist:"); DLOG_PERROR("write to auto hostlist:");
return; 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 else
{ {

View File

@ -300,6 +300,14 @@ time_t file_mod_time(const char *filename)
struct stat st; struct stat st;
return stat(filename,&st)==-1 ? 0 : st.st_mtime; 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) bool pf_in_range(uint16_t port, const port_filter *pf)
{ {

View File

@ -60,6 +60,14 @@ void fill_pattern(uint8_t *buf,size_t bufsize,const void *pattern,size_t patsize
int fprint_localtime(FILE *F); 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); time_t file_mod_time(const char *filename);
typedef struct typedef struct

View File

@ -105,21 +105,21 @@ static bool LoadHostList(struct hostlist_file *hfile)
{ {
if (hfile->filename) if (hfile->filename)
{ {
time_t t = file_mod_time(hfile->filename); file_mod_sig fsig;
if (!t) if (!file_mod_signature(hfile->filename, &fsig))
{ {
// stat() error // stat() error
DLOG_ERR("cannot access hostlist file '%s'. in-memory content remains unchanged.\n",hfile->filename); DLOG_ERR("cannot access hostlist file '%s'. in-memory content remains unchanged.\n",hfile->filename);
return true; 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); StrPoolDestroy(&hfile->hostlist);
if (!AppendHostList(&hfile->hostlist, hfile->filename)) if (!AppendHostList(&hfile->hostlist, hfile->filename))
{ {
StrPoolDestroy(&hfile->hostlist); StrPoolDestroy(&hfile->hostlist);
return false; return false;
} }
hfile->mod_time=t; hfile->mod_sig=fsig;
} }
return true; return true;
} }

View File

@ -126,21 +126,21 @@ static bool LoadIpset(struct ipset_file *hfile)
{ {
if (hfile->filename) if (hfile->filename)
{ {
time_t t = file_mod_time(hfile->filename); file_mod_sig fsig;
if (!t) if (!file_mod_signature(hfile->filename, &fsig))
{ {
// stat() error // stat() error
DLOG_ERR("cannot access ipset file '%s'. in-memory content remains unchanged.\n",hfile->filename); DLOG_ERR("cannot access ipset file '%s'. in-memory content remains unchanged.\n",hfile->filename);
return true; 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); ipsetDestroy(&hfile->ipset);
if (!AppendIpset(&hfile->ipset, hfile->filename)) if (!AppendIpset(&hfile->ipset, hfile->filename))
{ {
ipsetDestroy(&hfile->ipset); ipsetDestroy(&hfile->ipset);
return false; return false;
} }
hfile->mod_time=t; hfile->mod_sig=fsig;
} }
return true; return true;
} }

View File

@ -169,7 +169,7 @@ struct hostlist_file *hostlist_files_add(struct hostlist_files_head *head, const
} }
else else
entry->filename = NULL; entry->filename = NULL;
entry->mod_time = 0; FILE_MOD_RESET(&entry->mod_sig);
entry->hostlist = NULL; entry->hostlist = NULL;
LIST_INSERT_HEAD(head, entry, next); LIST_INSERT_HEAD(head, entry, next);
} }
@ -206,7 +206,7 @@ void hostlist_files_reset_modtime(struct hostlist_files_head *list)
struct hostlist_file *hfile; struct hostlist_file *hfile;
LIST_FOREACH(hfile, list, next) 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) 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 else
entry->filename = NULL; entry->filename = NULL;
entry->mod_time = 0; FILE_MOD_RESET(&entry->mod_sig);
memset(&entry->ipset,0,sizeof(entry->ipset)); memset(&entry->ipset,0,sizeof(entry->ipset));
LIST_INSERT_HEAD(head, entry, next); LIST_INSERT_HEAD(head, entry, next);
} }
@ -427,7 +427,7 @@ void ipset_files_reset_modtime(struct ipset_files_head *list)
struct ipset_file *hfile; struct ipset_file *hfile;
LIST_FOREACH(hfile, list, next) 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) struct ipset_item *ipset_collection_add(struct ipset_collection_head *head, struct ipset_file *hfile)

View File

@ -50,7 +50,7 @@ void strlist_destroy(struct str_list_head *head);
struct hostlist_file { struct hostlist_file {
char *filename; char *filename;
time_t mod_time; file_mod_sig mod_sig;
strpool *hostlist; strpool *hostlist;
LIST_ENTRY(hostlist_file) next; LIST_ENTRY(hostlist_file) next;
}; };
@ -112,7 +112,7 @@ void ipsetPrint(ipset *ipset);
struct ipset_file { struct ipset_file {
char *filename; char *filename;
time_t mod_time; file_mod_sig mod_sig;
ipset ipset; ipset ipset;
LIST_ENTRY(ipset_file) next; LIST_ENTRY(ipset_file) next;
}; };

View File

@ -314,6 +314,14 @@ time_t file_mod_time(const char *filename)
struct stat st; struct stat st;
return stat(filename, &st) == -1 ? 0 : st.st_mtime; 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) bool pf_in_range(uint16_t port, const port_filter *pf)
{ {

View File

@ -62,6 +62,14 @@ static inline void phton16(uint8_t *p, uint16_t v) {
int fprint_localtime(FILE *F); 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); time_t file_mod_time(const char *filename);
typedef struct typedef struct

View File

@ -105,21 +105,21 @@ static bool LoadHostList(struct hostlist_file *hfile)
{ {
if (hfile->filename) if (hfile->filename)
{ {
time_t t = file_mod_time(hfile->filename); file_mod_sig fsig;
if (!t) if (!file_mod_signature(hfile->filename, &fsig))
{ {
// stat() error // stat() error
DLOG_ERR("cannot access hostlist file '%s'. in-memory content remains unchanged.\n",hfile->filename); DLOG_ERR("cannot access hostlist file '%s'. in-memory content remains unchanged.\n",hfile->filename);
return true; 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); StrPoolDestroy(&hfile->hostlist);
if (!AppendHostList(&hfile->hostlist, hfile->filename)) if (!AppendHostList(&hfile->hostlist, hfile->filename))
{ {
StrPoolDestroy(&hfile->hostlist); StrPoolDestroy(&hfile->hostlist);
return false; return false;
} }
hfile->mod_time=t; hfile->mod_sig=fsig;
} }
return true; return true;
} }

View File

@ -126,21 +126,21 @@ static bool LoadIpset(struct ipset_file *hfile)
{ {
if (hfile->filename) if (hfile->filename)
{ {
time_t t = file_mod_time(hfile->filename); file_mod_sig fsig;
if (!t) if (!file_mod_signature(hfile->filename, &fsig))
{ {
// stat() error // stat() error
DLOG_ERR("cannot access ipset file '%s'. in-memory content remains unchanged.\n",hfile->filename); DLOG_ERR("cannot access ipset file '%s'. in-memory content remains unchanged.\n",hfile->filename);
return true; 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); ipsetDestroy(&hfile->ipset);
if (!AppendIpset(&hfile->ipset, hfile->filename)) if (!AppendIpset(&hfile->ipset, hfile->filename))
{ {
ipsetDestroy(&hfile->ipset); ipsetDestroy(&hfile->ipset);
return false; return false;
} }
hfile->mod_time=t; hfile->mod_sig=fsig;
} }
return true; return true;
} }

View File

@ -169,7 +169,7 @@ struct hostlist_file *hostlist_files_add(struct hostlist_files_head *head, const
} }
else else
entry->filename = NULL; entry->filename = NULL;
entry->mod_time = 0; FILE_MOD_RESET(&entry->mod_sig);
entry->hostlist = NULL; entry->hostlist = NULL;
LIST_INSERT_HEAD(head, entry, next); LIST_INSERT_HEAD(head, entry, next);
} }
@ -206,7 +206,7 @@ void hostlist_files_reset_modtime(struct hostlist_files_head *list)
struct hostlist_file *hfile; struct hostlist_file *hfile;
LIST_FOREACH(hfile, list, next) 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) 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 else
entry->filename = NULL; entry->filename = NULL;
entry->mod_time = 0; FILE_MOD_RESET(&entry->mod_sig);
memset(&entry->ipset,0,sizeof(entry->ipset)); memset(&entry->ipset,0,sizeof(entry->ipset));
LIST_INSERT_HEAD(head, entry, next); LIST_INSERT_HEAD(head, entry, next);
} }
@ -427,7 +427,7 @@ void ipset_files_reset_modtime(struct ipset_files_head *list)
struct ipset_file *hfile; struct ipset_file *hfile;
LIST_FOREACH(hfile, list, next) 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) struct ipset_item *ipset_collection_add(struct ipset_collection_head *head, struct ipset_file *hfile)

View File

@ -50,7 +50,7 @@ void strlist_destroy(struct str_list_head *head);
struct hostlist_file { struct hostlist_file {
char *filename; char *filename;
time_t mod_time; file_mod_sig mod_sig;
strpool *hostlist; strpool *hostlist;
LIST_ENTRY(hostlist_file) next; LIST_ENTRY(hostlist_file) next;
}; };
@ -112,7 +112,7 @@ void ipsetPrint(ipset *ipset);
struct ipset_file { struct ipset_file {
char *filename; char *filename;
time_t mod_time; file_mod_sig mod_sig;
ipset ipset; ipset ipset;
LIST_ENTRY(ipset_file) next; LIST_ENTRY(ipset_file) next;
}; };

View File

@ -443,7 +443,8 @@ static void auto_hostlist_failed(struct desync_profile *dp, const char *hostname
DLOG_PERROR("write to auto hostlist:"); DLOG_PERROR("write to auto hostlist:");
return; 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 else
{ {