aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2009-11-12 17:21:06 +0300
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2009-11-12 17:21:06 +0300
commita447a10790c37b862d5673a53ef63c0b7239ef73 (patch)
tree348f91a5efd61a008af1d432479293916bfcf243
parentfe5bfee09fe3d28839955b781cbb9adad24ec828 (diff)
downloadrspamd-a447a10790c37b862d5673a53ef63c0b7239ef73.tar.gz
rspamd-a447a10790c37b862d5673a53ef63c0b7239ef73.zip
* Fix some issues in binlog seeking
-rw-r--r--src/binlog.c78
-rw-r--r--src/binlog.h3
2 files changed, 30 insertions, 51 deletions
diff --git a/src/binlog.c b/src/binlog.c
index 0e92b09ca..c42e6b2af 100644
--- a/src/binlog.c
+++ b/src/binlog.c
@@ -58,25 +58,16 @@ binlog_write_header (struct rspamd_binlog *log)
/* Metaindex */
log->metaindex = g_malloc (sizeof (struct rspamd_binlog_metaindex));
bzero (log->metaindex, sizeof (struct rspamd_binlog_metaindex));
+ /* Offset to metaindex */
+ log->metaindex->indexes[0] = sizeof (struct rspamd_binlog_metaindex) + sizeof (struct rspamd_binlog_header);
+
if (write (log->fd, log->metaindex, sizeof (struct rspamd_binlog_metaindex)) == -1) {
g_free (log->metaindex);
msg_warn ("binlog_write_header: cannot write file %s, error %d, %s", log->filename, errno, strerror (errno));
unlock_file (log->fd, FALSE);
return FALSE;
}
- g_free (log->metaindex);
- /* Now mmap metaindex to memory */
- if ((log->metaindex = mmap (NULL, sizeof (struct rspamd_binlog_metaindex),
- PROT_READ | PROT_WRITE, MAP_SHARED, log->fd, sizeof (struct rspamd_binlog_header))) == MAP_FAILED) {
- msg_warn ("binlog_write_header: cannot mmap file %s, error %d, %s", log->filename, errno, strerror (errno));
- unlock_file (log->fd, FALSE);
- return FALSE;
- }
- /* First index block */
-
- /* Offset to metaindex */
- log->metaindex->indexes[0] = sizeof (struct rspamd_binlog_metaindex) + sizeof (struct rspamd_binlog_header);
/* Alloc, write, mmap */
log->cur_idx = g_malloc (sizeof (struct rspamd_index_block));
bzero (log->cur_idx, sizeof (struct rspamd_index_block));
@@ -239,47 +230,35 @@ write_binlog_tree (struct rspamd_binlog *log, GTree *nodes)
struct rspamd_binlog_index *idx;
lock_file (log->fd, FALSE);
- /* Write index */
- log->cur_idx->last_index ++;
- if (lseek (log->fd, log->metaindex->indexes[log->metaindex->last_index] + G_STRUCT_OFFSET (struct rspamd_index_block, last_index), SEEK_SET) == -1) {
+ log->cur_seq ++;
+
+ /* Seek to end of file */
+ if ((seek = lseek (log->fd, 0, SEEK_END)) == -1) {
unlock_file (log->fd, FALSE);
msg_info ("binlog_insert: cannot seek in file: %s, error: %s", log->filename, strerror (errno));
return FALSE;
}
- if (write (log->fd, &log->cur_idx->last_index, sizeof (log->cur_idx->last_index)) == -1) {
- unlock_file (log->fd, FALSE);
- msg_info ("binlog_insert: cannot write index to file: %s, error: %s", log->filename, strerror (errno));
- return FALSE;
- }
-
- log->cur_seq ++;
+ /* Now write all nodes to file */
+ g_tree_foreach (nodes, binlog_tree_callback, (gpointer)log);
+
+ /* Write index */
idx = &log->cur_idx->indexes[log->cur_idx->last_index];
idx->seek = seek;
idx->time = (uint64_t)time (NULL);
idx->len = g_tree_nnodes (nodes) * sizeof (struct rspamd_binlog_element);
- if (lseek (log->fd, log->metaindex->indexes[log->metaindex->last_index] +
- log->cur_idx->last_index * sizeof (struct rspamd_binlog_index), SEEK_SET) == -1) {
+ if (lseek (log->fd, log->metaindex->indexes[log->metaindex->last_index], SEEK_SET) == -1) {
unlock_file (log->fd, FALSE);
- msg_info ("binlog_insert: cannot seek in file: %s, error: %s", log->filename, strerror (errno));
+ msg_info ("binlog_insert: cannot seek in file: %s, error: %s, seek: %ld, op: insert index", log->filename,
+ strerror (errno), log->metaindex->indexes[log->metaindex->last_index]);
return FALSE;
}
- if (write (log->fd, idx, sizeof (struct rspamd_binlog_index)) == -1) {
+ if (write (log->fd, log->cur_idx, sizeof (struct rspamd_index_block)) == -1) {
unlock_file (log->fd, FALSE);
msg_info ("binlog_insert: cannot write index to file: %s, error: %s", log->filename, strerror (errno));
return FALSE;
}
-
- /* Seek to end of file */
- if ((seek = lseek (log->fd, 0, SEEK_END)) == -1) {
- unlock_file (log->fd, FALSE);
- msg_info ("binlog_insert: cannot seek in file: %s, error: %s", log->filename, strerror (errno));
- return FALSE;
- }
-
- /* Now write all nodes to file */
- g_tree_foreach (nodes, binlog_tree_callback, (gpointer)log);
-
+ log->cur_idx->last_index ++;
unlock_file (log->fd, FALSE);
return TRUE;
@@ -289,37 +268,36 @@ static gboolean
create_new_metaindex_block (struct rspamd_binlog *log)
{
off_t seek;
-
lock_file (log->fd, FALSE);
log->metaindex->last_index ++;
- /* Offset to metaindex */
- log->metaindex->indexes[log->metaindex->last_index] = seek;
- /* Overwrite all metaindexes */
- if (lseek (log->fd, sizeof (struct rspamd_binlog_header), SEEK_SET) == -1) {
+ /* Seek to end of file */
+ if ((seek = lseek (log->fd, 0, SEEK_END)) == -1) {
unlock_file (log->fd, FALSE);
msg_info ("create_new_metaindex_block: cannot seek in file: %s, error: %s", log->filename, strerror (errno));
return FALSE;
}
- if (write (log->fd, log->metaindex, sizeof (struct rspamd_binlog_metaindex)) == -1) {
+ if (write (log->fd, log->cur_idx, sizeof (struct rspamd_index_block)) == -1) {
unlock_file (log->fd, FALSE);
- msg_info ("create_new_metaindex_block: cannot write metaindex in file: %s, error: %s", log->filename, strerror (errno));
+ g_free (log->cur_idx);
+ msg_warn ("create_new_metaindex_block: cannot write file %s, error %d, %s", log->filename, errno, strerror (errno));
return FALSE;
}
- bzero (log->cur_idx, sizeof (struct rspamd_index_block));
- /* Seek to end of file */
- if ((seek = lseek (log->fd, 0, SEEK_END)) == -1) {
+ /* Offset to metaindex */
+ log->metaindex->indexes[log->metaindex->last_index] = seek;
+ /* Overwrite all metaindexes */
+ if (lseek (log->fd, sizeof (struct rspamd_binlog_header), SEEK_SET) == -1) {
unlock_file (log->fd, FALSE);
msg_info ("create_new_metaindex_block: cannot seek in file: %s, error: %s", log->filename, strerror (errno));
return FALSE;
}
- if (write (log->fd, log->cur_idx, sizeof (struct rspamd_index_block)) == -1) {
+ if (write (log->fd, log->metaindex, sizeof (struct rspamd_binlog_metaindex)) == -1) {
unlock_file (log->fd, FALSE);
- g_free (log->cur_idx);
- msg_warn ("create_new_metaindex_block: cannot write file %s, error %d, %s", log->filename, errno, strerror (errno));
+ msg_info ("create_new_metaindex_block: cannot write metaindex in file: %s, error: %s", log->filename, strerror (errno));
return FALSE;
}
+ bzero (log->cur_idx, sizeof (struct rspamd_index_block));
unlock_file (log->fd, FALSE);
return TRUE;
diff --git a/src/binlog.h b/src/binlog.h
index eef5f4d19..84fba3db6 100644
--- a/src/binlog.h
+++ b/src/binlog.h
@@ -9,12 +9,13 @@
#define BINLOG_IDX_LEN 200
#define METAINDEX_LEN 1024
+/* Assume 8 bytes words */
struct rspamd_binlog_header {
char magic[3];
char version[2];
char padding[3];
uint64_t create_time;
-} __attribute__((__packed__));
+};
struct rspamd_binlog_index {
uint64_t time;