1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
/*
** Copyright 2004-2007 Double Precision, Inc.
** See COPYING for distribution information.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_UTIME_H
#include <utime.h>
#endif
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "maildir/maildirnewshared.h"
static struct maildir_shindex_cache *shared_cache=NULL;
static void shared_cache_free(struct maildir_shindex_cache *c)
{
while (c)
{
struct maildir_shindex_cache *p=c;
size_t i;
c=c->next;
free(p->hierarchy);
if (p->records)
{
for (i=0; i<p->nrecords; i++)
free(p->records[i].name);
free(p->records);
}
maildir_newshared_close(&p->indexfile);
free(p);
}
}
static struct maildir_shindex_cache *do_shared_cache_read(const char *indexfile,
const char *subhier);
struct maildir_shindex_cache *
maildir_shared_cache_read(struct maildir_shindex_cache *parent,
const char *indexfile,
const char *subhierarchy)
{
struct maildir_shindex_cache *p;
if (parent && parent->next && subhierarchy &&
strcmp(parent->next->hierarchy, subhierarchy) == 0)
{
return parent->next; /* That was easy */
}
if (!parent && shared_cache)
{
return shared_cache;
}
if (!indexfile)
{
indexfile=maildir_shared_index_file();
if (!indexfile)
return NULL;
subhierarchy="";
}
if (!subhierarchy)
return NULL;
/* Should not happen, bad usage. subhierarchy allowed to be NULL only
** when indexfile is also NULL */
p=do_shared_cache_read(indexfile, subhierarchy);
if (!p)
return NULL;
if (!parent)
{
shared_cache_free(shared_cache);
shared_cache=p;
}
else
{
shared_cache_free(parent->next);
parent->next=p;
}
return p;
}
struct maildir_shindex_temp_record {
struct maildir_shindex_temp_record *next;
struct maildir_shindex_record rec;
};
static int shared_cache_read_cb(struct maildir_newshared_enum_cb *ptr)
{
struct maildir_shindex_temp_record *r;
struct maildir_shindex_temp_record **list=
(struct maildir_shindex_temp_record **)ptr->cb_arg;
if ((r=malloc(sizeof(struct maildir_shindex_temp_record))) == NULL ||
(r->rec.name=strdup(ptr->name)) == NULL)
{
if (r)
free(r);
perror("malloc");
return -1;
}
r->rec.offset=ptr->startingpos;
r->next= *list;
*list=r;
return 0;
}
static struct maildir_shindex_cache *do_shared_cache_read(const char *indexfile,
const char *subhier)
{
struct maildir_shindex_temp_record *rec=NULL;
size_t n;
struct maildir_shindex_cache *c;
int eof;
int rc;
if ((c=malloc(sizeof(struct maildir_shindex_cache))) == NULL ||
(c->hierarchy=strdup(subhier)) == NULL)
{
if (c)
free(c);
perror("malloc");
return NULL;
}
if (maildir_newshared_open(indexfile, &c->indexfile) < 0)
{
free(c->hierarchy);
free(c);
return NULL;
}
n=0;
while ((rc=maildir_newshared_next(&c->indexfile, &eof,
shared_cache_read_cb, &rec)) == 0)
{
if (eof)
break;
++n;
}
if (rc)
{
free(c->hierarchy);
free(c);
while (rec)
{
struct maildir_shindex_temp_record *r=rec;
rec=rec->next;
free(r->rec.name);
free(r);
}
return NULL;
}
/* Now, convert from list to array */
c->nrecords=n;
c->records=NULL;
c->next=NULL;
if (n)
{
if ((c->records=malloc(sizeof(*c->records)*n)) == NULL)
{
free(c->hierarchy);
free(c);
while (rec)
{
struct maildir_shindex_temp_record *r=rec;
rec=rec->next;
free(r->rec.name);
free(r);
}
return NULL;
}
n=0;
while (rec)
{
struct maildir_shindex_temp_record *r=rec;
rec=rec->next;
c->records[n]= r->rec;
free(r);
++n;
}
}
return c;
}
|