maildir-storage.c revision cfe14619609c0fcf8655c41db9f2ddeb695fc604
/* Copyright (C) 2002-2006 Timo Sirainen */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "hostpid.h"
#include "str.h"
#include "home-expand.h"
#include "mkdir-parents.h"
#include "unlink-directory.h"
#include "maildir-storage.h"
#include "maildir-uidlist.h"
#include "maildir-keywords.h"
#include "maildir-sync.h"
#include "index-mail.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#define MAILDIR_PLUSPLUS_DRIVER_NAME "maildir++"
#define MAILDIR_SUBFOLDER_FILENAME "maildirfolder"
#define MAILDIR_LIST_CONTEXT(obj) \
struct rename_context {
bool found;
const char *newname;
};
extern struct mail_storage maildir_storage;
extern struct mailbox maildir_mailbox;
static int
static int
static int
enum mailbox_list_file_type type,
enum mailbox_info_flags *flags_r);
static int
enum mailbox_list_file_type type,
enum mailbox_info_flags *flags_r);
static const char *strip_tail_slash(const char *path)
{
else
return path;
}
static const char *strip_tail_slash_and_cut(const char *path)
{
}
static int
const char **layout_r)
{
if ((flags & MAIL_STORAGE_FLAG_NO_AUTODETECTION) != 0) {
i_error("maildir: root directory not given");
return -1;
}
/* we'll need to figure out the maildir location ourself.
if (debug) {
i_info("maildir: root exists (%s)",
path);
}
} else {
if (debug) {
i_info("maildir: access(%s, rwx): "
"failed: %m", path);
}
}
} else {
if (debug)
i_info("maildir: HOME not set");
}
if (debug)
i_info("maildir: /cur exists, assuming chroot");
}
} else {
/* <Maildir> [:INBOX=<dir>] [:INDEX=<dir>] [:CONTROL=<dir>] */
if (debug)
if (p == NULL)
else {
do {
p++;
strip_tail_slash_and_cut(p+6);
strip_tail_slash_and_cut(p+6);
strip_tail_slash_and_cut(p+8);
*layout_r =
strip_tail_slash_and_cut(p+7);
}
p = strchr(p, ':');
} while (p != NULL);
}
}
if (debug)
i_info("maildir: couldn't find root dir");
return -1;
}
return 0;
}
static bool maildir_is_internal_name(const char *name)
{
}
const char *name)
{
const char *p;
return FALSE;
if (p != NULL)
name = p + 1;
return !maildir_is_internal_name(name);
}
const char *name)
{
const char *const *tmp;
return FALSE;
t_push();
if (maildir_is_internal_name(*tmp)) {
break;
}
}
t_pop();
return ret;
}
static struct mail_storage *maildir_alloc(void)
{
struct maildir_storage *storage;
}
static int
{
struct mailbox_list_settings list_set;
struct mailbox_list *list;
return -1;
/* normally the maildir is created in verify_inbox() */
if ((flags & MAIL_STORAGE_FLAG_NO_AUTOCREATE) != 0) {
i_error("stat(%s) failed: %m",
}
return -1;
}
}
return -1;
}
} else {
list->v.is_valid_create_name =
}
/* put the temp files into tmp/ directory preferrably */
}
(void)verify_inbox(_storage);
return 0;
}
{
const char *path;
if (debug)
return FALSE;
}
if (debug)
return FALSE;
}
return TRUE;
}
{
if (verify) {
return 0;
"stat(%s) failed: %m", dir);
return -1;
}
}
if (!verify)
return -1;
} else {
"mkdir(%s) failed: %m", dir);
return -1;
}
}
return 0;
}
/* create or fix maildir, ignore if it already exists */
{
const char *path;
return -1;
return -1;
/* if tmp/ directory exists, we need to clean it up once in a while */
/* the directory should be empty. we won't do anything
until ctime changes. */
/* time to scan */
}
return -1;
} else {
return -1;
}
return 0;
}
{
return 0;
name);
"mkdir(%s) failed: %m", dir);
return -1;
}
return 0;
}
{
const char *path;
return -1;
return -1;
return 0;
}
{
}
{
}
static struct mailbox *
enum mailbox_open_flags flags)
{
struct maildir_mailbox *mbox;
struct mail_index *index;
const char *path, *control_dir;
int shared;
/* for shared mailboxes get the create mode from the
permissions of dovecot-shared file. */
if (shared)
sizeof(mbox->maildir_hdr), 0, 0);
if (!shared) {
} else {
}
if ((flags & MAILBOX_OPEN_KEEP_LOCKED) != 0) {
mailbox_close(&box);
return NULL;
}
mbox);
}
}
static struct mailbox *
{
const char *path;
"Maildir doesn't support streamed mailboxes");
return NULL;
}
if (verify_inbox(_storage) < 0)
return NULL;
}
/* exists - make sure the required directories are also there */
return NULL;
return NULL;
} else {
path);
return NULL;
}
}
{
const char *path;
int fd;
/* add the execute bit if either read or write bit is set */
"Mailbox already exists");
}
return -1;
}
}
if (fd == -1) {
return -1;
}
"fchown(%s) failed: %m", path);
}
return 0;
}
const char *name,
bool directory __attr_unused__)
{
int fd;
/* if dovecot-shared exists in the root dir, create the mailbox using
its permissions and gid, and copy the dovecot-shared inside it. */
}
"Mailbox already exists");
}
return -1;
}
/* Maildir++ spec want that maildirfolder named file is created for
all subfolders. */
if (fd == -1)
else
return 0;
}
static const char *
{
const char *root_dir;
return NULL;
MAILDIR_PLUSPLUS_DRIVER_NAME) != 0) {
/* Not maildir++ driver. Don't use this trick. */
return NULL;
}
}
static int
const char *name)
{
struct dirent *d;
unsigned int dir_len;
bool unlinked_something = FALSE;
} else {
"opendir(%s) failed: %m", path);
}
return -1;
}
errno = 0;
if (d->d_name[0] == '.') {
/* skip . and .. */
continue;
continue;
}
if (maildir_is_internal_name(d->d_name)) {
"unlink_directory(%s) failed: %m",
} else {
}
continue;
}
/* trying to unlink() a directory gives either EPERM or EISDIR
(non-POSIX). it doesn't really work anywhere in practise,
so don't bother stat()ing the file first */
"unlink_directory(%s) failed: %m",
}
}
path);
}
return -1;
}
if (!unlinked_something) {
t_strdup_printf("Directory %s isn't empty, "
"can't delete it.", name));
return -1;
}
return 0;
}
static int
{
int count;
/* Make sure the indexes are closed before trying to delete the
directory that contains them. It can still fail with some NFS
implementations if indexes are opened by another session, but
that can't really be helped. */
/* delete the index and control directories */
return -1;
/* check if the mailbox actually exists */
return -1;
}
/* a symlink most likely */
"unlink(%s) failed: %m", src);
return -1;
}
return 0;
}
/* delete the directory directly without any renaming */
}
/* rename the .maildir into ..DOVECOT-TRASH which atomically
marks it as being deleted. If we die before deleting the
..DOVECOT-TRASH directory, it gets deleted the next time
mailbox listing sees it. */
count = 0;
/* it was just deleted under us by
another process */
return -1;
}
if (!EDESTDIREXISTS(errno)) {
return -1;
}
/* already existed, delete it and try again */
"unlink_directory(%s) failed: %m", dest);
return -1;
}
count++;
}
"unlink_directory(%s) failed: %m", dest);
/* it's already renamed to ..dir, which means it's
deleted as far as the client is concerned. Report
success. */
}
return 0;
}
{
/* INBOX often exists as the root ~/Maildir.
We can't rename it then. */
"Renaming INBOX isn't supported.");
return -1;
}
}
}
{
}
return index_storage_mailbox_close(box);
}
enum mailbox_status_items items,
struct mailbox_status *status_r)
{
return -1;
/* if index isn't up-to-date, get the values from uidlist */
return -1;
if (uid_validity != 0)
return 0;
}
{
else {
}
}
static int
enum mailbox_list_file_type type,
enum mailbox_info_flags *flags_r)
{
const char *path;
int ret;
if (maildir_is_internal_name(fname)) {
return 0;
}
switch (type) {
/* non-directories are not */
return 0;
break;
}
t_push();
ret = 1;
/* temporary NFS file */
ret = 0;
} else {
ret = 0;
}
/* this was a directory. maybe it has children. */
ret = 1;
} else {
ret = 0;
}
t_pop();
return ret;
}
static int
enum mailbox_list_file_type type,
enum mailbox_info_flags *flags_r)
{
int ret;
const char *path;
/* this directory is in the middle of being deleted,
or the process trying to delete it had died.
delete it ourself if it's been there longer than
one hour. */
t_push();
t_pop();
return 0;
}
switch (type) {
/* all directories are valid maildirs */
return 1;
/* non-directories are not */
return 0;
/* need to check with stat() to be sure */
break;
}
/* Check files beginning with .nfs always because they may be
temporary files created by the kernel */
const char *path;
t_push();
/* if fname="", we're checking if a base maildir has INBOX */
ret = 1;
else {
else
ret = 0;
}
/* just deleted? */
ret = 0;
} else {
ret = 0;
}
t_pop();
} else {
ret = 1;
}
return ret;
}
static void maildir_class_init(void)
{
}
static void maildir_class_deinit(void)
{
}
struct mail_storage maildir_storage = {
{
NULL,
}
};
struct mailbox maildir_mailbox = {
{
NULL,
}
};