~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/lib-index/mail-index.c

  • Committer: Package Import Robot
  • Author(s): Jaldhar H. Vyas
  • Date: 2013-09-09 00:57:32 UTC
  • mfrom: (1.13.11)
  • mto: (4.8.5 experimental) (1.16.1)
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: package-import@ubuntu.com-20130909005732-dn1eell8srqbhh0e
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2003-2012 Dovecot authors, see the included COPYING file */
 
1
/* Copyright (c) 2003-2013 Dovecot authors, see the included COPYING file */
2
2
 
3
3
#include "lib.h"
4
4
#include "ioloop.h"
45
45
        index->mode = 0600;
46
46
        index->gid = (gid_t)-1;
47
47
        index->lock_method = FILE_LOCK_METHOD_FCNTL;
48
 
        index->max_lock_timeout_secs = -1U;
 
48
        index->max_lock_timeout_secs = UINT_MAX;
49
49
 
50
50
        index->keywords_ext_id =
51
51
                mail_index_ext_register(index, MAIL_INDEX_EXT_KEYWORDS,
52
52
                                        128, 2, 1);
53
53
        index->keywords_pool = pool_alloconly_create("keywords", 512);
54
54
        i_array_init(&index->keywords, 16);
55
 
        index->keywords_hash =
56
 
                hash_table_create(default_pool, index->keywords_pool, 0,
57
 
                                  strcase_hash, (hash_cmp_callback_t *)strcasecmp);
 
55
        hash_table_create(&index->keywords_hash, index->keywords_pool, 0,
 
56
                          strcase_hash, strcasecmp);
58
57
        index->log = mail_transaction_log_alloc(index);
59
58
        mail_index_modseq_init(index);
60
59
        return index;
87
86
 
88
87
void mail_index_set_fsync_mode(struct mail_index *index,
89
88
                               enum fsync_mode mode,
90
 
                               enum mail_index_sync_type mask)
 
89
                               enum mail_index_fsync_mask mask)
91
90
{
92
91
        index->fsync_mode = mode;
93
92
        index->fsync_mask = mask;
251
250
bool mail_index_keyword_lookup(struct mail_index *index,
252
251
                               const char *keyword, unsigned int *idx_r)
253
252
{
 
253
        char *key;
254
254
        void *value;
255
255
 
256
256
        /* keywords_hash keeps a name => index mapping of keywords.
257
257
           Keywords are never removed from it, so the index values are valid
258
258
           for the lifetime of the mail_index. */
259
259
        if (hash_table_lookup_full(index->keywords_hash, keyword,
260
 
                                   NULL, &value)) {
 
260
                                   &key, &value)) {
261
261
                *idx_r = POINTER_CAST_TO(value, unsigned int);
262
262
                return TRUE;
263
263
        }
264
264
 
265
 
        *idx_r = (unsigned int)-1;
 
265
        *idx_r = UINT_MAX;
266
266
        return FALSE;
267
267
}
268
268
 
280
280
        keyword = keyword_dup = p_strdup(index->keywords_pool, keyword);
281
281
        *idx_r = array_count(&index->keywords);
282
282
 
283
 
        hash_table_insert(index->keywords_hash,
284
 
                          keyword_dup, POINTER_CAST(*idx_r));
 
283
        hash_table_insert(index->keywords_hash, keyword_dup,
 
284
                          POINTER_CAST(*idx_r));
285
285
        array_append(&index->keywords, &keyword, 1);
286
286
 
287
287
        /* keep the array NULL-terminated, but the NULL itself invisible */
288
 
        (void)array_append_space(&index->keywords);
 
288
        array_append_zero(&index->keywords);
289
289
        array_delete(&index->keywords, array_count(&index->keywords)-1, 1);
290
290
}
291
291
 
403
403
        }
404
404
 
