Skip to content

Commit

Permalink
lib-index: mail_transaction_log_file_get_highest_modseq_at() - return…
Browse files Browse the repository at this point in the history
… error string

This can help figuring out why some error happened when more context is
provided in the caller's error messages.
  • Loading branch information
sirainen authored and GitLab committed May 20, 2017
1 parent 20a1069 commit a0c8af5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/lib-index/mail-transaction-log-file.c
Expand Up @@ -1171,7 +1171,8 @@ modseq_cache_get_modseq(struct mail_transaction_log_file *file, uint64_t modseq)

static int
log_get_synced_record(struct mail_transaction_log_file *file, uoff_t *offset,
const struct mail_transaction_header **hdr_r)
const struct mail_transaction_header **hdr_r,
const char **error_r)
{
const struct mail_transaction_header *hdr;
uint32_t trans_size;
Expand All @@ -1184,10 +1185,11 @@ log_get_synced_record(struct mail_transaction_log_file *file, uoff_t *offset,
trans_size = mail_index_offset_to_uint32(hdr->size);
if (trans_size < sizeof(*hdr) ||
*offset - file->buffer_offset + trans_size > file->buffer->used) {
mail_transaction_log_file_set_corrupted(file,
*error_r = t_strdup_printf(
"Transaction log corrupted unexpectedly at "
"%"PRIuUOFF_T": Invalid size %u (type=%x)",
*offset, trans_size, hdr->type);
mail_transaction_log_file_set_corrupted(file, "%s", *error_r);
return -1;
}
*offset += trans_size;
Expand All @@ -1197,7 +1199,8 @@ log_get_synced_record(struct mail_transaction_log_file *file, uoff_t *offset,

int mail_transaction_log_file_get_highest_modseq_at(
struct mail_transaction_log_file *file,
uoff_t offset, uint64_t *highest_modseq_r)
uoff_t offset, uint64_t *highest_modseq_r,
const char **error_r)
{
const struct mail_transaction_header *hdr;
struct modseq_cache *cache;
Expand Down Expand Up @@ -1230,7 +1233,7 @@ int mail_transaction_log_file_get_highest_modseq_at(

ret = mail_transaction_log_file_map(file, cur_offset, offset, &reason);
if (ret <= 0) {
mail_index_set_error(file->log->index,
*error_r = t_strdup_printf(
"Failed to map transaction log %s for getting modseq "
"at offset=%"PRIuUOFF_T" with start_offset=%"PRIuUOFF_T": %s",
file->filepath, offset, cur_offset, reason);
Expand All @@ -1240,7 +1243,7 @@ int mail_transaction_log_file_get_highest_modseq_at(
i_assert(cur_offset >= file->buffer_offset);
i_assert(cur_offset + file->buffer->used >= offset);
while (cur_offset < offset) {
if (log_get_synced_record(file, &cur_offset, &hdr) < 0)
if (log_get_synced_record(file, &cur_offset, &hdr, error_r) < 0)
return- 1;
mail_transaction_update_modseq(hdr, hdr + 1, &cur_modseq,
MAIL_TRANSACTION_LOG_HDR_VERSION(&file->hdr));
Expand Down Expand Up @@ -1313,8 +1316,11 @@ int mail_transaction_log_file_get_modseq_next_offset(

i_assert(cur_offset >= file->buffer_offset);
while (cur_offset < file->sync_offset) {
if (log_get_synced_record(file, &cur_offset, &hdr) < 0)
if (log_get_synced_record(file, &cur_offset, &hdr, &reason) < 0) {
mail_index_set_error(file->log->index,
"%s: %s", file->filepath, reason);
return -1;
}
mail_transaction_update_modseq(hdr, hdr + 1, &cur_modseq,
MAIL_TRANSACTION_LOG_HDR_VERSION(&file->hdr));
if (cur_modseq >= modseq)
Expand Down
3 changes: 2 additions & 1 deletion src/lib-index/mail-transaction-log-private.h
Expand Up @@ -153,7 +153,8 @@ void mail_transaction_update_modseq(const struct mail_transaction_header *hdr,
unsigned int version);
int mail_transaction_log_file_get_highest_modseq_at(
struct mail_transaction_log_file *file,
uoff_t offset, uint64_t *highest_modseq_r);
uoff_t offset, uint64_t *highest_modseq_r,
const char **error_r);
int mail_transaction_log_file_get_modseq_next_offset(
struct mail_transaction_log_file *file,
uint64_t modseq, uoff_t *next_offset_r);
Expand Down
10 changes: 6 additions & 4 deletions src/lib-index/mail-transaction-log-view.c
Expand Up @@ -336,10 +336,8 @@ int mail_transaction_log_view_set(struct mail_transaction_log_view *view,
view->broken = FALSE;

if (mail_transaction_log_file_get_highest_modseq_at(view->cur,
view->cur_offset, &view->prev_modseq) < 0) {
*reason_r = "Failed to get modseq";
view->cur_offset, &view->prev_modseq, reason_r) < 0)
return -1;
}

i_assert(view->cur_offset <= view->cur->sync_offset);
return 1;
Expand Down Expand Up @@ -401,8 +399,12 @@ int mail_transaction_log_view_set_all(struct mail_transaction_log_view *view)
view->broken = FALSE;

if (mail_transaction_log_file_get_highest_modseq_at(view->cur,
view->cur_offset, &view->prev_modseq) < 0)
view->cur_offset, &view->prev_modseq, &reason) < 0) {
mail_index_set_error(file->log->index,
"Failed to get modseq in %s for all-view: %s",
file->filepath, reason);
return -1;
}
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib-index/test-mail-transaction-log-view.c
Expand Up @@ -49,7 +49,8 @@ int mail_transaction_log_file_map(struct mail_transaction_log_file *file ATTR_UN

int mail_transaction_log_file_get_highest_modseq_at(
struct mail_transaction_log_file *file ATTR_UNUSED,
uoff_t offset ATTR_UNUSED, uint64_t *highest_modseq_r)
uoff_t offset ATTR_UNUSED, uint64_t *highest_modseq_r,
const char **error_r ATTR_UNUSED)
{
*highest_modseq_r = 0;
return 0;
Expand Down

0 comments on commit a0c8af5

Please sign in to comment.