mirror of
https://github.com/irssi/irssi.git
synced 2026-08-22 18:12:12 +02:00
implement with glib
This commit is contained in:
parent
8a428d2df2
commit
9c3532f542
1 changed files with 29 additions and 15 deletions
|
|
@ -29,7 +29,6 @@
|
|||
#include "settings.h"
|
||||
#include "default-config.h"
|
||||
|
||||
#include <openssl/sha.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define SETTINGS_AUTOSAVE_TIMEOUT (1000*60*60) /* 1 hour */
|
||||
|
|
@ -48,7 +47,8 @@ static int timeout_tag;
|
|||
static int config_last_modifycounter;
|
||||
static time_t config_last_mtime;
|
||||
static long config_last_size;
|
||||
static unsigned char config_last_checksum[SHA_DIGEST_LENGTH];
|
||||
static guint8 *config_last_checksum;
|
||||
static gsize config_last_checksum_length;
|
||||
|
||||
static SETTINGS_REC *settings_get(const char *key, SettingType type)
|
||||
{
|
||||
|
|
@ -662,14 +662,17 @@ void sig_term(int n)
|
|||
raise(SIGTERM);
|
||||
}
|
||||
|
||||
static int file_checksum(const char *fname, const unsigned char *old_checksum,
|
||||
unsigned char *checksum)
|
||||
static int file_checksum(const char *fname, const guint8 *old_checksum, gsize old_checksum_length,
|
||||
guint8 **checksum_out, gsize *checksum_length_out)
|
||||
{
|
||||
SHA_CTX c;
|
||||
unsigned char buf[512], tmp[SHA_DIGEST_LENGTH];
|
||||
GChecksum *c;
|
||||
unsigned char buf[512];
|
||||
guint8 *checksum;
|
||||
gsize checksum_length;
|
||||
int f, ret;
|
||||
static GChecksumType checksum_type = G_CHECKSUM_SHA1;
|
||||
|
||||
if (!SHA1_Init(&c)) {
|
||||
if ((c = g_checksum_new(checksum_type)) == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
|
@ -678,18 +681,23 @@ static int file_checksum(const char *fname, const unsigned char *old_checksum,
|
|||
return FALSE;
|
||||
|
||||
while ((ret = read(f, buf, sizeof(buf))) > 0) {
|
||||
SHA1_Update(&c, buf, ret);
|
||||
g_checksum_update(c, buf, ret);
|
||||
}
|
||||
close(f);
|
||||
|
||||
if (checksum == NULL) {
|
||||
checksum = tmp;
|
||||
checksum = g_new0(guint8, g_checksum_type_get_length(checksum_type));
|
||||
g_checksum_get_digest(c, checksum, &checksum_length);
|
||||
g_checksum_free(c);
|
||||
|
||||
if (checksum_out != NULL && checksum_length_out != NULL) {
|
||||
g_free(*checksum_out);
|
||||
*checksum_out = checksum;
|
||||
*checksum_length_out = checksum_length;
|
||||
}
|
||||
|
||||
SHA1_Final(checksum, &c);
|
||||
|
||||
if (old_checksum != NULL) {
|
||||
return memcmp(checksum, old_checksum, SHA_DIGEST_LENGTH) == 0;
|
||||
return old_checksum_length == checksum_length &&
|
||||
memcmp(checksum, old_checksum, checksum_length) == 0;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
|
@ -706,7 +714,7 @@ static void irssi_config_save_state(const char *fname)
|
|||
/* save modify time, file size and checksum */
|
||||
config_last_mtime = statbuf.st_mtime;
|
||||
config_last_size = statbuf.st_size;
|
||||
file_checksum(fname, NULL, config_last_checksum);
|
||||
file_checksum(fname, NULL, 0, &config_last_checksum, &config_last_checksum_length);
|
||||
}
|
||||
|
||||
int irssi_config_is_changed(const char *fname)
|
||||
|
|
@ -721,7 +729,7 @@ int irssi_config_is_changed(const char *fname)
|
|||
|
||||
return config_last_mtime != statbuf.st_mtime &&
|
||||
(config_last_size != statbuf.st_size ||
|
||||
file_checksum(fname, config_last_checksum, NULL));
|
||||
file_checksum(fname, config_last_checksum, config_last_checksum_length, NULL, NULL));
|
||||
}
|
||||
|
||||
static CONFIG_REC *parse_configfile(const char *fname)
|
||||
|
|
@ -919,4 +927,10 @@ void settings_deinit(void)
|
|||
g_hash_table_destroy(settings);
|
||||
|
||||
if (mainconfig != NULL) config_close(mainconfig);
|
||||
|
||||
if (config_last_checksum != NULL) {
|
||||
g_free(config_last_checksum);
|
||||
config_last_checksum = NULL;
|
||||
config_last_checksum_length = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue