quota-maildir.c revision b76bc28de1c65b7ed6b19c7873b294d3742f5d57
/* Copyright (c) 2006-2008 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "ioloop.h"
#include "nfs-workarounds.h"
#include "file-dotlock.h"
#include "mkdir-parents.h"
#include "read-full.h"
#include "write-full.h"
#include "str.h"
#include "maildir-storage.h"
#include "quota-private.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#define MAILDIRSIZE_FILENAME "maildirsize"
struct maildir_quota_root {
struct quota_root root;
const char *maildirsize_path;
int fd;
unsigned int limits_initialized:1;
};
struct maildir_list_context {
struct mail_storage *storage;
struct maildir_quota_root *root;
struct mailbox_list_iterate_context *iter;
const struct mailbox_info *info;
int state;
};
extern struct quota_backend quota_backend_maildir;
static struct dotlock_settings dotlock_settings = {
};
{
const char *p;
int ret = 0;
return 0;
return -1;
}
continue;
if (p != NULL) {
/* ,S=nnnn[:,] */
p += 3;
if (*p != ':' && *p != '\0' && *p != ',') {
/* not in expected format, fallback to stat() */
} else {
*total_bytes += num;
*total_count += 1;
}
}
*total_count += 1;
ret = -1;
}
}
}
return -1;
}
return ret;
}
static struct maildir_list_context *
struct mail_storage *storage)
{
struct maildir_list_context *ctx;
return ctx;
}
static const char *
{
struct quota_rule *rule;
bool is_file;
for (;;) {
return NULL;
/* mailbox not included in quota */
continue;
}
}
T_BEGIN {
const char *path;
&is_file);
"/new" : "/cur");
} T_END;
break;
/* ignore if the directory got lost, stale or if it was
actually a file and not a directory */
}
}
}
{
return ret;
}
static int
{
struct maildir_list_context *ctx;
int ret = 0;
if (mtime > latest_mtime) {
ret = 1;
break;
}
}
if (maildir_list_deinit(ctx) < 0)
return -1;
return ret;
}
{
struct mail_storage *const *storages;
unsigned int i, count;
const char *p, *dir;
int fd;
/* figure out what permissions we should use for maildirsize.
use the inbox namespace's permissions if possible. */
for (i = 0; i < count; i++) {
break;
}
}
/* the control directory doesn't exist yet? create it */
return -1;
}
i_error("chown(%s,-1,%ld) failed: %m",
}
}
}
if (fd == -1) {
/* someone's just in the middle of updating it */
return 1;
}
return -1;
}
/* if we have no limits, write 0S instead of an empty line */
(unsigned long long)_root->bytes_limit);
}
if (_root->count_limit != 0) {
(unsigned long long)_root->count_limit);
}
(unsigned long long)root->total_bytes,
(unsigned long long)root->total_count);
return -1;
}
/* keep the fd open since we might want to update it later */
if (file_dotlock_replace(&dotlock,
return -1;
}
return 0;
}
{
root->recalc_last_stamp = 0;
}
struct mail_storage *storage)
{
struct maildir_list_context *ctx;
const char *dir;
int ret = 0;
T_BEGIN {
&root->total_count) < 0)
ret = -1;
} T_END;
}
if (maildir_list_deinit(ctx) < 0)
ret = -1;
return ret;
}
{
/* FIXME: can't unlink(), because the limits would be lost. */
return;
}
}
int ret)
{
if (ret == 0) {
/* maildir didn't change, we can write the maildirsize file */
}
if (ret != 0)
return ret;
}
{
struct mail_storage *const *storages;
unsigned int i, count;
int ret = 0;
/* count mails from all storages */
for (i = 0; i < count; i++) {
ret = -1;
break;
}
}
if (ret == 0) {
/* check if any of the directories have changed */
for (i = 0; i < count; i++) {
if (ret != 0)
break;
}
}
}
static bool
{
const char *const *limit;
unsigned long long value;
char *pos;
*bytes_r = 0;
*count_r = 0;
/* 0 values mean unlimited */
switch (pos[0]) {
case 'C':
if (value != 0)
break;
case 'S':
if (value != 0)
break;
default:
break;
}
} else {
}
}
return ret;
}
{
long long bytes_diff, total_bytes;
int count_diff, total_count;
unsigned int line_count = 0;
return -1;
/* first line contains the limits */
/* truncate too high limits to signed 64bit int range */
/* limits haven't changed */
/* we know the limits and they've changed.
the file must be rewritten. */
return 0;
} else {
/* we're using limits from the file. */
}
/* no quota lines. rebuild it. */
return 0;
}
/* rest of the lines contains <bytes> <count> diffs */
total_bytes = 0; total_count = 0;
return -1;
}
if (total_bytes < 0 || total_count < 0) {
/* corrupted */
return -1;
}
/* we're over quota. don't trust these values if the file
contains more than the initial summary line, or if the file
is older than 15 minutes. */
if (line_count > 1)
return 0;
return 0;
}
return 1;
}
{
}
return 0;
return -1;
}
return 1;
}
{
if (dotlock_settings.nfs_flush) {
}
return TRUE;
return TRUE;
return TRUE;
}
{
unsigned int i, size;
int ret;
if (!maildirsize_has_changed(root))
return 1;
return ret;
/* @UNSAFE */
size = 0;
if (ret < 0) {
break;
break;
}
/* we'll need to recalculate the quota */
break;
}
}
/* try to use the file even if we ran into some error. if we don't have
forced limits, we'll need to read the header to get them */
/* skip the last line if there's no LF at the end. Remove the last LF
so we don't get one empty line in the strsplit. */
/* the read failed and there's no usable header, fail. */
return -1;
}
/* If there are any NUL bytes, the file is broken. */
for (i = 0; i < size; i++) {
if (buf[i] == '\0')
break;
}
if (i == size &&
ret == 0)
ret = 1;
else {
/* broken file / need recalculation */
ret = 0;
}
return ret;
}
{
if (!root->limits_initialized) {
i_warning("quota maildir: No maildir storages, "
"ignoring quota.");
}
}
}
{
int ret;
if (!maildirquota_limits_init(root))
return 0;
T_BEGIN {
} T_END;
if (ret == 0) {
/* no quota */
return 0;
/* explicitly specified 0 as quota. keep the quota
updated even if it's not enforced. */
}
}
return ret < 0 ? -1 : 0;
}
{
int ret = 0;
if (count_diff == 0 && bytes_diff == 0)
return 0;
/* We rely on O_APPEND working in here. That isn't NFS-safe, but it
isn't necessarily that bad because the file is recreated once in
a while, and sooner if corruption causes calculations to go
over quota. This is also how Maildir++ spec specifies it should be
done.. */
(long long)bytes_diff, count_diff);
ret = -1;
} else {
i_error("write_full(%s) failed: %m",
}
}
return ret;
}
static struct quota_root *maildir_quota_alloc(void)
{
struct maildir_quota_root *root;
}
{
}
static bool
struct quota_rule *rule,
{
*error_r = "Invalid Maildir++ quota rule";
return FALSE;
}
return TRUE;
}
static void
struct mail_storage *storage)
{
const char *control_dir;
return;
}
static void
{
struct quota_root **roots;
unsigned int i, count;
return;
for (i = 0; i < count; i++) {
}
}
static const char *const *
{
static const char *resources_both[] = {
};
return resources_both;
}
static int
{
if (maildirquota_refresh(root) < 0)
return -1;
} else
return 0;
return 1;
}
static int
struct quota_transaction_context *ctx)
{
struct maildir_quota_root *root =
(struct maildir_quota_root *) _root;
if (!maildirquota_limits_init(root)) {
/* no limits */
return 0;
}
/* even though we don't really care about the limits in here ourself,
we do want to make sure the header gets updated if the limits have
changed. also this makes sure the maildirsize file is created if
it doesn't exist. */
if (maildirquota_refresh(root) < 0)
return -1;
return 0;
}
struct quota_backend quota_backend_maildir = {
"maildir",
{
NULL,
}
};