folder.c
11.4 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2003, 2004,
2005 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <glob.h>
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <folder0.h>
#include <registrar0.h>
#include <mailutils/auth.h>
#include <mailutils/url.h>
#include <mailutils/stream.h>
#include <mailutils/mutil.h>
#include <mailutils/errno.h>
/* We export url parsing and the initialisation of
the mailbox, via the register entry/record. */
static struct mu__record _mbox_record =
{
MU_MBOX_PRIO,
MU_MBOX_SCHEME,
_url_mbox_init, /* Mailbox init. */
_mailbox_mbox_init, /* Mailbox init. */
NULL, /* Mailer init. */
_folder_mbox_init, /* Folder init. */
NULL, /* No need for an back pointer. */
NULL, /* _is_scheme method. */
NULL, /* _get_url method. */
NULL, /* _get_mailbox method. */
NULL, /* _get_mailer method. */
NULL /* _get_folder method. */
};
mu_record_t mu_mbox_record = &_mbox_record;
static int
_path_is_scheme (mu_record_t record, const char *url, int flags)
{
int rc = 0;
const char *path;
if (url && record->scheme)
{
if (mu_scheme_autodetect_p (url, &path))
/* implies if (strncmp (record->scheme, url, strlen(record->scheme)) == 0)*/
{
struct stat st;
if (stat (path, &st) < 0)
return MU_FOLDER_ATTRIBUTE_ALL; /* mu_mailbox_open will complain */
if ((flags & MU_FOLDER_ATTRIBUTE_FILE)
&& (S_ISREG (st.st_mode) || S_ISCHR (st.st_mode)))
rc |= MU_FOLDER_ATTRIBUTE_FILE;
if ((flags & MU_FOLDER_ATTRIBUTE_DIRECTORY)
&& S_ISDIR (st.st_mode))
rc |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
}
}
return rc;
}
static struct mu__record _path_record =
{
MU_PATH_PRIO,
MU_PATH_SCHEME,
_url_path_init, /* Mailbox init. */
_mailbox_mbox_init, /* Mailbox init. */
NULL, /* Mailer init. */
_folder_mbox_init, /* Folder init. */
NULL, /* No need for an owner. */
_path_is_scheme, /* is_scheme method. */
NULL, /* get_url method. */
NULL, /* get_mailbox method. */
NULL, /* get_mailer method. */
NULL /* get_folder method. */
};
mu_record_t mu_path_record = &_path_record;
/* lsub/subscribe/unsubscribe are not needed. */
static void folder_mbox_destroy (mu_folder_t);
static int folder_mbox_open (mu_folder_t, int);
static int folder_mbox_close (mu_folder_t);
static int folder_mbox_delete (mu_folder_t, const char *);
static int folder_mbox_rename (mu_folder_t , const char *, const char *);
static int folder_mbox_list (mu_folder_t, const char *, const char *,
struct mu_folder_list *);
static int folder_mbox_subscribe (mu_folder_t, const char *);
static int folder_mbox_unsubscribe (mu_folder_t, const char *);
static int folder_mbox_lsub (mu_folder_t, const char *, const char *,
struct mu_folder_list *);
static char *get_pathname (const char *, const char *);
static int folder_mbox_get_authority (mu_folder_t folder, mu_authority_t * pauth);
struct _fmbox
{
char *dirname;
char **subscribe;
size_t sublen;
};
typedef struct _fmbox *fmbox_t;
int
_folder_mbox_init (mu_folder_t folder)
{
fmbox_t dfolder;
size_t name_len = 0;
int status = 0;
/* We create an authority so the API is uniform across the mailbox
types. */
status = folder_mbox_get_authority (folder, NULL);
if (status != 0)
return status;
dfolder = folder->data = calloc (1, sizeof (*dfolder));
if (dfolder == NULL)
return ENOMEM;
mu_url_get_path (folder->url, NULL, 0, &name_len);
dfolder->dirname = calloc (name_len + 1, sizeof (char));
if (dfolder->dirname == NULL)
{
free (dfolder);
folder->data = NULL;
return ENOMEM;
}
mu_url_get_path (folder->url, dfolder->dirname, name_len + 1, NULL);
folder->_destroy = folder_mbox_destroy;
folder->_open = folder_mbox_open;
folder->_close = folder_mbox_close;
folder->_list = folder_mbox_list;
folder->_lsub = folder_mbox_lsub;
folder->_subscribe = folder_mbox_subscribe;
folder->_unsubscribe = folder_mbox_unsubscribe;
folder->_delete = folder_mbox_delete;
folder->_rename = folder_mbox_rename;
return 0;
}
void
folder_mbox_destroy (mu_folder_t folder)
{
if (folder->data)
{
fmbox_t fmbox = folder->data;
if (fmbox->dirname)
free (fmbox->dirname);
if (fmbox->subscribe)
free (fmbox->subscribe);
free (folder->data);
folder->data = NULL;
}
}
/* Noop. */
static int
folder_mbox_open (mu_folder_t folder, int flags ARG_UNUSED)
{
fmbox_t fmbox = folder->data;
if (flags & MU_STREAM_CREAT)
{
return (mkdir (fmbox->dirname, S_IRWXU) == 0) ? 0 : errno;
}
return 0;
}
/* Noop. */
static int
folder_mbox_close (mu_folder_t folder ARG_UNUSED)
{
return 0;
}
static int
folder_mbox_delete (mu_folder_t folder, const char *filename)
{
fmbox_t fmbox = folder->data;
if (filename)
{
int status = 0;
char *pathname = get_pathname (fmbox->dirname, filename);
if (pathname)
{
if (remove (pathname) != 0)
status = errno;
free (pathname);
}
else
status = ENOMEM;
return status;
}
return EINVAL;
}
static int
folder_mbox_rename (mu_folder_t folder, const char *oldpath, const char *newpath)
{
fmbox_t fmbox = folder->data;
if (oldpath && newpath)
{
int status = 0;
char *pathold = get_pathname (fmbox->dirname, oldpath);
if (pathold)
{
char *pathnew = get_pathname (fmbox->dirname, newpath);
if (pathnew)
{
if (rename (pathold, pathnew) != 0)
status = errno;
free (pathnew);
}
else
status = ENOMEM;
free (pathold);
}
else
status = ENOMEM;
return status;
}
return EINVAL;
}
/* The listing is not recursif and we use glob() some expansion for us.
Unfortunately glob() does not expand the '~'. We also return
The full pathname so it can be use to create other folders. */
static int
folder_mbox_list (mu_folder_t folder, const char *dirname, const char *pattern,
struct mu_folder_list *pflist)
{
fmbox_t fmbox = folder->data;
char *pathname = NULL;
int status;
size_t num = 0;
glob_t gl;
if (dirname == NULL || dirname[0] == '\0')
dirname = (const char *)fmbox->dirname;
pathname = get_pathname (dirname, pattern);
if (pathname)
{
memset(&gl, 0, sizeof(gl));
status = glob (pathname, 0, NULL, &gl);
free (pathname);
num = gl.gl_pathc;
}
else
status = ENOMEM;
/* Build the folder list from glob. */
if (status == 0)
{
if (pflist)
{
struct mu_list_response **plist;
plist = calloc (num, sizeof (*plist));
if (plist)
{
size_t i;
struct stat stbuf;
for (i = 0; i < num; i++)
{
plist[i] = calloc (1, sizeof (**plist));
if (plist[i] == NULL
|| (plist[i]->name = strdup (gl.gl_pathv[i])) == NULL)
{
num = i;
break;
}
if (stat (gl.gl_pathv[i], &stbuf) == 0)
{
mu_record_t record;
plist[i]->type = mu_registrar_lookup (gl.gl_pathv[i],
&record,
MU_FOLDER_ATTRIBUTE_ALL);
}
plist[i]->separator = '/';
}
}
pflist->element = plist;
pflist->num = num;
}
globfree (&gl);
}
else
{
switch (status)
{
case GLOB_NOSPACE:
status = ENOMEM;
break;
case GLOB_ABORTED:
status = MU_READ_ERROR;
break;
case GLOB_NOMATCH:
status = MU_ERR_NOENT;
break;
case GLOB_NOSYS:
status = ENOSYS;
break;
default:
status = MU_ERR_FAILURE;
break;
}
}
return status;
}
static int
folder_mbox_lsub (mu_folder_t folder, const char *ref ARG_UNUSED, const char *name,
struct mu_folder_list *pflist)
{
fmbox_t fmbox = folder->data;
size_t j = 0;
if (pflist == NULL)
return MU_ERR_OUT_NULL;
if (name == NULL || *name == '\0')
name = "*";
if (fmbox->sublen > 0)
{
struct mu_list_response **plist;
size_t i;
plist = calloc (fmbox->sublen, sizeof (*plist));
for (i = 0; i < fmbox->sublen; i++)
{
if (fmbox->subscribe[i]
&& fnmatch (name, fmbox->subscribe[i], 0) == 0)
{
plist[i] = calloc (1, sizeof (**plist));
if (plist[i] == NULL
|| (plist[i]->name = strdup (fmbox->subscribe[i])) == NULL)
break;
plist[i]->type = MU_FOLDER_ATTRIBUTE_FILE;
plist[i]->separator = '/';
j++;
}
}
pflist->element = plist;
}
pflist->num = j;
return 0;
}
static int
folder_mbox_subscribe (mu_folder_t folder, const char *name)
{
fmbox_t fmbox = folder->data;
char **tmp;
size_t i;
for (i = 0; i < fmbox->sublen; i++)
{
if (fmbox->subscribe[i] && strcmp (fmbox->subscribe[i], name) == 0)
return 0;
}
tmp = realloc (fmbox->subscribe, (fmbox->sublen + 1) * sizeof (*tmp));
if (tmp == NULL)
return ENOMEM;
fmbox->subscribe = tmp;
fmbox->subscribe[fmbox->sublen] = strdup (name);
if (fmbox->subscribe[fmbox->sublen] == NULL)
return ENOMEM;
fmbox->sublen++;
return 0;
}
static int
folder_mbox_unsubscribe (mu_folder_t folder, const char *name)
{
fmbox_t fmbox = folder->data;
size_t i;
for (i = 0; i < fmbox->sublen; i++)
{
if (fmbox->subscribe[i] && strcmp (fmbox->subscribe[i], name) == 0)
{
free (fmbox->subscribe[i]);
fmbox->subscribe[i] = NULL;
return 0;
}
}
return MU_ERR_NOENT;
}
static char *
get_pathname (const char *dirname, const char *basename)
{
char *pathname = NULL;
/* Skip eventual protocol designator.
FIXME: Actually, any valid URL spec should be allowed as dirname ... */
if (strncmp (dirname, MU_MBOX_SCHEME, MU_MBOX_SCHEME_LEN) == 0)
dirname += MU_MBOX_SCHEME_LEN;
else if (strncmp (dirname, MU_FILE_SCHEME, MU_FILE_SCHEME_LEN) == 0)
dirname += MU_FILE_SCHEME_LEN;
/* null basename gives dirname. */
if (basename == NULL)
pathname = (dirname) ? strdup (dirname) : strdup (".");
/* Absolute. */
else if (basename[0] == '/')
pathname = strdup (basename);
/* Relative. */
else
{
size_t baselen = strlen (basename);
size_t dirlen = strlen (dirname);
while (dirlen > 0 && dirname[dirlen-1] == '/')
dirlen--;
pathname = calloc (dirname + baselen + 2, sizeof (char));
if (pathname)
{
memcpy (pathname, dirname, dirlen);
pathname[dirlen] = '/';
strcpy (pathname + dirlen + 1, basename);
}
}
return pathname;
}
static int
folder_mbox_get_authority (mu_folder_t folder, mu_authority_t *pauth)
{
int status = 0;
if (folder->authority == NULL)
{
status = mu_authority_create_null (&folder->authority, folder);
}
if (!status && pauth)
*pauth = folder->authority;
return status;
}