annotate src/lib-storage/index/dbox/dbox-uidlist.c @ 4208:67f6d3afa5a5 HEAD

Fixes
author Timo Sirainen <tss@iki.fi>
date Thu, 20 Apr 2006 19:12:29 +0300
parents 513aa0df39ee
children 83322a523c91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (C) 2005 Timo Sirainen */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "hex-dec.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "array.h"
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
6 #include "bsearch-insert-pos.h"
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "seq-range-array.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "str.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "istream.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include "ostream.h"
3858
2e5c34fe9634 Small (compiling) fixes
Timo Sirainen <tss@iki.fi>
parents: 3846
diff changeset
11 #include "ostream-crlf.h"
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include "write-full.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 #include "dbox-file.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 #include "dbox-storage.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 #include "dbox-uidlist.h"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 #include <stddef.h>
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 #include <stdio.h>
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
19 #include <stdlib.h>
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 #include <fcntl.h>
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 #include <unistd.h>
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 #include <utime.h>
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 #include <sys/stat.h>
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 #define DBOX_APPEND_MAX_OPEN_FDS 64
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 #define DBOX_UIDLIST_VERSION 1
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 #define DBOX_UIDLIST_FILENAME "index"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 struct dbox_save_file {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 struct dbox_file *file;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 dev_t dev;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 ino_t ino;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 struct dotlock *dotlock;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 array_t ARRAY_DEFINE(seqs, unsigned int);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 /* append offset for the first mail we've saved */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 uoff_t append_offset;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 };
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 struct dbox_uidlist {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 struct dbox_mailbox *mbox;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 char *path;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 int fd;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 struct dotlock *dotlock;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 int lock_fd;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 unsigned int version;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 uint32_t uid_validity, last_uid, last_file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 ino_t ino;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 time_t mtime;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 uint32_t file_seq_highwater;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 pool_t entry_pool;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 array_t ARRAY_DEFINE(entries, struct dbox_uidlist_entry *);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 uint32_t entry_last_file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 unsigned int appending:1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 unsigned int need_full_rewrite:1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 };
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 struct dbox_uidlist_append_ctx {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 pool_t pool;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 struct dbox_uidlist *uidlist;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
71 time_t min_usable_timestamp;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 unsigned int mail_count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 array_t ARRAY_DEFINE(files, struct dbox_save_file *);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 unsigned int open_fds;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 unsigned int locked:1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 };
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 struct dbox_uidlist_sync_ctx {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 struct dbox_uidlist *uidlist;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 unsigned int modified:1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 };
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 const struct dotlock_settings uidlist_dotlock_settings = {
3985
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
86 MEMBER(temp_prefix) NULL,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
87 MEMBER(lock_suffix) NULL,
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88
3985
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
89 MEMBER(timeout) 120,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
90 MEMBER(stale_timeout) 60,
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91
3985
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
92 MEMBER(callback) NULL,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
93 MEMBER(context) NULL,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
94
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
95 MEMBER(use_excl_lock) FALSE
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 };
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 const struct dotlock_settings dbox_file_dotlock_set = {
3985
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
99 MEMBER(temp_prefix) NULL,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
100 MEMBER(lock_suffix) NULL,
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101
3985
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
102 MEMBER(timeout) 120,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
103 MEMBER(stale_timeout) 60,
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104
3985
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
105 MEMBER(callback) NULL,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
106 MEMBER(context) NULL,
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
107
38c352bb7bb7 Dotlock fixes
Timo Sirainen <tss@iki.fi>
parents: 3957
diff changeset
108 MEMBER(use_excl_lock) FALSE
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 };
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 struct dbox_uidlist *dbox_uidlist_init(struct dbox_mailbox *mbox)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 struct dbox_uidlist *uidlist;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 uidlist = i_new(struct dbox_uidlist, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 uidlist->mbox = mbox;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 uidlist->fd = -1;
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
118 uidlist->mtime = -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 uidlist->lock_fd = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 uidlist->entry_pool =
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
121 pool_alloconly_create("uidlist entry pool", 1024*32);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 uidlist->path =
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 i_strconcat(mbox->path, "/"DBOX_MAILDIR_NAME"/"
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 DBOX_UIDLIST_FILENAME, NULL);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 ARRAY_CREATE(&uidlist->entries, default_pool,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 struct dbox_uidlist_entry *, 64);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 return uidlist;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 void dbox_uidlist_deinit(struct dbox_uidlist *uidlist)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132 i_assert(!uidlist->appending);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 array_free(&uidlist->entries);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 pool_unref(uidlist->entry_pool);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 i_free(uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 i_free(uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 static int uidlist_merge(array_t *uid_list, const struct seq_range *seqs)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 ARRAY_SET_TYPE(uid_list, struct seq_range);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143 struct seq_range *range;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 unsigned int count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146 range = array_get_modifyable(uid_list, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 i_assert(count > 0);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 if (seqs->seq1 <= range[count-1].seq2)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150 return FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 if (seqs->seq1-1 == range[count-1].seq2) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153 /* we can just continue the existing range */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 range[count-1].seq2 = seqs->seq2;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 array_append(uid_list, seqs, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 return TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160
3756
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
161 static int dbox_uidlist_entry_cmp(const void *key, const void *p)
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
162 {
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
163 const unsigned int *file_seq = key;
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
164 struct dbox_uidlist_entry *const *entry = p;
3756
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
165
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
166 return (int)*file_seq - (int)(*entry)->file_seq;
3756
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
167 }
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
168
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3859
diff changeset
169 static bool dbox_uidlist_add_entry(struct dbox_uidlist *uidlist,
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3859
diff changeset
170 const struct dbox_uidlist_entry *src_entry)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 {
3756
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
172 struct dbox_uidlist_entry *dest_entry, **entries, **pos;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 const struct seq_range *range;
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
174 unsigned int i, idx, count;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
176 entries = array_get_modifyable(&uidlist->entries, &count);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 if (src_entry->file_seq > uidlist->entry_last_file_seq) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 /* append new file sequence */
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
179 idx = count;
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
180 } else {
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
181 pos = bsearch_insert_pos(&src_entry->file_seq, entries, count,
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
182 sizeof(*entries),
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
183 dbox_uidlist_entry_cmp);
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
184 idx = pos - entries;
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
185 }
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
186
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
187 if (idx == count || entries[idx]->file_seq != src_entry->file_seq) {
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
188 /* new entry */
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 dest_entry = p_new(uidlist->entry_pool,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 struct dbox_uidlist_entry, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 *dest_entry = *src_entry;
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
192 array_insert(&uidlist->entries, idx, &dest_entry, 1);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 uidlist->entry_last_file_seq = src_entry->file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 if (src_entry->file_seq > uidlist->last_file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 uidlist->last_file_seq = src_entry->file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197 } else {
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
198 /* merge to existing entry. UIDs must be growing since only
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
199 new mails are appended */
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
200 dest_entry = entries[idx];
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
201 if (src_entry->create_time > dest_entry->create_time)
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
202 dest_entry->create_time = src_entry->create_time;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 if (src_entry->file_size > dest_entry->file_size)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 dest_entry->file_size = src_entry->file_size;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
206 range = array_get(&src_entry->uid_list, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 if (!uidlist_merge(&dest_entry->uid_list, &range[i])) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 mail_storage_set_critical(
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 STORAGE(uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 "%s: UIDs not ordered (file_seq=%u)",
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212 uidlist->path, src_entry->file_seq);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 return FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 return TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3859
diff changeset
220 static bool dbox_uidlist_next(struct dbox_uidlist *uidlist, const char *line)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 struct dbox_uidlist_entry *entry;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223 struct seq_range range;
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
224 const char *error = NULL;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225 uint32_t digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
228 /* <uid list> <file seq> [<last write timestamp> <file size>] */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
230 entry = t_new(struct dbox_uidlist_entry, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
231 ARRAY_CREATE(&entry->uid_list, uidlist->entry_pool,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
232 struct seq_range, 8);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234 /* get uid list */
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
235 range.seq1 = range.seq2 = 0;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
236 for (digit = 0; *line != '\0'; line++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237 if (*line >= '0' && *line <= '9')
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238 digit = digit * 10 + *line-'0';
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239 else {
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
240 if (range.seq1 == 0) {
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
241 if (digit <= range.seq2) {
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
242 /* broken */
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
243 error = t_strdup_printf("UID %u <= %u",
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
244 digit,
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
245 range.seq2);
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
246 break;
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
247 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
248 range.seq1 = digit;
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
249 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250 if (*line == ',' || *line == ' ') {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 if (range.seq1 > digit) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252 /* broken */
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
253 error = t_strdup_printf("UID %u > %u",
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
254 range.seq1,
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
255 digit);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 range.seq2 = digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
259 array_append(&entry->uid_list, &range, 1);
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
260 range.seq1 = 0;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262 if (digit > uidlist->last_uid) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 /* last_uid isn't up to date */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264 uidlist->last_uid = digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 if (*line == ' ')
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
269 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
270 digit = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
274 if (error == NULL) {
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
275 if (*line != ' ') {
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
276 error = *line == '\0' ? "File sequence missing" :
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
277 "Expecting space after UID list";
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
278 } else if (array_count(&entry->uid_list) == 0)
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
279 error = "UID list missing";
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
280 }
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
281
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
282 if (error != NULL) {
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283 mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
284 "%s: Corrupted entry: %s", uidlist->path, error);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
285 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286 return FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
288
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
289 /* get file seq */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290 for (digit = 0, line++; *line >= '0' && *line <= '9'; line++)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291 digit = digit * 10 + *line-'0';
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 entry->file_seq = digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
294 /* get create timestamp */
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
295 line++;
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
296 for (; *line >= '0' && *line <= '9'; line++)
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
297 entry->create_time = entry->create_time * 10 + *line-'0';
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
299 if (*line != ' ') {
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
300 mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
301 "%s: Corrupted entry: Expecting space after timestamp",
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
302 uidlist->path);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
304 t_pop();
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
305 return FALSE;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 }
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
307 /* get file size */
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
308 for (; *line >= '0' && *line <= '9'; line++)
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
309 entry->file_size = entry->file_size * 10 + *line-'0';
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311 ret = dbox_uidlist_add_entry(uidlist, entry);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
312 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
313 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
314 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
315
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
316 static int dbox_uidlist_read(struct dbox_uidlist *uidlist)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
317 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
318 struct mail_storage *storage = STORAGE(uidlist->mbox->storage);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
319 const char *line;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
320 unsigned int uid_validity, last_uid, last_file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
321 struct istream *input;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
322 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
323 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
325 if (uidlist->fd != -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
326 if (stat(uidlist->path, &st) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327 if (errno != ENOENT) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
328 mail_storage_set_critical(storage,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
329 "stat(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
330 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
331 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
333 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
335 if (st.st_ino == uidlist->ino &&
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 st.st_mtime == uidlist->mtime) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 /* unchanged */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 return 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
341
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
342 uidlist->mtime = -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
343 if (uidlist->fd != -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
344 if (close(uidlist->fd) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
345 i_error("close(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
346 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
348 uidlist->fd = open(uidlist->path, O_RDWR);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349 if (uidlist->fd == -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
350 if (errno == ENOENT)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353 mail_storage_set_critical(storage,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354 "open(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
355 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
356 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
358 if (fstat(uidlist->fd, &st) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359 mail_storage_set_critical(storage,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
360 "fstat(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
362 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363 uidlist->ino = st.st_ino;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364 uidlist->mtime = st.st_mtime;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
366 input = i_stream_create_file(uidlist->fd, default_pool, 65536, FALSE);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
368 /* read header: <version> <uidvalidity> <next-uid>.
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
369 Note that <next-uid> may be updated by UID lines, so it can't be
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
370 used directly. */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
371 line = i_stream_read_next_line(input);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
372 if (line == NULL || sscanf(line, "%u %u %u %u", &uidlist->version,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
373 &uid_validity, &last_uid,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374 &last_file_seq) != 4 ||
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
375 uidlist->version != DBOX_UIDLIST_VERSION) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
376 mail_storage_set_critical(storage,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
377 "Corrupted header in file %s (version = %u)",
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
378 uidlist->path, uidlist->version);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
380 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
381 uint32_t old_last_uid, old_last_file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
383 old_last_uid = uidlist->uid_validity == uid_validity ?
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
384 uidlist->last_uid : 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 old_last_file_seq = uidlist->uid_validity == uid_validity ?
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386 uidlist->last_file_seq : 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
387
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
388 uidlist->uid_validity = uid_validity;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
389 uidlist->last_uid = last_uid;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
390 uidlist->last_file_seq = last_file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
391 uidlist->entry_last_file_seq = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
392 p_clear(uidlist->entry_pool);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
393 array_clear(&uidlist->entries);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
394
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
395 ret = 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
396 while ((line = i_stream_read_next_line(input)) != NULL) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
397 if (!dbox_uidlist_next(uidlist, line)) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
398 ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
399 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
400 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
401 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
402
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
403 if (ret > 0 && uidlist->last_uid < old_last_uid) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
404 mail_storage_set_critical(storage,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
405 "%s: last_uid was lowered (%u -> %u)",
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
406 uidlist->path, old_last_uid, uidlist->last_uid);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
407 ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
408 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
409 if (ret > 0 && uidlist->last_file_seq < old_last_file_seq) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
410 mail_storage_set_critical(storage,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
411 "%s: last_uid was lowered (%u -> %u)",
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
412 uidlist->path, old_last_file_seq,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
413 uidlist->last_file_seq);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
414 ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
415 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
416
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
417 if (uidlist->file_seq_highwater < uidlist->last_file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
418 uidlist->file_seq_highwater = uidlist->last_file_seq;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
419 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
420
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
421 if (ret == 0) {
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
422 /* broken file */
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
423 (void)unlink(uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
424
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
425 if (close(uidlist->fd) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
426 i_error("close(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
427 uidlist->fd = -1;
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
428 uidlist->mtime = -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
429 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
430
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 4056
diff changeset
431 i_stream_destroy(&input);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
432 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
433 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
434
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
435 static int dbox_uidlist_lock(struct dbox_uidlist *uidlist)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
436 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
437 i_assert(uidlist->lock_fd == -1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
438
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439 uidlist->lock_fd = file_dotlock_open(&uidlist_dotlock_settings,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
440 uidlist->path, 0,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
441 &uidlist->dotlock);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
442 if (uidlist->lock_fd == -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
443 mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
444 "file_dotlock_open(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
445 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
446 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
447
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
448 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
449 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
450
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
451 static void dbox_uidlist_unlock(struct dbox_uidlist *uidlist)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
452 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
453 i_assert(uidlist->lock_fd != -1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
454
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
455 (void)file_dotlock_delete(&uidlist->dotlock);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
456 uidlist->lock_fd = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
457 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
458
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
459 static struct dbox_uidlist_entry *
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
460 dbox_uidlist_entry_lookup_int(struct dbox_uidlist *uidlist, uint32_t file_seq,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
461 unsigned int *idx_r)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
462 {
3756
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
463 struct dbox_uidlist_entry *const *entries, **entry;
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
464 unsigned int count;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
465
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
466 entries = array_get(&uidlist->entries, &count);
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
467 entry = bsearch(&file_seq, entries, count, sizeof(*entries),
3756
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
468 dbox_uidlist_entry_cmp);
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
469 if (entry == NULL)
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
470 return NULL;
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
471
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
472 *idx_r = entry - entries;
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
473 return *entry;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
474 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
475
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
476 struct dbox_uidlist_entry *
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
477 dbox_uidlist_entry_lookup(struct dbox_uidlist *uidlist, uint32_t file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
478 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
479 unsigned int idx;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
480
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
481 return dbox_uidlist_entry_lookup_int(uidlist, file_seq, &idx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
482 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
483
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
484 static time_t get_min_timestamp(unsigned int days)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
485 {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
486 struct tm tm;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
487 time_t stamp;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
488
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
489 if (days == 0)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
490 return 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
491
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
492 /* get beginning of today */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
493 tm = *localtime(&ioloop_time);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
494 tm.tm_hour = 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
495 tm.tm_min = 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
496 tm.tm_sec = 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
497 stamp = mktime(&tm);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
498 if (stamp == (time_t)-1)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
499 i_panic("mktime(today) failed");
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
500
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
501 return stamp - (3600*24 * (days-1));
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
502 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
503
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
504 struct dbox_uidlist_append_ctx *
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
505 dbox_uidlist_append_init(struct dbox_uidlist *uidlist)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
506 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
507 struct dbox_uidlist_append_ctx *ctx;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
508 pool_t pool;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
509
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
510 i_assert(!uidlist->appending);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
511
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
512 pool = pool_alloconly_create("dbox uidlist append context", 4096);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
513 ctx = p_new(pool, struct dbox_uidlist_append_ctx, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
514 ctx->pool = pool;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
515 ctx->uidlist = uidlist;
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
516 ctx->min_usable_timestamp =
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
517 get_min_timestamp(uidlist->mbox->rotate_days);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
518 ARRAY_CREATE(&ctx->files, pool, struct dbox_save_file *, 16);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
519 return ctx;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
520 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
521
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
522 static int dbox_uidlist_full_rewrite(struct dbox_uidlist *uidlist)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
523 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
524 struct dbox_uidlist_entry *const *entries;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
525 struct ostream *output;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
526 struct stat st, st2;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
527 const char *lock_path;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
528 const struct seq_range *range;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
529 string_t *str;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
530 unsigned int i, count, ui, range_count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
531 int ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
532
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
533 i_assert(uidlist->lock_fd != -1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
534
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
535 output = o_stream_create_file(uidlist->lock_fd, default_pool, 0, FALSE);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
536
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
537 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
538 str = t_str_new(256);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
539
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
540 /* header: <version> <uidvalidity> <next-uid>. */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
541 str_printfa(str, "%u %u %u %u\n", DBOX_UIDLIST_VERSION,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
542 uidlist->uid_validity, uidlist->last_uid,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
543 uidlist->last_file_seq);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
544 o_stream_send(output, str_data(str), str_len(str));
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
545
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
546 entries = array_get(&uidlist->entries, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
547 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
548 str_truncate(str, 0);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
549
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
550 /* <uid list> <file seq> [<last write timestamp> <file size>] */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
551 range = array_get(&entries[i]->uid_list, &range_count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
552 i_assert(range_count != 0);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
553 for (ui = 0; ui < range_count; ui++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
554 if (str_len(str) > 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
555 str_append_c(str, ',');
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
556 if (range[ui].seq1 == range[ui].seq2)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
557 str_printfa(str, "%u", range[ui].seq1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
558 else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
559 str_printfa(str, "%u-%u",
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
560 range[ui].seq1, range[ui].seq2);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
561 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
562 }
4176
513aa0df39ee dbox fixes
Timo Sirainen <tss@iki.fi>
parents: 4152
diff changeset
563 str_printfa(str, " %u %lu %"PRIuUOFF_T, entries[i]->file_seq,
513aa0df39ee dbox fixes
Timo Sirainen <tss@iki.fi>
parents: 4152
diff changeset
564 (unsigned long)entries[i]->create_time,
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
565 entries[i]->file_size);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
566 str_append_c(str, '\n');
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
567 if (o_stream_send(output, str_data(str), str_len(str)) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
568 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
569 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
570 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
571
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
572 if (output->stream_errno != 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
573 mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
574 "write(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
575 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
576 }
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 4056
diff changeset
577 o_stream_destroy(&output);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
578
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
579 if (ret < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
580 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
581
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
582 /* grow mtime by one if needed to make sure the last write is noticed */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
583 lock_path = file_dotlock_get_lock_path(uidlist->dotlock);
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
584 if (stat(uidlist->path, &st) < 0) {
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
585 if (errno != ENOENT) {
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
586 mail_storage_set_critical(
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
587 STORAGE(uidlist->mbox->storage),
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
588 "stat(%s) failed: %m", uidlist->path);
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
589 return -1;
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
590 }
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
591 st.st_mtime = 0;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
592 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
593 if (fstat(uidlist->lock_fd, &st2) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
594 mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
595 "fstat(%s) failed: %m", lock_path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
596 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
597 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
598
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
599 if (st2.st_mtime <= st.st_mtime) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
600 struct utimbuf ut;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
601
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
602 st2.st_mtime = st.st_mtime + 1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
603 ut.actime = ioloop_time;
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
604 ut.modtime = st2.st_mtime;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
605
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
606 if (utime(lock_path, &ut) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
607 mail_storage_set_critical(
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
608 STORAGE(uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
609 "utime(%s) failed: %m", lock_path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
610 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
611 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
612 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
613
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
614 uidlist->ino = st2.st_ino;
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
615 uidlist->mtime = st2.st_mtime;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
616
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
617 /* now, finish the uidlist update by renaming the lock file to
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
618 uidlist */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
619 uidlist->lock_fd = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
620 if (file_dotlock_replace(&uidlist->dotlock, 0) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
621 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
622
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
623 uidlist->need_full_rewrite = FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
624 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
625 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
626
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
627 static void dbox_uidlist_build_update_line(struct dbox_save_file *save_file,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
628 string_t *str, uint32_t uid_start)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
629 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
630 const unsigned int *seqs;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
631 unsigned int seq, seq_count, start;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
632
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
633 str_truncate(str, 0);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
634
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
635 /* build uidlist string */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
636 seqs = array_get(&save_file->seqs, &seq_count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
637 start = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
638 for (seq = 0; seq < seq_count; seq++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
639 if (seq != seq_count-1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
640 if (seq == 0 || seqs[seq-1]+1 == seqs[seq])
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
641 continue;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
642 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
643
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
644 if (str_len(str) > 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
645 str_append_c(str, ',');
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
646 str_printfa(str, "%u", uid_start + seqs[start] - 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
647 if (seq != start)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
648 str_printfa(str, "-%u", uid_start + seqs[seq] - 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
649 start = seq + 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
650 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
651 str_printfa(str, " %u", save_file->file->file_seq);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
652
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
653 /* add creation time and file size */
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
654 str_printfa(str, " %s %s", dec2str(save_file->file->create_time),
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
655 dec2str(save_file->append_offset));
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
656 str_append_c(str, '\n');
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
657 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
658
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
659 static void dbox_uidlist_update_changes(struct dbox_uidlist_append_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
660 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
661 struct dbox_save_file *const *files;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
662 string_t *str;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
663 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
664 uint32_t uid_start;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
665
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
666 uid_start = ctx->uidlist->last_uid + 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
667
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
668 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
669 str = t_str_new(256);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
670 files = array_get(&ctx->files, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
671 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
672 dbox_uidlist_build_update_line(files[i], str, uid_start);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
673 if (!dbox_uidlist_next(ctx->uidlist, str_c(str)))
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
674 i_panic("dbox_uidlist_next() internal update failed");
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
675 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
676 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
677 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
678
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
679 static int dbox_uidlist_append_changes(struct dbox_uidlist_append_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
680 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
681 struct dbox_save_file *const *files;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
682 struct ostream *output;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
683 struct utimbuf ut;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
684 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
685 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
686 uint32_t uid_start;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
687 string_t *str;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
688 int ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
689
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
690 i_assert(ctx->uidlist->fd != -1);
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
691 i_assert(ctx->uidlist->lock_fd != -1);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
692
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
693 if (lseek(ctx->uidlist->fd, 0, SEEK_END) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
694 mail_storage_set_critical(STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
695 "lseek(%s) failed: %m", ctx->uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
696 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
697 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
698 output = o_stream_create_file(ctx->uidlist->fd, default_pool, 0, FALSE);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
699
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
700 uid_start = ctx->uidlist->last_uid + 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
701
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
702 /* simply append the change-lines to the index file. if someone's
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
703 reading the file at the same time, it doesn't matter. the entries
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
704 are complete only after the LF has been written. */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
705 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
706 str = t_str_new(256);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
707 files = array_get(&ctx->files, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
708 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
709 dbox_uidlist_build_update_line(files[i], str, uid_start);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
710 if (!dbox_uidlist_next(ctx->uidlist, str_c(str)))
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
711 i_panic("dbox_uidlist_next() internal update failed");
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
712 o_stream_send(output, str_data(str), str_len(str));
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
713 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
714 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
715
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
716 if (output->stream_errno != 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
717 mail_storage_set_critical(STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
718 "write(%s) failed: %m", ctx->uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
719 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
720 }
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 4056
diff changeset
721 o_stream_destroy(&output);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
722
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
723 if (ret < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
724 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
725
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
726 /* grow mtime by one to make sure the last write is noticed */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
727 if (fstat(ctx->uidlist->fd, &st) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
728 mail_storage_set_critical(STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
729 "fstat(%s) failed: %m", ctx->uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
730 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
731 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
732
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
733 ut.actime = ioloop_time;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
734 ut.modtime = st.st_mtime + 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
735 if (utime(ctx->uidlist->path, &ut) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
736 mail_storage_set_critical(STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
737 "utime(%s) failed: %m", ctx->uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
738 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
739 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
740
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
741 ctx->uidlist->ino = st.st_ino;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
742 ctx->uidlist->mtime = ut.modtime;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
743 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
744 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
745
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
746 static int
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
747 dbox_uidlist_write_append_offsets(struct dbox_uidlist_append_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
748 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
749 struct dbox_save_file *const *files;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
750 struct dbox_file_header hdr;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
751 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
752 int ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
753
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
754 files = array_get(&ctx->files, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
755 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
756 DEC2HEX(hdr.append_offset_hex,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
757 files[i]->file->output->offset);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
758
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
759 if (pwrite_full(files[i]->file->fd, hdr.append_offset_hex,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
760 sizeof(hdr.append_offset_hex),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
761 offsetof(struct dbox_file_header,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
762 append_offset_hex)) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
763 mail_storage_set_critical(
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
764 STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
765 "pwrite_full(%s) failed: %m",
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
766 files[i]->file->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
767 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
768 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
769 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
770 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
771 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
772
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
773 int dbox_uidlist_append_commit(struct dbox_uidlist_append_ctx *ctx,
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
774 time_t *mtime_r)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
775 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
776 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
777
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
778 if (ctx->mail_count == 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
779 /* nothing actually appended */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
780 dbox_uidlist_append_rollback(ctx);
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
781 *mtime_r = ctx->uidlist->mtime;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
782 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
783 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
784
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
785 i_assert(ctx->locked);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
786
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
787 if (dbox_uidlist_write_append_offsets(ctx) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
788 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
789 else {
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
790 ctx->uidlist->need_full_rewrite = TRUE; // FIXME
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
791 if (ctx->uidlist->need_full_rewrite) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
792 dbox_uidlist_update_changes(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
793 ret = dbox_uidlist_full_rewrite(ctx->uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
794 if (ctx->uidlist->dotlock == NULL)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
795 ctx->locked = FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
796 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
797 ret = dbox_uidlist_append_changes(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
798 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
799 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
800
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
801 *mtime_r = ctx->uidlist->mtime;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
802 dbox_uidlist_append_rollback(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
803 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
804 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
805
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
806 void dbox_uidlist_append_rollback(struct dbox_uidlist_append_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
807 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
808 struct dbox_save_file *const *files;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
809 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
810
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
811 /* unlock files */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
812 files = array_get(&ctx->files, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
813 for (i = 0; i < count; i++)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
814 file_dotlock_delete(&files[i]->dotlock);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
815
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
816 if (ctx->locked)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
817 dbox_uidlist_unlock(ctx->uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
818 ctx->uidlist->appending = FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
819 pool_unref(ctx->pool);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
820 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
821
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
822 static int dbox_reopen_file(struct dbox_uidlist_append_ctx *ctx,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
823 struct dbox_save_file *save_file)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
824 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
825 struct dbox_file *file = save_file->file;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
826 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
827
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
828 if (file->fd != -1)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
829 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
830
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
831 /* open the file and make sure it's the same as expected,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
832 since we have it locked */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
833 file->fd = open(file->path, O_RDWR);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
834 if (file->fd == -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
835 mail_storage_set_critical(STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
836 "open(%s) failed: %m", file->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
837 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
838 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
839
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
840 if (fstat(file->fd, &st) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
841 mail_storage_set_critical(STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
842 "fstat(%s) failed: %m", file->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
843 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
844 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
845
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
846 if (st.st_ino != save_file->ino ||
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
847 !CMP_DEV_T(st.st_dev, save_file->dev)) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
848 mail_storage_set_critical(STORAGE(ctx->uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
849 "Appended file changed unexpectedly: %s", file->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
850 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
851 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
852 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
853 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
854
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
855 static int dbox_uidlist_files_lookup(struct dbox_uidlist_append_ctx *ctx,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
856 uint32_t file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
857 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
858 struct dbox_save_file *const *files;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
859 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
860
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
861 files = array_get(&ctx->files, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
862 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
863 if (files[i]->file->file_seq == file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
864 return TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
865 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
866 return FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
867 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
868
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
869 #define DBOX_CAN_APPEND(ctx, create_time, file_size) \
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
870 (((create_time) >= (ctx)->min_usable_timestamp && \
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
871 (file_size) < (ctx)->uidlist->mbox->rotate_size) || \
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
872 (file_size) < (ctx)->uidlist->mbox->rotate_min_size)
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
873
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
874 static int
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
875 dbox_file_append(struct dbox_uidlist_append_ctx *ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
876 const char *path, struct dbox_uidlist_entry *entry,
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
877 struct stat *st, struct dbox_file **file_r, bool existing)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
878 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
879 struct dbox_mailbox *mbox = ctx->uidlist->mbox;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
880 struct dbox_file *file;
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
881 int fd;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
882
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
883 *file_r = NULL;
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
884
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
885 fd = open(path, O_RDWR | (existing ? 0 : O_CREAT), 0600);
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
886 if (fd == -1) {
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
887 if (errno == ENOENT && existing) {
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
888 /* the file was unlinked just now, update its size
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
889 so that we don't get back here. */
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
890 entry->file_size = (uoff_t)-1;
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
891 return 0;
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
892 }
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
893 mail_storage_set_critical(STORAGE(mbox->storage),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
894 "open(%s) failed: %m", path);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
895 return -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
896 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
897
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4114
diff changeset
898 if (fstat(fd, st) < 0) {
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
899 mail_storage_set_critical(STORAGE(mbox->storage),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
900 "fstat(%s) failed: %m", path);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
901 (void)close(fd);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
902 return -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
903 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
904
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
905 file = i_new(struct dbox_file, 1);
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
906 file->path = i_strdup(path);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
907 file->fd = fd;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
908
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
909 file->input = i_stream_create_file(file->fd, default_pool,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
910 65536, FALSE);
3956
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
911 file->output = o_stream_create_file(file->fd, default_pool, 0, FALSE);
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4114
diff changeset
912 if ((uoff_t)st->st_size < sizeof(struct dbox_file_header)) {
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
913 if (dbox_file_write_header(mbox, file) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
914 dbox_file_close(file);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
915 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
916 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
917 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
918 if (dbox_file_read_header(mbox, file) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
919 dbox_file_close(file);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
920 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
921 }
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
922
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
923 if (entry != NULL) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
924 entry->create_time = file->create_time;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
925 entry->file_size = file->append_offset;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
926 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
927
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
928 if (!DBOX_CAN_APPEND(ctx, file->create_time,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
929 file->append_offset)) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
930 dbox_file_close(file);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
931 return 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
932 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
933 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
934
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
935 *file_r = file;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
936 return 1;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
937 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
938
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
939 static int
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
940 dbox_file_append_lock(struct dbox_uidlist_append_ctx *ctx, string_t *path,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
941 uint32_t *file_seq_r, struct dbox_uidlist_entry **entry_r,
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
942 struct dotlock **dotlock_r, bool *existing_r)
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
943 {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
944 struct dbox_mailbox *mbox = ctx->uidlist->mbox;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
945 struct dbox_uidlist_entry *const *entries;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
946 unsigned int i, count;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
947 uint32_t file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
948 int ret;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
949
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
950 entries = array_get(&ctx->uidlist->entries, &count);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
951 for (i = 0;; i++) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
952 file_seq = 0;
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
953 *existing_r = FALSE;
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
954 for (; i < count; i++) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
955 if (DBOX_CAN_APPEND(ctx, entries[i]->create_time,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
956 entries[i]->file_size) &&
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
957 !dbox_uidlist_files_lookup(ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
958 entries[i]->file_seq)) {
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
959 *existing_r = TRUE;
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
960 file_seq = entries[i]->file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
961 break;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
962 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
963 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
964
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
965 if (file_seq == 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
966 /* create new file */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
967 file_seq = dbox_uidlist_get_new_file_seq(ctx->uidlist);
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
968 }
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
969
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
970 /* try locking the file. */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
971 str_truncate(path, 0);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
972 str_printfa(path, "%s/"DBOX_MAILDIR_NAME"/"
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
973 DBOX_MAIL_FILE_FORMAT, mbox->path, file_seq);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
974 ret = file_dotlock_create(&dbox_file_dotlock_set, str_c(path),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
975 DOTLOCK_CREATE_FLAG_NONBLOCK,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
976 dotlock_r);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
977 if (ret > 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
978 /* success */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
979 break;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
980 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
981 if (ret < 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
982 mail_storage_set_critical(STORAGE(mbox->storage),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
983 "file_dotlock_create(%s) failed: %m",
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
984 str_c(path));
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
985 return -1;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
986 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
987
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
988 /* lock already exists, try next file */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
989 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
990
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
991 *file_seq_r = file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
992 *entry_r = i < count ? entries[i] : NULL;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
993 return 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
994 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
995
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
996 int dbox_uidlist_append_locked(struct dbox_uidlist_append_ctx *ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
997 struct dbox_file **file_r)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
998 {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
999 struct dbox_save_file *const *files, *save_file;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1000 struct dbox_uidlist_entry *entry;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1001 struct dbox_file *file = NULL;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1002 struct dotlock *dotlock = NULL;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1003 struct ostream *output;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1004 string_t *path;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1005 unsigned int i, count;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1006 struct stat st;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1007 uint32_t file_seq;
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
1008 bool existing;
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1009 int ret;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1010
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1011 /* check first from already opened files */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1012 files = array_get(&ctx->files, &count);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1013 for (i = 0; i < count; i++) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1014 if (DBOX_CAN_APPEND(ctx, files[i]->file->create_time,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1015 files[i]->append_offset)) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1016 if (dbox_reopen_file(ctx, files[i]) < 0)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1017 return -1;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1018
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1019 *file_r = file = files[i]->file;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1020 o_stream_seek(file->output, file->append_offset);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1021 return 0;
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
1022 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1023 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1024
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1025 /* check from other existing files. use uidlist's file_size field.
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1026 it's not completely trustworthy though. */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1027 path = str_new(ctx->pool, 64);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1028 do {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1029 if (dotlock != NULL)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1030 file_dotlock_delete(&dotlock);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1031 if (dbox_file_append_lock(ctx, path, &file_seq,
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
1032 &entry, &dotlock, &existing) < 0)
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1033 return -1;
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4114
diff changeset
1034 } while ((ret = dbox_file_append(ctx, str_c(path), entry,
4208
Timo Sirainen <tss@iki.fi>
parents: 4176
diff changeset
1035 &st, &file, existing)) == 0);
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1036
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1037 if (ret < 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1038 file_dotlock_delete(&dotlock);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1039 dbox_file_close(file);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1040 return -1;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1041 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1042 file->file_seq = file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1043
3956
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1044 /* we'll always use CRLF linefeeds for mails (but not the header,
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1045 so don't do this before dbox_file_write_header()) */
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1046 output = o_stream_create_crlf(default_pool, file->output);
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1047 o_stream_unref(&file->output);
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1048 file->output = output;
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1049
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1050 o_stream_seek(file->output, file->append_offset);
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1051
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
1052 save_file = p_new(ctx->pool, struct dbox_save_file, 1);
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
1053 save_file->file = file;
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
1054 save_file->dotlock = dotlock;
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
1055 save_file->dev = st.st_dev;
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
1056 save_file->ino = st.st_ino;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1057 ARRAY_CREATE(&save_file->seqs, ctx->pool, unsigned int, 8);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1058
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1059 array_append(&ctx->files, &save_file, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1060 *file_r = file;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1061 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1062 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1063
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1064 void dbox_uidlist_append_finish_mail(struct dbox_uidlist_append_ctx *ctx,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1065 struct dbox_file *file)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1066 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1067 struct dbox_save_file *const *files, *save_file = NULL;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1068 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1069
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1070 files = array_get(&ctx->files, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1071 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1072 if (files[i]->file == file) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1073 save_file = files[i];
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1074 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1075 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1076 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1077 i_assert(save_file != NULL);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1078
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1079 ctx->mail_count++;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1080 array_append(&save_file->seqs, &ctx->mail_count, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1081
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1082 file->append_offset = file->output->offset;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1083 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1084
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1085 struct dbox_file *
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1086 dbox_uidlist_append_lookup_file(struct dbox_uidlist_append_ctx *ctx,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1087 uint32_t file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1088 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1089 struct dbox_save_file *const *files;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1090 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1091
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1092 files = array_get(&ctx->files, &count);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1093 for (i = 0; i < count; i++) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1094 if (files[i]->file->file_seq == file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1095 return files[i]->file;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1096 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1097
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1098 i_unreached();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1099 return NULL;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1100 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1101
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1102 uint32_t dbox_uidlist_get_new_file_seq(struct dbox_uidlist *uidlist)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1103 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1104 /* Note that unless uidlist is locked, it's not guaranteed that this
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1105 actually returns a new unused file sequence. */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1106 return ++uidlist->file_seq_highwater;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1107 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1108
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1109 int dbox_uidlist_append_get_first_uid(struct dbox_uidlist_append_ctx *ctx,
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
1110 uint32_t *uid_r, time_t *mtime_r)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1111 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1112 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1113
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1114 /* from now on we'll need to keep uidlist locked until it's
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1115 committed or rollbacked */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1116 if (!ctx->locked) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1117 if (dbox_uidlist_lock(ctx->uidlist) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1118 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1119 ctx->locked = TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1120
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1121 /* update uidlist to make sure we have the latest state */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1122 if ((ret = dbox_uidlist_read(ctx->uidlist)) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1123 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1124 if (ret == 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1125 /* file is deleted */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1126 ctx->uidlist->need_full_rewrite = TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1127 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1128 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1129
3957
8f0ff62befd3 When appending, update sync_stamp in index so that dbox won't get a full
Timo Sirainen <tss@iki.fi>
parents: 3956
diff changeset
1130 *mtime_r = ctx->uidlist->mtime;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1131 *uid_r = ctx->uidlist->last_uid + 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1132 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1133 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1134
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1135 int dbox_uidlist_sync_init(struct dbox_uidlist *uidlist,
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1136 struct dbox_uidlist_sync_ctx **ctx_r,
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1137 time_t *mtime_r)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1138 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1139 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1140
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1141 *mtime_r = -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1142 if (dbox_uidlist_lock(uidlist) < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1143 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1144
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1145 if ((ret = dbox_uidlist_read(uidlist)) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1146 dbox_uidlist_unlock(uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1147 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1148 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1149
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1150 if (ret == 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1151 /* file is deleted */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1152 uidlist->need_full_rewrite = TRUE;
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1153 } else {
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1154 *mtime_r = uidlist->mtime;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1155 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1156
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1157 *ctx_r = i_new(struct dbox_uidlist_sync_ctx, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1158 (*ctx_r)->uidlist = uidlist;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1159 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1160 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1161
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1162 int dbox_uidlist_sync_commit(struct dbox_uidlist_sync_ctx *ctx, time_t *mtime_r)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1163 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1164 int ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1165
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1166 if (ctx->modified) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1167 /* this call may or may not release the dotlock.. */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1168 ret = dbox_uidlist_full_rewrite(ctx->uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1169 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1170
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1171 *mtime_r = ctx->uidlist->mtime;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1172
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1173 if (ctx->uidlist->dotlock != NULL)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1174 dbox_uidlist_unlock(ctx->uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1175 i_free(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1176 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1177 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1178
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1179 void dbox_uidlist_sync_rollback(struct dbox_uidlist_sync_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1180 {
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1181 array_clear(&ctx->uidlist->entries);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1182 ctx->uidlist->ino = 0;
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1183 ctx->uidlist->mtime = 0;
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1184
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1185 dbox_uidlist_unlock(ctx->uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1186 i_free(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1187 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1188
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1189 void dbox_uidlist_sync_from_scratch(struct dbox_uidlist_sync_ctx *ctx)
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1190 {
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1191 array_clear(&ctx->uidlist->entries);
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1192 ctx->uidlist->ino = 0;
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1193 ctx->uidlist->mtime = 0;
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1194
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1195 ctx->modified = TRUE;
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1196 ctx->uidlist->need_full_rewrite = TRUE;
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1197 }
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1198
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1199 void dbox_uidlist_sync_set_modified(struct dbox_uidlist_sync_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1200 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1201 ctx->modified = TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1202 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1203
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1204 void dbox_uidlist_sync_append(struct dbox_uidlist_sync_ctx *ctx,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1205 const struct dbox_uidlist_entry *entry)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1206 {
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1207 struct dbox_uidlist_entry *const *entries, **pos;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1208 struct dbox_uidlist_entry *new_entry;
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1209 unsigned int count;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1210
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
1211 i_assert(array_count(&entry->uid_list) > 0);
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
1212
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1213 new_entry = p_new(ctx->uidlist->entry_pool,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1214 struct dbox_uidlist_entry, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1215 *new_entry = *entry;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1216
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1217 ARRAY_CREATE(&new_entry->uid_list, ctx->uidlist->entry_pool,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1218 struct seq_range, array_count(&entry->uid_list) + 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1219 array_append_array(&new_entry->uid_list, &entry->uid_list);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1220
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1221 entries = array_get(&ctx->uidlist->entries, &count);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1222 if (count == 0 || entries[count-1]->file_seq < new_entry->file_seq)
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1223 array_append(&ctx->uidlist->entries, &new_entry, 1);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1224 else {
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1225 pos = bsearch_insert_pos(&new_entry->file_seq, entries,
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1226 count, sizeof(*entries),
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1227 dbox_uidlist_entry_cmp);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1228 array_insert(&ctx->uidlist->entries, pos - entries,
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1229 &new_entry, 1);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1230 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1231 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1232
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1233 void dbox_uidlist_sync_unlink(struct dbox_uidlist_sync_ctx *ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1234 uint32_t file_seq)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1235 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1236 struct dbox_uidlist_entry *entry;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1237 unsigned int idx;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1238
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1239 entry = dbox_uidlist_entry_lookup_int(ctx->uidlist, file_seq, &idx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1240 i_assert(entry != NULL);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1241
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1242 array_delete(&ctx->uidlist->entries, idx, 1);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1243
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1244 dbox_uidlist_sync_set_modified(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1245 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1246
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1247 uint32_t dbox_uidlist_sync_get_uid_validity(struct dbox_uidlist_sync_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1248 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1249 if (ctx->uidlist->uid_validity == 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1250 ctx->uidlist->uid_validity = ioloop_time;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1251 ctx->modified = TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1252 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1253
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1254 return ctx->uidlist->uid_validity;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1255 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1256
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1257 uint32_t dbox_uidlist_sync_get_next_uid(struct dbox_uidlist_sync_ctx *ctx)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1258 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1259 return ctx->uidlist->last_uid + 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1260 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1261
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1262 int dbox_uidlist_get_mtime(struct dbox_uidlist *uidlist, time_t *mtime_r)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1263 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1264 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1265
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1266 if (stat(uidlist->path, &st) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1267 if (errno != ENOENT) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1268 mail_storage_set_critical(
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1269 STORAGE(uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1270 "stat(%s) failed: %m", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1271 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1272 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1273
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1274 *mtime_r = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1275 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1276 *mtime_r = st.st_mtime;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1277 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1278 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1279 }