annotate src/lib-storage/index/dbox/dbox-uidlist.c @ 4176:513aa0df39ee HEAD

dbox fixes
author Timo Sirainen <tss@iki.fi>
date Wed, 12 Apr 2006 22:24:57 +0300
parents e2edd333c473
children 67f6d3afa5a5
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;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224 uint32_t digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227 /* <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
228 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229 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
230 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
231 struct seq_range, 8);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
232
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233 /* get uid list */
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
234 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
235 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
236 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
237 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
238 else {
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
239 if (range.seq1 == 0) {
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
240 if (digit <= range.seq2) {
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
241 /* broken */
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
242 array_clear(&entry->uid_list);
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
243 break;
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
244 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245 range.seq1 = digit;
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
246 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247 if (*line == ',' || *line == ' ') {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
248 if (range.seq1 > digit) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
249 /* broken */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250 array_clear(&entry->uid_list);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 range.seq2 = digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254 array_append(&entry->uid_list, &range, 1);
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
255 range.seq1 = 0;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257 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
258 /* 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
259 uidlist->last_uid = digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
260 }
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 (*line == ' ')
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265 digit = 0;
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 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
269 if (*line != ' ' || array_count(&entry->uid_list) == 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
270 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
271 "%s: Corrupted entry", uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273 return FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 /* get file seq */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 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
278 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
279 entry->file_seq = digit;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
281 /* get create timestamp */
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
282 line++;
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
283 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
284 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
285
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
286 if (*line != ' ') {
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
287 mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
288 "%s: Corrupted entry", uidlist->path);
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
289
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
290 t_pop();
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
291 return FALSE;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 }
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
293 /* get file size */
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
294 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
295 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
296
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297 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
298 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
301
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 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
303 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304 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
305 const char *line;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 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
307 struct istream *input;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309 int ret;
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 if (uidlist->fd != -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
312 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
313 if (errno != ENOENT) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
314 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
315 "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
316 return -1;
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 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
319 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
320
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
321 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
322 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
323 /* unchanged */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324 return 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
325 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
326 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327
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
328 uidlist->mtime = -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
329 if (uidlist->fd != -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
330 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
331 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
332 }
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 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
335 if (uidlist->fd == -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 if (errno == ENOENT)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 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
340 "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
341 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
342 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
343
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
344 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
345 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
346 "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
347 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
348 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349 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
350 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
351
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352 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
353
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354 /* 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
355 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
356 used directly. */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357 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
358 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
359 &uid_validity, &last_uid,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
360 &last_file_seq) != 4 ||
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361 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
362 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
363 "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
364 uidlist->path, uidlist->version);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365 ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
366 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367 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
368
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
369 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
370 uidlist->last_uid : 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
371 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
372 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
373
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374 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
375 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
376 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
377 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
378 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
379 array_clear(&uidlist->entries);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
380
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
381 ret = 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382 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
383 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
384 ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386 }
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
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
389 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
390 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
391 "%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
392 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
393 ret = 0;
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 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
396 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
397 "%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
398 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
399 uidlist->last_file_seq);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
400 ret = 0;
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 (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
404 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
405 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
406
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
407 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
408 /* broken file */
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
409 (void)unlink(uidlist->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
410
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
411 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
412 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
413 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
414 uidlist->mtime = -1;
3720
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
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 4056
diff changeset
417 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
418 return ret;
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 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
422 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
423 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
424
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
425 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
426 uidlist->path, 0,
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
427 &uidlist->dotlock);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
428 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
429 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
430 "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
431 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
432 }
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 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
435 }
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 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
438 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439 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
440
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
441 (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
442 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 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
444
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
445 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
446 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
447 unsigned int *idx_r)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
448 {
3756
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
449 struct dbox_uidlist_entry *const *entries, **entry;
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
450 unsigned int count;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
451
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
452 entries = array_get(&uidlist->entries, &count);
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
453 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
454 dbox_uidlist_entry_cmp);
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
455 if (entry == NULL)
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
456 return NULL;
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
457
5fcf8e16ba8c Use binary search for finding entries
Timo Sirainen <tss@iki.fi>
parents: 3755
diff changeset
458 *idx_r = entry - entries;
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
459 return *entry;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
460 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
461
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
462 struct dbox_uidlist_entry *
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
463 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
464 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
465 unsigned int idx;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
466
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
467 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
468 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
469
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
470 static time_t get_min_timestamp(unsigned int days)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
471 {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
472 struct tm tm;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
473 time_t stamp;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
474
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
475 if (days == 0)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
476 return 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
477
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
478 /* get beginning of today */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
479 tm = *localtime(&ioloop_time);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
480 tm.tm_hour = 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
481 tm.tm_min = 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
482 tm.tm_sec = 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
483 stamp = mktime(&tm);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
484 if (stamp == (time_t)-1)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
485 i_panic("mktime(today) failed");
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
486
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
487 return stamp - (3600*24 * (days-1));
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
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
490 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
491 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
492 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
493 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
494 pool_t pool;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
495
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
496 i_assert(!uidlist->appending);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
497
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
498 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
499 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
500 ctx->pool = pool;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
501 ctx->uidlist = uidlist;
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
502 ctx->min_usable_timestamp =
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
503 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
504 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
505 return ctx;
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
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
508 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
509 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
510 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
511 struct ostream *output;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
512 struct stat st, st2;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
513 const char *lock_path;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
514 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
515 string_t *str;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
516 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
517 int ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
518
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
519 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
520
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
521 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
522
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
523 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
524 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
525
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
526 /* 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
527 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
528 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
529 uidlist->last_file_seq);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
530 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
531
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
532 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
533 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
534 str_truncate(str, 0);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
535
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
536 /* <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
537 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
538 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
539 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
540 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
541 str_append_c(str, ',');
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
542 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
543 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
544 else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
545 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
546 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
547 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
548 }
4176
513aa0df39ee dbox fixes
Timo Sirainen <tss@iki.fi>
parents: 4152
diff changeset
549 str_printfa(str, " %u %lu %"PRIuUOFF_T, entries[i]->file_seq,
513aa0df39ee dbox fixes
Timo Sirainen <tss@iki.fi>
parents: 4152
diff changeset
550 (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
551 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
552 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
553 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
554 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
555 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
556 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
557
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
558 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
559 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
560 "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
561 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
562 }
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 4056
diff changeset
563 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
564
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
565 if (ret < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
566 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
567
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
568 /* 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
569 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
570 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
571 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
572 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
573 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
574 "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
575 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
576 }
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
577 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
578 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
579 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
580 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
581 "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
582 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
583 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
584
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
585 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
586 struct utimbuf ut;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
587
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
588 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
589 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
590 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
591
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
592 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
593 mail_storage_set_critical(
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
594 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 "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
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
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
600 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
601 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
602
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
603 /* 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
604 uidlist */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
605 uidlist->lock_fd = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
606 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
607 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
608
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
609 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
610 return 0;
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 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
614 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
615 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
616 const unsigned int *seqs;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
617 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
618
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
619 str_truncate(str, 0);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
620
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
621 /* build uidlist string */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
622 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
623 start = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
624 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
625 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
626 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
627 continue;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
628 }
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 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
631 str_append_c(str, ',');
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
632 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
633 if (seq != start)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
634 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
635 start = seq + 1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
636 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
637 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
638
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
639 /* 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
640 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
641 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
642 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
643 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
644
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
645 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
646 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
647 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
648 string_t *str;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
649 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
650 uint32_t uid_start;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
651
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
652 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
653
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
654 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
655 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
656 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
657 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
658 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
659 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
660 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
661 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
662 t_pop();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
663 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
664
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
665 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
666 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
667 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
668 struct ostream *output;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
669 struct utimbuf ut;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
670 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
671 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
672 uint32_t uid_start;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
673 string_t *str;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
674 int ret = 0;
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 i_assert(ctx->uidlist->fd != -1);
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
677 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
678
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
679 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
680 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
681 "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
682 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
683 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
684 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
685
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
686 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
687
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
688 /* 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
689 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
690 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
691 t_push();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
692 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
693 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
694 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
695 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
696 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
697 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
698 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
699 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
700 t_pop();
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 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
703 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
704 "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
705 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
706 }
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 4056
diff changeset
707 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
708
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
709 if (ret < 0)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
710 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
711
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
712 /* 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
713 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
714 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
715 "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
716 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
717 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
718
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
719 ut.actime = ioloop_time;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
720 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
721 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
722 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
723 "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
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
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
727 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
728 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
729 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
730 }
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 static int
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
733 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
734 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
735 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
736 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
737 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
738 int ret = 0;
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 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
741 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
742 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
743 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
744
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
745 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
746 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
747 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
748 append_offset_hex)) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
749 mail_storage_set_critical(
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
750 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
751 "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
752 files[i]->file->path);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
753 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
754 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
755 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
756 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
757 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
758
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
759 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
760 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
761 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
762 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
763
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
764 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
765 /* nothing actually appended */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
766 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
767 *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
768 return 0;
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
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
771 i_assert(ctx->locked);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
772
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
773 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
774 ret = -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
775 else {
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
776 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
777 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
778 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
779 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
780 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
781 ctx->locked = FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
782 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
783 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
784 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
785 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
786
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
787 *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
788 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
789 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
790 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
791
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
792 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
793 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
794 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
795 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
796
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
797 /* unlock files */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
798 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
799 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
800 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
801
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
802 if (ctx->locked)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
803 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
804 ctx->uidlist->appending = FALSE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
805 pool_unref(ctx->pool);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
806 }
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 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
809 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
810 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
811 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
812 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
813
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
814 if (file->fd != -1)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
815 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
816
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
817 /* 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
818 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
819 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
820 if (file->fd == -1) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
821 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
822 "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
823 return -1;
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
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
826 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
827 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
828 "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
829 return -1;
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
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
832 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
833 !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
834 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
835 "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
836 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
837 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
838 return 0;
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
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
841 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
842 uint32_t file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
843 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
844 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
845 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
846
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
847 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
848 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
849 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
850 return TRUE;
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 FALSE;
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
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
855 #define DBOX_CAN_APPEND(ctx, create_time, file_size) \
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
856 (((create_time) >= (ctx)->min_usable_timestamp && \
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
857 (file_size) < (ctx)->uidlist->mbox->rotate_size) || \
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
858 (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
859
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
860 static int
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
861 dbox_file_append(struct dbox_uidlist_append_ctx *ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
862 const char *path, struct dbox_uidlist_entry *entry,
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4114
diff changeset
863 struct stat *st, struct dbox_file **file_r)
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
864 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
865 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
866 struct dbox_file *file;
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
867 int fd;
3720
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 *file_r = NULL;
3812
2881f7e79098 Added rotation by number of days since file was created.
Timo Sirainen <tss@iki.fi>
parents: 3761
diff changeset
870
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
871 fd = open(path, O_CREAT | O_RDWR, 0600);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
872 if (fd == -1) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
873 mail_storage_set_critical(STORAGE(mbox->storage),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
874 "open(%s) failed: %m", path);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
875 return -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
876 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
877
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4114
diff changeset
878 if (fstat(fd, st) < 0) {
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
879 mail_storage_set_critical(STORAGE(mbox->storage),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
880 "fstat(%s) failed: %m", path);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
881 (void)close(fd);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
882 return -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
883 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
884
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
885 file = i_new(struct dbox_file, 1);
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
886 file->path = i_strdup(path);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
887 file->fd = fd;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
888
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
889 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
890 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
891 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
892 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
893 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
894 dbox_file_close(file);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
895 return -1;
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 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
898 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
899 dbox_file_close(file);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
900 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
901 }
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
902
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
903 if (entry != NULL) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
904 entry->create_time = file->create_time;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
905 entry->file_size = file->append_offset;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
906 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
907
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
908 if (!DBOX_CAN_APPEND(ctx, file->create_time,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
909 file->append_offset)) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
910 dbox_file_close(file);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
911 return 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
912 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
913 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
914
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
915 *file_r = file;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
916 return 1;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
917 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
918
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
919 static int
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
920 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
921 uint32_t *file_seq_r, struct dbox_uidlist_entry **entry_r,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
922 struct dotlock **dotlock_r)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
923 {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
924 struct dbox_mailbox *mbox = ctx->uidlist->mbox;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
925 struct dbox_uidlist_entry *const *entries;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
926 unsigned int i, count;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
927 uint32_t file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
928 int ret;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
929
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
930 entries = array_get(&ctx->uidlist->entries, &count);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
931 for (i = 0;; i++) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
932 file_seq = 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
933 for (; i < count; i++) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
934 if (DBOX_CAN_APPEND(ctx, entries[i]->create_time,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
935 entries[i]->file_size) &&
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
936 !dbox_uidlist_files_lookup(ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
937 entries[i]->file_seq)) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
938 file_seq = entries[i]->file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
939 break;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
940 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
941 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
942
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
943 if (file_seq == 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
944 /* create new file */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
945 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
946 }
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
947
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
948 /* try locking the file. */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
949 str_truncate(path, 0);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
950 str_printfa(path, "%s/"DBOX_MAILDIR_NAME"/"
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
951 DBOX_MAIL_FILE_FORMAT, mbox->path, file_seq);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
952 ret = file_dotlock_create(&dbox_file_dotlock_set, str_c(path),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
953 DOTLOCK_CREATE_FLAG_NONBLOCK,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
954 dotlock_r);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
955 if (ret > 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
956 /* success */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
957 break;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
958 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
959 if (ret < 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
960 mail_storage_set_critical(STORAGE(mbox->storage),
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
961 "file_dotlock_create(%s) failed: %m",
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
962 str_c(path));
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
963 return -1;
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
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
966 /* lock already exists, try next file */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
967 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
968
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
969 *file_seq_r = file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
970 *entry_r = i < count ? entries[i] : NULL;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
971 return 0;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
972 }
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
973
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
974 int dbox_uidlist_append_locked(struct dbox_uidlist_append_ctx *ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
975 struct dbox_file **file_r)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
976 {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
977 struct dbox_save_file *const *files, *save_file;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
978 struct dbox_uidlist_entry *entry;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
979 struct dbox_file *file = NULL;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
980 struct dotlock *dotlock = NULL;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
981 struct ostream *output;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
982 string_t *path;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
983 unsigned int i, count;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
984 struct stat st;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
985 uint32_t file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
986 int ret;
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 /* check first from already opened files */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
989 files = array_get(&ctx->files, &count);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
990 for (i = 0; i < count; i++) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
991 if (DBOX_CAN_APPEND(ctx, files[i]->file->create_time,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
992 files[i]->append_offset)) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
993 if (dbox_reopen_file(ctx, files[i]) < 0)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
994 return -1;
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 *file_r = file = files[i]->file;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
997 o_stream_seek(file->output, file->append_offset);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
998 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
999 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1000 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1001
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1002 /* check from other existing files. use uidlist's file_size field.
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1003 it's not completely trustworthy though. */
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1004 path = str_new(ctx->pool, 64);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1005 do {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1006 if (dotlock != NULL)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1007 file_dotlock_delete(&dotlock);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1008 if (dbox_file_append_lock(ctx, path, &file_seq,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1009 &entry, &dotlock) < 0)
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1010 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
1011 } while ((ret = dbox_file_append(ctx, str_c(path), entry,
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4114
diff changeset
1012 &st, &file)) == 0);
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1013
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1014 if (ret < 0) {
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1015 file_dotlock_delete(&dotlock);
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1016 dbox_file_close(file);
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->file_seq = file_seq;
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1020
3956
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1021 /* 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
1022 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
1023 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
1024 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
1025 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
1026
a52c36c51ff2 Support storing keywords in dbox files. Doesn't yet work while saving.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
1027 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
1028
4109
988a8ef1deea Some fixes to get dbox code working better. Still needs some work though..
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
1029 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
1030 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
1031 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
1032 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
1033 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
1034 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
1035
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1036 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
1037 *file_r = file;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1038 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1039 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1040
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1041 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
1042 struct dbox_file *file)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1043 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1044 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
1045 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1046
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1047 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
1048 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
1049 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
1050 save_file = files[i];
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1051 break;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1052 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1053 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1054 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
1055
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1056 ctx->mail_count++;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1057 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
1058
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1059 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
1060 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1061
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1062 struct dbox_file *
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1063 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
1064 uint32_t file_seq)
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1065 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1066 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
1067 unsigned int i, count;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1068
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1069 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
1070 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
1071 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
1072 return files[i]->file;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1073 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1074
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1075 i_unreached();
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1076 return NULL;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1077 }
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 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
1080 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1081 /* 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
1082 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
1083 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
1084 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1085
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1086 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
1087 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
1088 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1089 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1090
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1091 /* 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
1092 committed or rollbacked */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1093 if (!ctx->locked) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1094 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
1095 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1096 ctx->locked = TRUE;
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 /* 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
1099 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
1100 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1101 if (ret == 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1102 /* file is deleted */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1103 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
1104 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1105 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1106
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
1107 *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
1108 *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
1109 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1110 }
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 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
1113 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
1114 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
1115 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1116 int ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1117
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1118 *mtime_r = -1;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1119 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
1120 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1121
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(uidlist)) < 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1123 dbox_uidlist_unlock(uidlist);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1124 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1125 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1126
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1127 if (ret == 0) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1128 /* file is deleted */
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1129 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
1130 } else {
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1131 *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
1132 }
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 *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
1135 (*ctx_r)->uidlist = uidlist;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1136 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1137 }
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 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
1140 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1141 int ret = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1142
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1143 if (ctx->modified) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1144 /* 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
1145 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
1146 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1147
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1148 *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
1149
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1150 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
1151 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
1152 i_free(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1153 return ret;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1154 }
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 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
1157 {
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1158 array_clear(&ctx->uidlist->entries);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1159 ctx->uidlist->ino = 0;
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1160 ctx->uidlist->mtime = 0;
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1161
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1162 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
1163 i_free(ctx);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1164 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1165
3761
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1166 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
1167 {
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1168 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
1169 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
1170 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
1171
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1172 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
1173 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
1174 }
ef482c909771 When index files (dovecot.index and dbox index) aren't synced with each
Timo Sirainen <tss@iki.fi>
parents: 3758
diff changeset
1175
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1176 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
1177 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1178 ctx->modified = TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1179 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1180
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1181 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
1182 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
1183 {
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1184 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
1185 struct dbox_uidlist_entry *new_entry;
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1186 unsigned int count;
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1187
4056
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
1188 i_assert(array_count(&entry->uid_list) > 0);
Timo Sirainen <tss@iki.fi>
parents: 3985
diff changeset
1189
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1190 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
1191 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
1192 *new_entry = *entry;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1193
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1194 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
1195 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
1196 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
1197
3758
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1198 entries = array_get(&ctx->uidlist->entries, &count);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1199 if (count == 0 || entries[count-1]->file_seq < new_entry->file_seq)
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1200 array_append(&ctx->uidlist->entries, &new_entry, 1);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1201 else {
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1202 pos = bsearch_insert_pos(&new_entry->file_seq, entries,
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1203 count, sizeof(*entries),
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1204 dbox_uidlist_entry_cmp);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1205 array_insert(&ctx->uidlist->entries, pos - entries,
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1206 &new_entry, 1);
Timo Sirainen <tss@iki.fi>
parents: 3756
diff changeset
1207 }
3720
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1208 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1209
4114
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1210 void dbox_uidlist_sync_unlink(struct dbox_uidlist_sync_ctx *ctx,
d1c27abc6ebc Fixes and cleanups.
Timo Sirainen <tss@iki.fi>
parents: 4109
diff changeset
1211 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
1212 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1213 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
1214 unsigned int idx;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1215
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1216 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
1217 i_assert(entry != NULL);
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1218
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1219 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
1220
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1221 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
1222 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1223
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1224 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
1225 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1226 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
1227 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
1228 ctx->modified = TRUE;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1229 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1230
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1231 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
1232 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1233
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1234 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
1235 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1236 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
1237 }
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 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
1240 {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1241 struct stat st;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1242
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1243 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
1244 if (errno != ENOENT) {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1245 mail_storage_set_critical(
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1246 STORAGE(uidlist->mbox->storage),
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1247 "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
1248 return -1;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1249 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1250
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1251 *mtime_r = 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1252 } else {
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1253 *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
1254 }
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1255 return 0;
fd0986477809 Initial implementation of Dovecot's own high performance file format, named
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1256 }