just use a real checksum

This commit is contained in:
ailin-nemui 2018-02-03 22:08:16 +01:00
commit 8a428d2df2

View file

@ -29,6 +29,7 @@
#include "settings.h" #include "settings.h"
#include "default-config.h" #include "default-config.h"
#include <openssl/sha.h>
#include <signal.h> #include <signal.h>
#define SETTINGS_AUTOSAVE_TIMEOUT (1000*60*60) /* 1 hour */ #define SETTINGS_AUTOSAVE_TIMEOUT (1000*60*60) /* 1 hour */
@ -47,7 +48,7 @@ static int timeout_tag;
static int config_last_modifycounter; static int config_last_modifycounter;
static time_t config_last_mtime; static time_t config_last_mtime;
static long config_last_size; static long config_last_size;
static unsigned int config_last_checksum; static unsigned char config_last_checksum[SHA_DIGEST_LENGTH];
static SETTINGS_REC *settings_get(const char *key, SettingType type) static SETTINGS_REC *settings_get(const char *key, SettingType type)
{ {
@ -661,24 +662,36 @@ void sig_term(int n)
raise(SIGTERM); raise(SIGTERM);
} }
/* Yes, this is my own stupid checksum generator, some "real" algorithm static int file_checksum(const char *fname, const unsigned char *old_checksum,
would be nice but would just take more space without much real benefit */ unsigned char *checksum)
static unsigned int file_checksum(const char *fname)
{ {
unsigned char buf[512]; SHA_CTX c;
int f, ret, n; unsigned char buf[512], tmp[SHA_DIGEST_LENGTH];
unsigned int checksum = 0; int f, ret;
if (!SHA1_Init(&c)) {
return FALSE;
}
f = open(fname, O_RDONLY); f = open(fname, O_RDONLY);
if (f == -1) return 0; if (f == -1)
return FALSE;
n = 0;
while ((ret = read(f, buf, sizeof(buf))) > 0) { while ((ret = read(f, buf, sizeof(buf))) > 0) {
while (ret-- > 0) SHA1_Update(&c, buf, ret);
checksum += buf[ret] << ((n++ & 3)*8);
} }
close(f); close(f);
return checksum;
if (checksum == NULL) {
checksum = tmp;
}
SHA1_Final(checksum, &c);
if (old_checksum != NULL) {
return memcmp(checksum, old_checksum, SHA_DIGEST_LENGTH) == 0;
}
return TRUE;
} }
static void irssi_config_save_state(const char *fname) static void irssi_config_save_state(const char *fname)
@ -693,7 +706,7 @@ static void irssi_config_save_state(const char *fname)
/* save modify time, file size and checksum */ /* save modify time, file size and checksum */
config_last_mtime = statbuf.st_mtime; config_last_mtime = statbuf.st_mtime;
config_last_size = statbuf.st_size; config_last_size = statbuf.st_size;
config_last_checksum = file_checksum(fname); file_checksum(fname, NULL, config_last_checksum);
} }
int irssi_config_is_changed(const char *fname) int irssi_config_is_changed(const char *fname)
@ -707,8 +720,8 @@ int irssi_config_is_changed(const char *fname)
return FALSE; return FALSE;
return config_last_mtime != statbuf.st_mtime && return config_last_mtime != statbuf.st_mtime &&
(config_last_size != statbuf.st_size || (config_last_size != statbuf.st_size ||
config_last_checksum != file_checksum(fname)); file_checksum(fname, config_last_checksum, NULL));
} }
static CONFIG_REC *parse_configfile(const char *fname) static CONFIG_REC *parse_configfile(const char *fname)