mail-modifylog.c revision 79d62d195b7eb330c4962ad166f2d066f8837e91
/* Copyright (C) 2002 Timo Sirainen */
#include "lib.h"
#include "mmap-util.h"
#include "write-full.h"
#include "mail-index.h"
#include "mail-index-util.h"
#include "mail-modifylog.h"
#include <stdlib.h>
#include <fcntl.h>
/* Maximum size for modify log (isn't exact) */
#define MAX_MODIFYLOG_SIZE 10240
struct _MailModifyLog {
int fd;
char *filepath;
void *mmap_base;
unsigned int modified:1;
unsigned int dirty_mmap:1;
unsigned int second_log:1;
};
const char *function)
{
}
{
/* make sure we don't get back here */
}
{
return 0;
return -1;
}
return 1;
}
/* Returns 1 = ok, 0 = failed to get the lock, -1 = error */
{
int ret;
if (ret == -1)
return ret;
}
{
return TRUE;
}
/* returns 1 = yes, 0 = no, -1 = error */
{
int ret;
/* try grabbing exclusive lock */
if (ret == -1)
return -1;
/* revert back to shared lock */
case 0:
/* shouldn't happen */
/* fall through */
case -1:
return -1;
}
return ret == 0 ? 1 : 0;
}
{
unsigned int extra;
return TRUE;
return FALSE;
}
/* FIXME: we could do better.. */
i_panic("modify log corrupted");
}
sizeof(ModifyLogRecord);
if (extra != 0) {
/* partial write or corrupted -
truncate the file to valid length */
}
return TRUE;
}
{
return log;
}
{
}
}
}
const char *path)
{
/* write header */
"log %s: %m", path);
return FALSE;
}
"log %s: %m", path);
return FALSE;
}
return TRUE;
}
const char *path)
{
if (fd == -1) {
"file %s: %m", path);
return FALSE;
}
if (ret == -1) {
"file %s: %m", path);
}
return TRUE;
}
return FALSE;
}
{
const char *path;
!mmap_update(log)) {
/* fatal failure */
return FALSE;
}
return TRUE;
}
/* Returns 1 = ok, 0 = full, -1 = error */
{
if (fd == -1) {
"file %s: %m", path);
}
return -1;
}
ret = 1;
"log file %s: %m", path);
ret = -1;
}
"file %s", path);
ret = -1;
/* we have to rebuild it, make sure it's deleted. */
}
/* full */
ret = 0;
}
if (ret == 1) {
} else {
}
return ret;
}
{
int i, ret;
for (i = 0; i < 2; i++) {
/* first try <index>.log */
if (ret == 1)
return TRUE;
if (ret == 0) {
/* then <index>.log.2 */
return TRUE;
}
return TRUE;
return TRUE;
/* maybe the file was just switched, check the logs again */
}
return FALSE;
}
{
if (!mail_modifylog_find_or_create(log) ||
!mmap_update(log)) {
/* fatal failure */
return FALSE;
}
return TRUE;
}
{
}
{
return TRUE;
return FALSE;
}
}
return FALSE;
}
return TRUE;
}
int external_change)
{
if (!external_change) {
switch (mail_modifylog_have_other_users(log)) {
case 0:
/* we're the only one having this log open,
no need for modify log. */
return TRUE;
case -1:
return FALSE;
}
}
return FALSE;
}
return FALSE;
}
if (!external_change) {
}
return TRUE;
}
unsigned int uid, int external_change)
{
/* expunges must not be added when log isn't synced */
}
unsigned int uid, int external_change)
{
}
unsigned int *count)
{
*count = 0;
if (!mmap_update(log))
return NULL;
log->mmap_length);
return rec;
}
{
return mail_modifylog_open_or_create(index);
}
{
const char *path;
/* FIXME: we want to update the _old_ file's header.
and this changes the new one. and it's already closed the
old one and mmap() is invalid, and we crash here.. */
}
}
{
if (!mmap_update(log))
return FALSE;
/* log file is full, switch to next one */
return mail_modifylog_switch_file(log);
}
/* we are already synced */
return TRUE;
}
/* if the other file isn't locked, switch to it */
return TRUE;
}
return TRUE;
}
{
}
const unsigned int *
unsigned int first_seq,
unsigned int last_seq,
unsigned int *expunges_before)
{
*expunges_before = 0;
if (!mmap_update(log))
return NULL;
/* find the first expunged message that affects our range */
log->mmap_length);
break;
rec++;
}
/* none found */
*expunges = 0;
return expunges;
}
/* allocate memory for the returned array. the file size - synced
position should be quite near the amount of memory we need, unless
there's lots of FLAGS_CHANGED records which is why there's the
second check to make sure it's not unneededly large. */
sizeof(ModifyLogRecord);
/* last_pos_seq is updated all the time to contain the last_seq
comparable to current record's seq. number */
before = 0;
continue;
/* before our range */
before++;
last_pos_seq--;
/* within our range */
last_pos_seq--;
if (max_records-- == 0) {
/* log contains more data than it should
have - must be corrupted. */
return NULL;
}
}
}
*arr = 0;
/* sort the UID array, not including the terminating 0 */
return expunges;
}
const unsigned int *
unsigned int first_uid,
unsigned int last_uid)
{
/* pretty much copy&pasted from sequence code above ..
kind of annoying */
if (!mmap_update(log))
return NULL;
/* find the first expunged message that affects our range */
log->mmap_length);
break;
rec++;
}
/* none found */
*expunges = 0;
return expunges;
}
/* allocate memory for the returned array. the file size - synced
position should be quite near the amount of memory we need, unless
there's lots of FLAGS_CHANGED records which is why there's the
second check to make sure it's not unneededly large. */
sizeof(ModifyLogRecord);
before = 0;
/* within our range */
if (max_records-- == 0) {
/* log contains more data than it should
have - must be corrupted. */
return NULL;
}
}
rec++;
}
*arr = 0;
/* sort the UID array, not including the terminating 0 */
return expunges;
}