maildir-index.c revision dec504e26667fb97d47f6145e5f65c0bc1c615ea
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
b90c23a9862b91594959b918b035d73f7bc0b265Timo SirainenMailFlags maildir_filename_get_flags(const char *fname, MailFlags default_flags)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (info == NULL || info[1] != '2' || info[2] != ',')
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* custom flag */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* unknown flag - ignore */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenconst char *maildir_filename_set_flags(const char *fname, MailFlags flags)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* remove the old :info from file name, and get the old flags */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (info != NULL && strrchr(fname, '/') > info)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* insert the new flags between old flags. flags must be sorted by
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen their ASCII code. unknown flags are kept. */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen flags_buf = t_malloc(MAIL_FLAGS_COUNT+strlen(oldflags)+1);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* skip all known flags */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen while (*oldflags == 'D' || *oldflags == 'F' ||
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen (unsigned char) *oldflags;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if ((flags & MAIL_FLAGGED) && nextflag > 'F') {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if ((flags & MAIL_ANSWERED) && nextflag > 'R') {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if ((flags & MAIL_DELETED) && nextflag > 'T') {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if ((flags & MAIL_CUSTOM_FLAGS_MASK) && nextflag > 'a') {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen for (i = 0; i < MAIL_CUSTOM_FLAGS_COUNT; i++) {
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen if (flags & (1 << (i + MAIL_CUSTOM_FLAG_1_BIT)))
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen *p++ = 'a' + i;
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen return t_strconcat(fname, ":2,", flags_buf, NULL);
b90c23a9862b91594959b918b035d73f7bc0b265Timo SirainenMailIndex *maildir_index_alloc(const char *dir)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen memcpy(index, &maildir_index, sizeof(MailIndex));
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic void maildir_index_free(MailIndex *index)
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenstatic int maildir_index_update_flags(MailIndex *index, MailIndexRecord *rec,
0e94016c18197cb42d00be0085e6da4223a1c84eTimo Sirainen if (!mail_index_update_flags(index, rec, seq, flags, external_change))
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* we need to update the flags in the file name */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen old_fname = index->lookup_field(index, rec, FIELD_TYPE_LOCATION);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_set_error(index, "Corrupted index file %s: "
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen "Missing location field for record %u",
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen new_fname = maildir_filename_set_flags(old_fname, flags);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen old_path = t_strconcat(index->dir, "/cur/", old_fname, NULL);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen new_path = t_strconcat(index->dir, "/cur/", new_fname, NULL);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* minor problem: new_path is overwritten if it exists.. */
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen index_set_error(index, "maildir flags update: "
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen "rename(%s, %s) failed: %m",
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen /* update the filename in index */