maildir-util.c revision 16c89b1260c9d07c01c83a9219424d3727069b2e
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2004 Timo Sirainen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_file_do_try(struct index_mailbox *ibox, uint32_t uid,
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen fname = maildir_uidlist_lookup(ibox->uidlist, uid, &flags);
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen if ((flags & MAILDIR_UIDLIST_REC_FLAG_NEW_DIR) != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* probably in new/ dir */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen path = t_strconcat(ibox->path, "/new/", fname, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen path = t_strconcat(ibox->path, "/cur/", fname, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint maildir_file_do(struct index_mailbox *ibox, uint32_t uid,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = maildir_file_do_try(ibox, uid, func, context);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* file is either renamed or deleted. sync the maildir and
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen see which one. if file appears to be renamed constantly,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen don't try to open it more than 10 times. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = maildir_file_do_try(ibox, uid, func, context);
039e42997fe5e0d1c5ad9306dc0ae69bf0e1ca10Timo Sirainen if (i == 10) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenint maildir_filename_get_flags(const char *fname, pool_t pool,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const char *const **keywords_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int num;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (info == NULL || info[1] != '2' || info[2] != ',')
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (info += 3; *info != '\0' && *info != ','; info++) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* FIXME: keyword */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* unknown flag - ignore */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *maildir_filename_set_flags(const char *fname, enum mail_flags flags,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const char *const *keywords)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* remove the old :info from file name, and get the old flags */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (info != NULL && strrchr(fname, '/') > info)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* insert the new flags between old flags. flags must be sorted by
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen their ASCII code. unknown flags are kept. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* skip all known flags */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while (*oldflags == 'D' || *oldflags == 'F' ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen nextflag = *oldflags == '\0' || *oldflags == ',' ? 256 :
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (unsigned char) *oldflags;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((flags_left & MAIL_DRAFT) && nextflag > 'D') {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((flags_left & MAIL_FLAGGED) && nextflag > 'F') {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((flags_left & MAIL_ANSWERED) && nextflag > 'R') {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((flags_left & MAIL_SEEN) && nextflag > 'S') {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((flags_left & MAIL_DELETED) && nextflag > 'T') {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* another flagset, we don't know about these, just keep them */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *maildir_generate_tmp_filename(const struct timeval *tv)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen static unsigned int create_count = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (first_stamp == 0 || first_stamp == ioloop_time) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* it's possible that within last second another process had
4c176a543191e4572910401c218a63306ed68784Timo Sirainen the same PID as us. Use usecs to make sure we don't create
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen duplicate base name. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* Don't bother with usecs. Saves a bit space :) */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint maildir_create_tmp(struct index_mailbox *ibox, const char *dir,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen pool = pool_alloconly_create("maildir_tmp", 4096);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen tmp_fname = maildir_generate_tmp_filename(tv);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen path = p_strconcat(pool, dir, "/", tmp_fname, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* doesn't exist */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* wait and try again - very unlikely */
102c6b4e47a0d17699004e48740f651153fb8b76Timo Sirainen "Not enough disk space");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* a char* hash function from ASU -- from glib */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenunsigned int maildir_hash(const void *p)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const unsigned char *s = p;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int g, h = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen h = (h << 4) + *s;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((g = h & 0xf0000000UL)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen h = h ^ (g >> 24);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint maildir_cmp(const void *p1, const void *p2)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while (*s1 == *s2 && *s1 != ':' && *s1 != '\0') {