maildir-util.c revision 8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91
/* Copyright (C) 2004 Timo Sirainen */
#include "lib.h"
#include "hostpid.h"
#include "ioloop.h"
#include "str.h"
#include "maildir-storage.h"
#include "maildir-uidlist.h"
#include "maildir-keywords.h"
#include "maildir-sync.h"
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <utime.h>
{
int ret;
return -2; /* expunged */
t_push();
if ((flags & MAILDIR_UIDLIST_REC_FLAG_NEW_DIR) != 0) {
/* probably in new/ dir */
if (ret != 0) {
t_pop();
return ret;
}
}
t_pop();
return ret;
}
{
int i, ret;
for (i = 0; i < 10 && ret == 0; i++) {
/* file is either renamed or deleted. sync the maildir and
see which one. if file appears to be renamed constantly,
don't try to open it more than 10 times. */
if (maildir_storage_sync_force(mbox) < 0)
return -1;
}
if (i == 10) {
ret = -1;
}
}
{
static unsigned int create_count = 0;
static time_t first_stamp = 0;
/* it's possible that within last second another process had
the same PID as us. Use usecs to make sure we don't create
duplicate base name. */
return t_strdup_printf("%s.P%sQ%uM%s.%s",
create_count++,
} else {
/* Don't bother with usecs. Saves a bit space :) */
return t_strdup_printf("%s.P%sQ%u.%s",
create_count++, my_hostname);
}
}
{
int fd;
tv = &ioloop_timeval;
for (;;) {
/* doesn't exist */
break;
}
/* wait and try again - very unlikely */
sleep(2);
i_fatal("gettimeofday(): %m");
}
if (fd == -1) {
"Not enough disk space");
} else {
"open(%s) failed: %m", path);
}
"fchown(%s) failed: %m", path);
}
}
return fd;
}
{
struct dirent *d;
unsigned int dir_len;
"opendir(%s) failed: %m", dir);
}
return;
}
t_push();
if (d->d_name[0] == '.' &&
/* skip . and .. */
continue;
}
}
}
}
}
t_pop();
#ifdef HAVE_DIRFD
"fstat(%s) failed: %m", dir);
}
#else
"stat(%s) failed: %m", dir);
}
#endif
/* mounted with noatime. update it ourself. */
"utime(%s) failed: %m", dir);
}
}
"closedir(%s) failed: %m", dir);
}
}
{
fname += 3;
break;
}
}
if (*fname == '\0')
return FALSE;
fname++;
}
if (*fname != MAILDIR_INFO_SEP &&
*fname != MAILDIR_EXTRA_SEP &&
*fname != '\0')
return FALSE;
return TRUE;
}
/* a char* hash function from ASU -- from glib */
unsigned int maildir_hash(const void *p)
{
const unsigned char *s = p;
unsigned int g, h = 0;
while (*s != MAILDIR_INFO_SEP && *s != '\0') {
i_assert(*s != '/');
h = (h << 4) + *s;
if ((g = h & 0xf0000000UL)) {
h = h ^ (g >> 24);
h = h ^ g;
}
s++;
}
return h;
}
{
}
return 0;
}