405
405
        if (index->fd == -1) {
406
 
                if (errno != ENOENT)
407
 
                        return mail_index_set_syscall_error(index, "open()");
 
406
                if (errno != ENOENT) {
 
407
                        mail_index_set_syscall_error(index, "open()");
 
408
                        return -1;
 
409
                }
408
410
 
409
411
                /* have to create it */
410
412
                return 0;
422
424
        if (MAIL_INDEX_IS_IN_MEMORY(index))
423
425
                return 0;
424
426
 
425
 
        i_assert(index->map == NULL || index->map->rec_map->lock_id == 0);
426
427
        ret = mail_index_map(index, MAIL_INDEX_SYNC_HANDLER_HEAD);
427
428
        if (ret == 0) {
428
429
                /* it's corrupted - recreate it */
569
570
                i_strdup("(in-memory index)") :
570
571
                i_strconcat(index->dir, "/", index->prefix, NULL);
571
572
 
572
 
        index->shared_lock_count = 0;
573
 
        index->excl_lock_count = 0;
574
573
        index->lock_type = F_UNLCK;
575
574
        index->lock_id_counter = 2;
576
575
 
628
627
 
629
628
        index->lock_id_counter += 2;
630
629
        index->lock_type = F_UNLCK;
631
 
        index->shared_lock_count = 0;
632
 
        index->excl_lock_count = 0;
633
630
}
634
631
 
635
632
void mail_index_close(struct mail_index *index)
693
690
{
694
691
        struct stat st1, st2;
695
692
 
696
 
        i_assert(index->shared_lock_count == 0 ||
697
 
                 (index->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) == 0);
698
 
        i_assert(index->excl_lock_count == 0);
699
 
 
700
693
        if (MAIL_INDEX_IS_IN_MEMORY(index))
701
694
                return 0;
702
695
 
708
701
        if (nfs_safe_stat(index->filepath, &st2) < 0) {
709
702
                if (errno == ENOENT)
710
703
                        return 0;
711
 
                return mail_index_set_syscall_error(index, "stat()");
 
704
                mail_index_set_syscall_error(index, "stat()");
 
705
                return -1;
712
706
        }
713
707
 
714
708
        if (fstat(index->fd, &st1) < 0) {
715
 
                if (!ESTALE_FSTAT(errno))
716
 
                        return mail_index_set_syscall_error(index, "fstat()");
 
709
                if (!ESTALE_FSTAT(errno)) {
 
710
                        mail_index_set_syscall_error(index, "fstat()");
 
711
                        return -1;
 
712
                }
717
713
                /* deleted/recreated, reopen */
718
714
        } else if (st1.st_ino == st2.st_ino &&
719
715
                   CMP_DEV_T(st1.st_dev, st2.st_dev)) {
741
737
        return index->cache;
742
738
}
743
739
 
744
 
int mail_index_set_error(struct mail_index *index, const char *fmt, ...)
 
740
void mail_index_set_error(struct mail_index *index, const char *fmt, ...)
745
741
{
746
742
        va_list va;
747
743
 
756
752
 
757
753
                i_error("%s", index->error);
758
754
        }
759
 
 
760
 
        return -1;
761
755
}
762
756
 
763
757
bool mail_index_is_in_memory(struct mail_index *index)
878
872
                mail_index_file_set_syscall_error(index, path, "fchmod()");
879
873
}
880
874
 
881
 
int mail_index_set_syscall_error(struct mail_index *index,
882
 
                                 const char *function)
 
875
void mail_index_set_syscall_error(struct mail_index *index,
 
876
                                  const char *function)
883
877
{
884
 
        return mail_index_file_set_syscall_error(index, index->filepath,
885
 
                                                 function);
 
878
        mail_index_file_set_syscall_error(index, index->filepath, function);
886
879
}
887
880
 
888
 
int mail_index_file_set_syscall_error(struct mail_index *index,
889
 
                                      const char *filepath,
890
 
                                      const char *function)
 
881
void mail_index_file_set_syscall_error(struct mail_index *index,
 
882
                                       const char *filepath,
 
883
                                       const char *function)
891
884
{
892
885
        const char *errstr;
893
886
 
897
890
        if (ENOSPACE(errno)) {
898
891
                index->nodiskspace = TRUE;
899
892
                if ((index->flags & MAIL_INDEX_OPEN_FLAG_NEVER_IN_MEMORY) == 0)
900
 
                        return -1;
 
893
                        return;
901
894
        }
902
895
 
903
896
        if (errno == EACCES) {
907
900
                        errstr = eacces_error_get_creating(function, filepath);
908
901
                else
909
902
                        errstr = eacces_error_get(function, filepath);
910
 
                return mail_index_set_error(index, "%s", errstr);
 
903
                mail_index_set_error(index, "%s", errstr);
911
904
        } else {
912
905
                const char *suffix = errno != EFBIG ? "" :
913
906
                        " (process was started with ulimit -f limit)";
914
 
                return mail_index_set_error(index, "%s failed with file %s: "
915
 
                                            "%m%s", function, filepath, suffix);
 
907
                mail_index_set_error(index, "%s failed with file %s: "
 
908
                                     "%m%s", function, filepath, suffix);
916
909
        }
917
910
}
918
911