mailbox-list-delete.c revision 2e37d45867d081db150ab78dad303b9077aea24f
/* Copyright (c) 2010-2011 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "hex-binary.h"
#include "hostpid.h"
#include "randgen.h"
#include "unlink-directory.h"
#include "mailbox-list-private.h"
#include "mailbox-list-delete.h"
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
static int
const char *path)
{
const char *root_dir;
return 0;
"INBOX can't be deleted.");
return -1;
}
"Mail storage root can't be deleted.");
return -1;
}
static const char *unique_fname(void)
{
unsigned char randbuf[8];
}
const char *name,
const char *trash_dir)
{
const char *src, *trash_dest;
unsigned int count;
return -1;
/* rename the mailbox dir to trash dir, which atomically
marks it as being deleted. */
/* either the source was just deleted or
the trash dir was deleted. */
continue;
}
return -1;
}
/* can't do this the fast way */
return 0;
}
if (!EDESTDIREXISTS(errno)) {
return -1;
return -1;
}
/* trash dir already exists. the reasons for this are:
a) another process is in the middle of deleting it
b) previous process crashed and didn't delete it
c) NFS: mailbox was recently deleted, but some connection
still has that mailbox open. the directory contains .nfs*
files that can't be deleted until the mailbox is closed.
Because of c) we'll first try to rename the mailbox under
the trash directory and only later try to delete the entire
trash directory. */
if (trash_dir == trash_dest) {
unique_fname(), NULL);
} else if (mailbox_list_delete_trash(trash_dest) < 0 &&
"unlink_directory(%s) failed: %m", trash_dest);
return -1;
}
}
if (mailbox_list_delete_trash(trash_dir) < 0 &&
"unlink_directory(%s) failed: %m", trash_dir);
/* it's already renamed to trash dir, which means it's
deleted as far as the client is concerned. Report
success. */
}
return 1;
}
const char *name)
{
const char *path;
/* we can simply unlink() the file */
return 0;
return -1;
} else {
if (!mailbox_list_set_error_from_errno(list)) {
"unlink(%s) failed: %m", path);
}
return -1;
}
}
bool rmdir_path)
{
struct dirent *d;
unsigned int dir_len;
return -1;
} else {
if (!mailbox_list_set_error_from_errno(list)) {
"opendir(%s) failed: %m", path);
}
}
return -1;
}
if (d->d_name[0] == '.') {
/* skip . and .. */
continue;
continue;
}
if (mailbox_dir) {
"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",
}
}
if (errno != 0)
path);
}
if (rmdir_path) {
path);
return -1;
}
}
if (!unlinked_something) {
"Mailbox has children, can't delete it");
return -1;
}
return 0;
}
enum mailbox_list_path_type type)
{
const char *root_dir, *p;
unsigned int len;
the .imap/ part, but no further. */
len--;
if (len == 0)
return;
len--;
len--;
if (len == 0)
return;
}
return;
path);
return;
}
if (p == NULL)
break;
}
}
enum mailbox_list_path_type type)
{
const char *mailbox_path, *path;
return;
/* this directory may contain also child mailboxes' data.
we don't want to delete that. */
rmdir_path) < 0)
return;
} else {
if (mailbox_list_delete_trash(path) < 0 &&
"unlink_directory(%s) failed: %m", path);
}
}
/* avoid leaving empty directories lying around */
}
{
}
int mailbox_list_delete_trash(const char *path)
{
/* it's a symlink? try just deleting it */
return 0;
return -1;
}
}
return 0;
}