annotate src/lib-storage/mailbox-uidvalidity.c @ 9354:687ac828b964 HEAD

lib-index: modseqs weren't tracked properly within session when changes were done.
author Timo Sirainen <tss@iki.fi>
date Tue, 01 Sep 2009 13:05:03 -0400
parents b9faf4db2a9f
children 00cd9aacd03c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8590
b9faf4db2a9f Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents: 8359
diff changeset
1 /* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
8260
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "ioloop.h"
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "str.h"
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "read-full.h"
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "write-full.h"
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "mailbox-uidvalidity.h"
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include <stdio.h>
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include <stdlib.h>
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include <unistd.h>
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 #include <dirent.h>
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 #include <fcntl.h>
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 #define RETRY_COUNT 10
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 static uint32_t mailbox_uidvalidity_next_fallback(void)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 static uint32_t uid_validity = 0;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 /* we failed to use the uidvalidity file. don't fail the mailbox
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 creation because of it though, most of the time it's safe enough
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 to use the current time as the uidvalidity value. */
8359
54ff30d93cab Compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 8260
diff changeset
25 if (uid_validity < (uint32_t)ioloop_time)
54ff30d93cab Compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 8260
diff changeset
26 uid_validity = (uint32_t)ioloop_time;
8260
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 else
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 uid_validity++;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 return uid_validity;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 static void mailbox_uidvalidity_write(const char *path, uint32_t uid_validity)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 char buf[8+1];
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 int fd;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 fd = open(path, O_RDWR | O_CREAT, 0666);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 if (fd == -1) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 i_error("open(%s) failed: %m", path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 return;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 i_snprintf(buf, sizeof(buf), "%08x", uid_validity);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 if (pwrite_full(fd, buf, strlen(buf), 0) < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 i_error("write(%s) failed: %m", path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 if (close(fd) < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 i_error("close(%s) failed: %m", path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 static int mailbox_uidvalidity_rename(const char *path, uint32_t *uid_validity)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 string_t *src, *dest;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 unsigned int i, prefix_len;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 int ret;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 src = t_str_new(256);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 str_append(src, path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 dest = t_str_new(256);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 str_append(dest, path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 prefix_len = str_len(src);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 for (i = 0; i < RETRY_COUNT; i++) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 str_printfa(src, ".%08x", *uid_validity);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 *uid_validity += 1;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 str_printfa(dest, ".%08x", *uid_validity);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 if ((ret = rename(str_c(src), str_c(dest))) == 0 ||
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 errno != ENOENT)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 break;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 /* possibly a race condition. try the next value. */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 str_truncate(src, prefix_len);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 str_truncate(dest, prefix_len);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 if (ret < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 i_error("rename(%s, %s) failed: %m", str_c(src), str_c(dest));
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 return ret;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 static uint32_t mailbox_uidvalidity_next_rescan(const char *path)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 DIR *d;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 struct dirent *dp;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 const char *fname, *dir, *prefix, *tmp;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 char *endp;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 unsigned int i, prefix_len;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 uint32_t cur_value, min_value, max_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 int fd;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 fname = strrchr(path, '/');
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 if (fname == NULL) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 dir = ".";
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 fname = path;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 } else {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 dir = t_strdup_until(path, fname);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95 fname++;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 d = opendir(dir);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 if (d == NULL) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 i_error("opendir(%s) failed: %m", dir);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101 return mailbox_uidvalidity_next_fallback();
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 prefix = t_strconcat(fname, ".", NULL);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 prefix_len = strlen(prefix);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 /* just in case there happens to be multiple matching uidvalidity
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 files, track the min/max values. use the max value and delete the
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 min value file. */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 max_value = 0; min_value = (uint32_t)-1;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 while ((dp = readdir(d)) != NULL) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 if (strncmp(dp->d_name, prefix, prefix_len) == 0) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 cur_value = strtoul(dp->d_name + prefix_len, &endp, 16);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 if (*endp == '\0') {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 if (min_value > cur_value)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 min_value = cur_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 if (max_value < cur_value)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 max_value = cur_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 if (closedir(d) < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 i_error("closedir(%s) failed: %m", dir);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 if (max_value == 0) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 /* no uidvalidity files. create one. */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 for (i = 0; i < RETRY_COUNT; i++) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 cur_value = mailbox_uidvalidity_next_fallback();
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 tmp = t_strdup_printf("%s.%08x", path, cur_value);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 fd = open(tmp, O_RDWR | O_CREAT | O_EXCL, 0666);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 if (fd != -1 || errno != EEXIST)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 break;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132 /* already exists. although it's quite unlikely we'll
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 hit this race condition. more likely we'll create
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 a duplicate file.. */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 if (fd == -1) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 i_error("creat(%s) failed: %m", tmp);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 return cur_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 (void)close(fd);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141 mailbox_uidvalidity_write(path, cur_value);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 return cur_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 if (min_value != max_value) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 /* duplicate uidvalidity files, delete the oldest */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146 tmp = t_strdup_printf("%s.%08x", path, min_value);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 if (unlink(tmp) < 0 && errno != ENOENT)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 i_error("unlink(%s) failed: %m", tmp);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151 cur_value = max_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 if (mailbox_uidvalidity_rename(path, &cur_value) < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153 return mailbox_uidvalidity_next_fallback();
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 mailbox_uidvalidity_write(path, cur_value);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 return cur_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 uint32_t mailbox_uidvalidity_next(const char *path)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 char buf[8+1], *endp;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 uint32_t cur_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 int fd, ret;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
164 fd = open(path, O_RDWR);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165 if (fd == -1) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 if (errno != ENOENT)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 i_error("open(%s) failed: %m", path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 return mailbox_uidvalidity_next_rescan(path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 ret = read_full(fd, buf, sizeof(buf)-1);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 if (ret < 0) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 i_error("read(%s) failed: %m", path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 (void)close(fd);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 return mailbox_uidvalidity_next_rescan(path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 buf[sizeof(buf)-1] = 0;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 cur_value = strtoul(buf, &endp, 16);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 if (ret == 0 || endp != buf+sizeof(buf)-1) {
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 /* broken value */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 (void)close(fd);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 return mailbox_uidvalidity_next_rescan(path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182 }
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184 /* we now have the current uidvalidity value that's hopefully correct */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185 if (mailbox_uidvalidity_rename(path, &cur_value) < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 return mailbox_uidvalidity_next_rescan(path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
187
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188 /* fast path succeeded. write the current value to the main
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 uidvalidity file. */
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 i_snprintf(buf, sizeof(buf), "%08x", cur_value);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 if (pwrite_full(fd, buf, strlen(buf), 0) < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 i_error("write(%s) failed: %m", path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193 if (close(fd) < 0)
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 i_error("close(%s) failed: %m", path);
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 return cur_value;
110afc84fbb1 Maildir/dbox: Try harder to assign unique UIDVALIDITY values to mailboxes.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 }