/* Copyright (c) 2006-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "ioloop.h"
#include "nfs-workarounds.h"
#include "safe-mkstemp.h"
#include "mkdir-parents.h"
#include "read-full.h"
#include "write-full.h"
#include "str.h"
#include "maildir-storage.h"
#include "mailbox-list-private.h"
#include "quota-private.h"
#include <stdio.h>
#include <dirent.h>
struct maildir_quota_root {
const char *maildirsize_path;
int fd;
};
struct maildir_list_context {
int state;
};
extern struct quota_backend quota_backend_maildir;
.timeout = 0,
.stale_timeout = 30
};
{
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 *
{
return ctx;
}
{
&path) > 0) {
"/new" : "/cur");
}
}
static const char *
{
for (;;) {
return NULL;
/* mailbox not included in quota */
continue;
}
}
if (!maildir_set_next_path(ctx)) {
continue;
}
break;
/* ignore if the directory got lost, stale or if it was
actually a file and not a directory */
}
}
}
const char **error_r)
{
if (ret < 0)
"Listing mailboxes failed: %s",
return ret;
}
static int
const char **error_r)
{
int ret = 0;
if (mtime > latest_mtime) {
ret = 1;
break;
}
}
return -1;
return ret;
}
{
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++) {
continue;
&perm);
break;
}
/* the control directory doesn't exist yet? create it */
perm.file_create_gid_origin) < 0 &&
return -1;
}
}
if (fd == -1) {
return -1;
}
/* if we have no limits, write 0S instead of an empty line */
}
if (_root->count_limit != 0) {
}
i_close_fd(&fd);
return -1;
}
i_close_fd(&fd);
return -1;
}
return 0;
}
{
root->recalc_last_stamp = 0;
}
struct mail_namespace *ns,
const char **error_r)
{
const char *dir;
int ret = 0;
ret = -1;
}
ret = -1;
return ret;
}
{
/* FIXME: can't unlink(), because the limits would be lost. */
return;
}
}
{
if (ret == 0) {
/* maildir didn't change, we can write the maildirsize file */
*error_r = "failed to write maildirsize";
}
if (ret != 0)
return ret;
}
const char **error_r)
{
unsigned int i, count;
int ret = 0;
/* count mails from all namespaces */
for (i = 0; i < count; i++) {
continue;
ret = -1;
break;
}
}
if (ret == 0) {
/* check if any of the directories have changed */
for (i = 0; i < count; i++) {
namespaces[i]))
continue;
error_r);
if (ret != 0)
break;
}
}
}
static bool
{
const char *const *limit;
unsigned long long value;
const char *pos;
*bytes_r = 0;
*count_r = 0;
/* 0 values mean unlimited */
continue;
}
switch (pos[0]) {
case 'C':
if (value != 0)
break;
case 'S':
if (value != 0)
break;
default:
break;
}
} else {
}
}
return ret;
}
{
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;
}
const char **error_r)
{
return 0;
return -1;
}
return 1;
}
{
if (dotlock_settings.nfs_flush) {
}
return TRUE;
return TRUE;
return TRUE;
}
const char **error_r)
{
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;
}
{
const char *control_dir;
if (root->limits_initialized)
return FALSE;
}
/* non-maildir namespace, skip */
if ((storage->class_flags &
MAIL_STORAGE_CLASS_FLAG_NOQUOTA) == 0) {
i_warning("quota: Namespace '%s' is not Maildir, "
"skipping for Maildir++ quota",
}
return FALSE;
}
&control_dir))
i_unreached();
}
return TRUE;
}
const char **error_r)
{
int ret, n = 0;
if (!maildirquota_limits_init(root))
return 1;
do {
if (n == NFS_ESTALE_RETRY_COUNT)
n++;
return ret;
}
static int
const char **error_r)
{
int ret;
*recalculated_r = FALSE;
if (ret == 0) {
/* no quota */
return 0;
/* explicitly specified 0 as quota. keep the quota
updated even if it's not enforced. */
}
if (ret == 0)
*recalculated_r = TRUE;
}
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) < 0)
i_unreached();
ret = -1;
} else {
i_error("write_full(%s) failed: %m",
}
} else {
/* close the file to force a flush with NFS */
ret = -1;
}
}
return ret;
}
{
}
const char **error_r)
{
}
{
}
static bool
struct quota_rule *rule,
{
bytes = 0;
count = 0;
"quota-maildir: Invalid Maildir++ quota rule \"%s\"",
str);
return FALSE;
}
return TRUE;
}
const char **error_r)
{
const char *error;
"quota-maildir: Failed to read limits: %s", error);
return -1;
}
return 0;
}
static void
struct mail_namespace *ns)
{
}
static void
{
unsigned int i, count;
for (i = 0; i < count; i++) {
}
}
static const char *const *
{
static const char *resources_both[] = {
};
return resources_both;
}
static enum quota_get_result
{
bool recalculated;
const char *error;
return QUOTA_GET_RESULT_INTERNAL_ERROR;
}
} else {
}
return QUOTA_GET_RESULT_LIMITED;
}
static int
struct quota_transaction_context *ctx,
const char **error_r)
{
bool recalculated;
const char *error;
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. */
"quota-maildir: Could not update storage usage data: %s",
error);
return -1;
}
if (recalculated) {
/* quota was just recalculated and it already contains the changes
we wanted to do. */
}
return 0;
}
.name = "maildir",
.v = {
}
};