mbox-file.c revision 3f190f4cbb9233a3a6830956cb5c7ae56a577b79
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik/* Copyright (C) 2002-2003 Timo Sirainen */
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik#include "lib.h"
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik#include "istream.h"
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik#include "mbox-storage.h"
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik#include "mbox-file.h"
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik#include "istream-raw-mbox.h"
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik#include <sys/stat.h>
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnikint mbox_file_open(struct index_mailbox *ibox)
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik{
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik struct stat st;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik int fd;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_assert(ibox->mbox_fd == -1);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik fd = open(ibox->path, ibox->mbox_readonly ? O_RDONLY : O_RDWR);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (fd == -1 && errno == EACCES && !ibox->mbox_readonly) {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_readonly = TRUE;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik fd = open(ibox->path, O_RDONLY);
c3baf4d7c0cbd139d96fd04f6b3c175d2f99de6cLukas Slebodnik }
089db891b8a7a94b5666e8cffb1d7b359d6aeb6eLukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (fd == -1) {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik mbox_set_syscall_error(ibox, "open()");
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik return -1;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik }
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (fstat(fd, &st) < 0) {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik mbox_set_syscall_error(ibox, "fstat()");
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik (void)close(fd);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik return -1;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik }
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_fd = fd;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_dev = st.st_dev;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_ino = st.st_ino;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik return 0;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik}
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnikvoid mbox_file_close(struct index_mailbox *ibox)
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik{
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik mbox_file_close_stream(ibox);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (ibox->mbox_fd != -1) {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (close(ibox->mbox_fd) < 0)
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_error("close(mbox) failed: %m");
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_fd = -1;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik }
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik}
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnikint mbox_file_open_stream(struct index_mailbox *ibox)
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik{
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (ibox->mbox_stream != NULL)
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik return 0;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_assert(ibox->mbox_file_stream == NULL);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (ibox->mbox_fd == -1) {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (mbox_file_open(ibox) < 0)
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik return -1;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik }
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (ibox->mail_read_mmaped) {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_file_stream =
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_stream_create_mmap(ibox->mbox_fd, default_pool,
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik MAIL_MMAP_BLOCK_SIZE,
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik 0, 0, FALSE);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik } else {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_file_stream =
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_stream_create_file(ibox->mbox_fd, default_pool,
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik MAIL_READ_BLOCK_SIZE, FALSE);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik }
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_stream =
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_stream_create_raw_mbox(default_pool, ibox->mbox_file_stream);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik return 0;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik}
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnikvoid mbox_file_close_stream(struct index_mailbox *ibox)
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik{
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik if (ibox->mbox_stream != NULL) {
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_stream_close(ibox->mbox_file_stream);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_stream_unref(ibox->mbox_file_stream);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_file_stream = NULL;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik i_stream_unref(ibox->mbox_stream);
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik ibox->mbox_stream = NULL;
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik }
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik}
a2c10cf31d14bac598f5cd008973375c3f9575a6Lukas Slebodnik