fsfolder.c
11.9 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/* Implementation of file-system folder for GNU Mailutils
Copyright (C) 1999-2001, 2003-2008, 2010-2012, 2014 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 3 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, see
<http://www.gnu.org/licenses/>. */
#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 <mailutils/sys/folder.h>
#include <mailutils/sys/registrar.h>
#include <mailutils/auth.h>
#include <mailutils/url.h>
#include <mailutils/stream.h>
#include <mailutils/util.h>
#include <mailutils/errno.h>
#include <mailutils/debug.h>
#include <mailutils/property.h>
#include <mailutils/iterator.h>
/* File-system folder is shared between UNIX mbox, maildir and MH
mailboxes. It implements all usual folder methods, excepting
for _delete, which is implemented on the mailbox level. See
comment to mu_folder_delete in folder.c */
struct _mu_fsfolder
{
char *dirname;
mu_property_t subscription;
};
static int
open_subscription (struct _mu_fsfolder *folder)
{
int rc;
mu_property_t prop;
mu_stream_t str;
char *filename = mu_make_file_name (folder->dirname, ".mu-subscr");
rc = mu_file_stream_create (&str, filename, MU_STREAM_RDWR|MU_STREAM_CREAT);
if (rc)
return rc;
rc = mu_property_create_init (&prop, mu_assoc_property_init, str);
free (filename);
if (rc == 0)
folder->subscription = prop;
return rc;
}
static char *
get_pathname (const char *dirname, const char *basename)
{
char *pathname = NULL, *p;
/* Skip eventual protocol designator. */
p = strchr (dirname, ':');
if (p && p[1] == '/' && p[2] == '/')
dirname = p + 3;
/* null basename gives dirname. */
if (basename == NULL)
pathname = strdup (dirname ? dirname : ".");
/* 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 (dirlen + baselen + 2, sizeof (char));
if (pathname)
{
memcpy (pathname, dirname, dirlen);
pathname[dirlen] = '/';
strcpy (pathname + dirlen + 1, basename);
}
}
return pathname;
}
static void
_fsfolder_destroy (mu_folder_t folder)
{
if (folder->data)
{
struct _mu_fsfolder *fsfolder = folder->data;
free (fsfolder->dirname);
mu_property_destroy (&fsfolder->subscription);
free (folder->data);
folder->data = NULL;
}
}
/* Noop. */
static int
_fsfolder_open (mu_folder_t folder, int flags MU_ARG_UNUSED)
{
struct _mu_fsfolder *fsfolder = folder->data;
if (flags & MU_STREAM_CREAT)
{
return (mkdir (fsfolder->dirname, S_IRWXU) == 0) ? 0 : errno;
}
return 0;
}
/* Noop. */
static int
_fsfolder_close (mu_folder_t folder MU_ARG_UNUSED)
{
int rc = 0;
struct _mu_fsfolder *fsfolder = folder->data;
if (fsfolder->subscription)
rc = mu_property_save (fsfolder->subscription);
return rc;
}
static int
_fsfolder_rename (mu_folder_t folder, const char *oldpath,
const char *newpath)
{
struct _mu_fsfolder *fsfolder = folder->data;
if (oldpath && newpath)
{
int status = 0;
char *pathold = get_pathname (fsfolder->dirname, oldpath);
if (pathold)
{
char *pathnew = get_pathname (fsfolder->dirname, newpath);
if (pathnew)
{
if (access (pathnew, F_OK) == 0)
status = EEXIST;
else if (rename (pathold, pathnew) != 0)
status = errno;
free (pathnew);
}
else
status = ENOMEM;
free (pathold);
}
else
status = ENOMEM;
return status;
}
return EINVAL;
}
struct inode_list /* Inode/dev number list used to cut off
recursion */
{
struct inode_list *next;
ino_t inode;
dev_t dev;
};
struct search_data
{
mu_list_t result;
mu_folder_enumerate_fp enumfun;
void *enumdata;
char *dirname;
size_t dirlen;
void *pattern;
int flags;
size_t max_level;
size_t errcnt;
mu_folder_t folder;
};
static int
inode_list_lookup (struct inode_list *list, struct stat *st)
{
for (; list; list = list->next)
if (list->inode == st->st_ino && list->dev == st->st_dev)
return 1;
return 0;
}
static int
list_helper (struct search_data *data, mu_record_t record,
const char *dirname, size_t level,
struct inode_list *ilist)
{
DIR *dirp;
struct dirent *dp;
int stop = 0;
if (data->max_level && level > data->max_level)
return 0;
dirp = opendir (dirname);
if (dirp == NULL)
{
mu_debug (MU_DEBCAT_MAILER, MU_DEBUG_ERROR,
("list_helper cannot open directory %s: %s",
dirname, mu_strerror (errno)));
data->errcnt++;
return 1;
}
if (!record)
{
int type;
mu_registrar_lookup (dirname, MU_FOLDER_ATTRIBUTE_ALL,
&record, &type);
}
while ((dp = readdir (dirp)))
{
char const *ename = dp->d_name;
char *fname;
struct stat st;
if (ename[ename[0] != '.' ? 0 : ename[1] != '.' ? 1 : 2] == 0)
continue;
if (strncmp (ename, ".mu-", 4) == 0)
continue;
fname = get_pathname (dirname, ename);
if (lstat (fname, &st) == 0)
{
int f;
if (S_ISDIR (st.st_mode))
f = MU_FOLDER_ATTRIBUTE_DIRECTORY;
else if (S_ISREG (st.st_mode))
f = MU_FOLDER_ATTRIBUTE_FILE;
else if (S_ISLNK (st.st_mode))
f = MU_FOLDER_ATTRIBUTE_LINK;
else
f = 0;
if (mu_record_list_p (record, ename, f))
{
if (data->folder->_match == NULL
|| data->folder->_match (fname + data->dirlen +
((data->dirlen > 1
&& data->dirname[data->dirlen-1] != '/') ?
1 : 0),
data->pattern,
data->flags) == 0)
{
char *refname = fname;
int type = 0;
struct mu_list_response *resp;
mu_record_t rec = NULL;
resp = malloc (sizeof (*resp));
if (resp == NULL)
{
mu_debug (MU_DEBCAT_MAILER, MU_DEBUG_ERROR,
("list_helper: %s", mu_strerror (ENOMEM)));
data->errcnt++;
free (fname);
continue;
}
mu_registrar_lookup (refname, MU_FOLDER_ATTRIBUTE_ALL,
&rec, &type);
resp->name = fname;
resp->level = level;
resp->separator = '/';
resp->type = type;
resp->format = rec;
if (resp->type == 0)
{
free (resp->name);
free (resp);
continue;
}
if (data->enumfun)
{
if (data->enumfun (data->folder, resp, data->enumdata))
{
free (resp->name);
free (resp);
stop = 1;
break;
}
}
if (data->result)
{
fname = NULL;
mu_list_append (data->result, resp);
}
else
free (resp);
if ((type & MU_FOLDER_ATTRIBUTE_DIRECTORY)
&& !inode_list_lookup (ilist, &st))
{
struct inode_list idata;
idata.inode = st.st_ino;
idata.dev = st.st_dev;
idata.next = ilist;
stop = list_helper (data, rec, refname, level + 1,
&idata);
}
}
else if (S_ISDIR (st.st_mode))
{
struct inode_list idata;
idata.inode = st.st_ino;
idata.dev = st.st_dev;
idata.next = ilist;
stop = list_helper (data, NULL, fname, level + 1, &idata);
}
}
}
else
{
mu_debug (MU_DEBCAT_MAILER, MU_DEBUG_ERROR,
("list_helper cannot stat %s: %s",
fname, mu_strerror (errno)));
}
free (fname);
}
closedir (dirp);
return stop;
}
static int
_fsfolder_list (mu_folder_t folder, const char *ref,
void *pattern,
int flags,
size_t max_level,
mu_list_t flist,
mu_folder_enumerate_fp enumfun, void *enumdata)
{
struct _mu_fsfolder *fsfolder = folder->data;
struct inode_list iroot;
struct search_data sdata;
memset (&iroot, 0, sizeof iroot);
sdata.dirname = get_pathname (fsfolder->dirname, ref);
sdata.dirlen = strlen (sdata.dirname);
sdata.result = flist;
sdata.enumfun = enumfun;
sdata.enumdata = enumdata;
sdata.pattern = pattern;
sdata.flags = flags;
sdata.max_level = max_level;
sdata.folder = folder;
sdata.errcnt = 0;
list_helper (&sdata, NULL, sdata.dirname, 0, &iroot);
free (sdata.dirname);
/* FIXME: error code */
return 0;
}
static int
_fsfolder_lsub (mu_folder_t folder, const char *ref, const char *name,
mu_list_t flist)
{
struct _mu_fsfolder *fsfolder = folder->data;
int rc;
char *pattern;
mu_iterator_t itr;
if (name == NULL || *name == '\0')
name = "*";
if (!fsfolder->subscription && (rc = open_subscription (fsfolder)))
return rc;
pattern = mu_make_file_name (ref, name);
rc = mu_property_get_iterator (fsfolder->subscription, &itr);
if (rc == 0)
{
for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
const char *key, *val;
mu_iterator_current_kv (itr, (const void **)&key, (void**)&val);
if (fnmatch (pattern, key, 0) == 0)
{
struct mu_list_response *resp;
resp = malloc (sizeof (*resp));
if (resp == NULL)
{
rc = ENOMEM;
break;
}
else if ((resp->name = strdup (key)) == NULL)
{
free (resp);
rc = ENOMEM;
break;
}
resp->type = MU_FOLDER_ATTRIBUTE_FILE;
resp->level = 0;
resp->separator = '/';
rc = mu_list_append (flist, resp);
if (rc)
{
free (resp);
break;
}
}
}
mu_iterator_destroy (&itr);
}
free (pattern);
return rc;
}
static int
_fsfolder_subscribe (mu_folder_t folder, const char *name)
{
struct _mu_fsfolder *fsfolder = folder->data;
int rc;
if (!fsfolder->subscription && (rc = open_subscription (fsfolder)))
return rc;
return mu_property_set_value (fsfolder->subscription, name, "", 1);
}
static int
_fsfolder_unsubscribe (mu_folder_t folder, const char *name)
{
struct _mu_fsfolder *fsfolder = folder->data;
int rc;
if (!fsfolder->subscription && (rc = open_subscription (fsfolder)))
return rc;
return mu_property_unset (fsfolder->subscription, name);
}
static int
_fsfolder_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;
}
int
_mu_fsfolder_init (mu_folder_t folder)
{
struct _mu_fsfolder *dfolder;
int status = 0;
/* We create an authority so the API is uniform across the mailbox
types. */
status = _fsfolder_get_authority (folder, NULL);
if (status != 0)
return status;
dfolder = folder->data = calloc (1, sizeof (*dfolder));
if (dfolder == NULL)
return ENOMEM;
status = mu_url_aget_path (folder->url, &dfolder->dirname);
if (status == MU_ERR_NOENT)
{
dfolder->dirname = malloc (2);
if (dfolder->dirname == NULL)
status = ENOMEM;
else
{
strcpy (dfolder->dirname, ".");
status = 0;
}
}
if (status)
{
free (dfolder);
folder->data = NULL;
return status;
}
folder->_destroy = _fsfolder_destroy;
folder->_open = _fsfolder_open;
folder->_close = _fsfolder_close;
folder->_list = _fsfolder_list;
folder->_lsub = _fsfolder_lsub;
folder->_subscribe = _fsfolder_subscribe;
folder->_unsubscribe = _fsfolder_unsubscribe;
folder->_delete = NULL;
folder->_rename = _fsfolder_rename;
return 0;
}