-
Notifications
You must be signed in to change notification settings - Fork 11
/
notmuchfs.c
1372 lines (1166 loc) · 39.3 KB
/
notmuchfs.c
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*============================================================================*/
/*
* notmuchfs - A virtual maildir file system for notmuch queries
*
* Copyright © 2012-2017 Tim Stoakes
*
* This file is part of notmuchfs.
*
* Notmuchfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with notmuchfs. If not, see http://www.gnu.org/licenses/ .
*
* Authors: Tim Stoakes <[email protected]>
*/
/*============================================================================*/
/**
* @section message_names Message Names
*
* Messages in a notmuchfs virtual maildir are named by taking the full path
* to the real message, and replacing all / with #. This whole string is now
* the message name.
*
*
* @section x_label X-Label Header
*
* Each message read from a virtual maildir has an X-Label header inserted
* on-the-fly, containing the concatenation of the notmuch tags of this
* message (comma separated), up to #MAX_XLABEL_LENGTH characters long.
*/
/*============================================================================*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <dirent.h>
#include <assert.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/param.h>
#include <string.h>
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include "notmuch.h"
/*============================================================================*/
#define NOTMUCHFS_VERSION "0.4"
/*============================================================================*/
/**
* Global configuration information, from CLI.
*
* @{
*/
struct notmuchfs_config {
/**
* The backing directory path.
*/
char *backing_dir;
/**
* The notmuch database directory path. This is actually the directory that
* contains the .notmuch/ database directory, since that's what notmuch
* requires.
*/
char *mail_dir;
/**
* Tag to apply when a mail is deleted instead of unlinking the
* underlying maildir file.
*/
char *delete_tag;
/**
* Mutt is not compliant with the maildir spec, see:
* - http://dev.mutt.org/trac/ticket/2476
* - http://notmuchmail.org/pipermail/notmuch/2011/004833.html
* Notmuchfs can workaround this issue if this field is set.
*/
bool mutt_2476_workaround_allowed;
};
static struct notmuchfs_config global_config;
/** @} */
/*============================================================================*/
/**
* Whether to enable debug tracing.
*/
#define NOTMUCHFS_DEBUG 0
/**
* The maximum length of the X-Label header that notmuchfs will synthesize.
*
* @todo this limit is arbitrary.
*/
#define MAX_XLABEL_LENGTH 1024
/**
* The text of the X-Label header.
*/
#define XLABEL "X-Label: "
/*============================================================================*/
/** Lock a pthread mutex with error checking. */
#define PTHREAD_LOCK(LOCK) \
do { \
int _ret = pthread_mutex_lock(LOCK); \
assert(_ret == 0); \
} while (0);
/** Unlock a pthread mutex with error checking. */
#define PTHREAD_UNLOCK(LOCK) \
do { \
int _ret = pthread_mutex_unlock(LOCK); \
assert(_ret == 0); \
} while (0);
/*============================================================================*/
/**
* Logging.
*/
#if NOTMUCHFS_DEBUG
#define LOG_TRACE(...) \
printf(__VA_ARGS__)
#else
#define LOG_TRACE(...)
#endif
/*============================================================================*/
/**
* The context required to deal with the notmuch database.
*/
typedef struct
{
/** Mutex to protect the database object from concurrent access. */
pthread_mutex_t mutex;
/** The notmuch database handle. May be NULL if the database is not opened. */
notmuch_database_t *db;
/** Newline-delimited string list of tags to exclude from results. */
char *excluded_tags;
} notmuch_context_t;
/*============================================================================*/
/**
* Replace every instance of character 'old' with 'new' in 'str_in'.
*
* @param[in,out] str_in The string to replace within.
* @param[in] old The character to replace.
* @param[in] new The character to replace with.
*/
static void string_replace (char *str_in, char old, char new)
{
char *str = str_in;
while (*str != '\0') {
if (*str == old)
*str = new;
str++;
}
}
/*============================================================================*/
/**
* Open the notmuch database inside this context. Continue trying forever
* if the open fails (e.g. the database was locked).
*
* @param[in,out] p_ctx The notmuch context.
* @param[in] need_write Whether to open the database in read-only or
* read-write mode.
* @pre Database must not already be open.
*/
static void database_open (notmuch_context_t *p_ctx, bool need_write)
{
LOG_TRACE("notmuch database_open\n");
PTHREAD_LOCK(&p_ctx->mutex);
assert(p_ctx->db == NULL);
while (TRUE) {
notmuch_status_t status =
notmuch_database_open(global_config.mail_dir,
need_write ?
NOTMUCH_DATABASE_MODE_READ_WRITE:
NOTMUCH_DATABASE_MODE_READ_ONLY,
&p_ctx->db);
if (status == NOTMUCH_STATUS_SUCCESS) {
break;
}
else if (status == NOTMUCH_STATUS_XAPIAN_EXCEPTION) {
/* Try again. */
sleep(1);
}
else {
fprintf(stderr, "ERROR: Database open error.\n");
exit(1);
}
}
if (notmuch_database_needs_upgrade(p_ctx->db)) {
fprintf(stderr, "ERROR: Database needs upgrade.\n");
exit(1);
}
}
/*============================================================================*/
/**
* Close the notmuch database inside this context.
*
* @param[in,out] p_ctx The notmuch context.
* @pre Database must be open.
*/
static void database_close (notmuch_context_t *p_ctx)
{
LOG_TRACE("notmuch database_close\n");
assert(p_ctx->db != NULL);
notmuch_database_close(p_ctx->db);
notmuch_database_destroy(p_ctx->db);
p_ctx->db = NULL;
PTHREAD_UNLOCK(&p_ctx->mutex);
}
/*============================================================================*/
/* FUSE operations. */
/** The maximum length of the tag exclusion string. Arbitrarily chosen. */
#define EXCLUDED_TAGS_MAX_LENGTH 128
static void *notmuchfs_init (struct fuse_conn_info *conn)
{
(void)conn;
int res = chdir(global_config.backing_dir);
if (res == -1)
return NULL;
notmuch_context_t *p_ctx = malloc(sizeof(notmuch_context_t));
memset(p_ctx, 0, sizeof(notmuch_context_t));
res = pthread_mutex_init(&p_ctx->mutex, NULL);
if (res != 0) {
free(p_ctx);
return NULL;
}
/* Fetch the list of excluded tags from notmuch config.
* If only there was an API for this...
*/
p_ctx->excluded_tags = malloc(EXCLUDED_TAGS_MAX_LENGTH);
p_ctx->excluded_tags[0] = '\0';
FILE *fp = popen("notmuch config get search.exclude_tags", "r");
if (fp != NULL) {
size_t bytes_read = fread(p_ctx->excluded_tags, 1, EXCLUDED_TAGS_MAX_LENGTH,
fp);
if (bytes_read > 0) {
p_ctx->excluded_tags[bytes_read - 1] = '\0';
}
(void)pclose(fp);
}
return p_ctx;
}
/*============================================================================*/
static void notmuchfs_destroy (void *p_ctx_in)
{
notmuch_context_t *p_ctx = (notmuch_context_t *)p_ctx_in;
free(p_ctx->excluded_tags);
int res = pthread_mutex_destroy(&p_ctx->mutex);
/* Any failure here is a problem that we caused. */
assert(res == 0);
free(p_ctx);
}
/*============================================================================*/
static int notmuchfs_getattr (const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
/* Querying the base directory, pass to backing store. */
if (stat(".", stbuf) != 0)
res = -errno;
return res;
}
char *last_slash = strrchr(path + 1, '/');
if (last_slash == NULL) {
/* Querying '/<query>', pass to backing store. */
LOG_TRACE("getattr stat1: %s\n", path + 1);
if (lstat(path + 1, stbuf) != 0)
res = -errno;
}
else if (strcmp(last_slash + 1, "new") == 0 ||
strcmp(last_slash + 1, "tmp") == 0 ||
strcmp(last_slash + 1, "cur") == 0) {
/* Querying a maildir directory, so copy the parent directory. */
char trans_name[PATH_MAX];
strncpy(trans_name, path + 1, last_slash - path);
trans_name[last_slash - path] = '\0';
LOG_TRACE("getattr stat2: %s\n", trans_name);
if (stat(trans_name, stbuf) != 0)
res = -errno;
}
else {
/* '/<query>/cur/translated#msg#name' */
char *first_slash = strchr(path + 1, '/');
bool mutt_2476_workaround = FALSE;
if (global_config.mutt_2476_workaround_allowed) {
/* The workaround here is to intercept all getattr()s of a path like:
* /real/path/new/fake#maildir#cur#foofile
* and treat it as if 'new' was 'cur' thus:
* /real/path/cur/fake#maildir#cur#foofile
*/
if ((last_slash - path) >= 3 &&
memcmp(last_slash - 3, "new", 3) == 0) {
LOG_TRACE("Activating mutt_bug_2476 workaround for getattr(%s)\n", path);
mutt_2476_workaround = TRUE;
}
}
if (mutt_2476_workaround ||
(last_slash - first_slash > 3 &&
strncmp(first_slash, "/cur/", 5) == 0)) {
char trans_name[PATH_MAX];
strncpy(trans_name, last_slash + 1, PATH_MAX - 1);
trans_name[PATH_MAX - 1] = '\0';
string_replace(trans_name, '#', '/');
LOG_TRACE("getattr stat3: %s\n", trans_name);
if (stat(trans_name, stbuf) != 0)
res = -errno;
/* Inflate the size of the file by the maximum length of a synthetic
* X-Label header.
*/
stbuf->st_size += MAX_XLABEL_LENGTH;
}
else {
res = -ENOENT;
}
}
return res;
}
/*============================================================================*/
/**
* Which type of directory read is being done?
*/
typedef enum
{
/** A directory containing no files. */
OPENDIR_TYPE_EMPTY_DIR,
/** A maildir root (e.g. containing 'cur/'). */
OPENDIR_TYPE_MAIL_DIR,
/** A real directory in the backing store. */
OPENDIR_TYPE_BACKING_DIR,
/** A maildir with message files taken from a notmuch query. */
OPENDIR_TYPE_NOTMUCH_QUERY
} opendir_type_t;
/**
* Context for opendir(), readdir(), releasedir().
*/
typedef struct
{
opendir_type_t type;
/**
* These are for type == OPENDIR_TYPE_NOTMUCH_QUERY.
* @{
*/
notmuch_query_t *p_query;
notmuch_messages_t *p_messages;
/** @} */
/** This is for type == OPENDIR_TYPE_BACKING_DIR. */
DIR *fd;
off_t next_offset;
} opendir_t;
/*============================================================================*/
static int notmuchfs_opendir (const char* path, struct fuse_file_info* fi)
{
int res = 0;
opendir_t *dir_fd = (opendir_t *) malloc(sizeof(opendir_t));
if (strcmp(path, "/") == 0) {
/* Listing '/', so show the backing directory. */
dir_fd->type = OPENDIR_TYPE_BACKING_DIR;
char trans_name[PATH_MAX];
strncpy(trans_name, global_config.backing_dir, PATH_MAX - 1);
strncpy(trans_name + strlen(global_config.backing_dir), path + 1,
PATH_MAX - 1 - strlen(global_config.backing_dir));
trans_name[PATH_MAX - 1] = '\0';
LOG_TRACE("opendir list backing dir: %s\n", trans_name);
dir_fd->fd = opendir(trans_name);
}
else {
char *last_slash = strrchr(path + 1, '/');
if (last_slash == NULL) {
/* Listing '/<query>', so return the 3 maildir dirs. */
LOG_TRACE("opendir fake maildir: %s\n", path);
dir_fd->type = OPENDIR_TYPE_MAIL_DIR;
}
else if (strcmp(last_slash + 1, "new") == 0 ||
strcmp(last_slash + 1, "tmp") == 0) {
/* Listing '/<query>/new' or '/<query>/tmp', so return nothing. */
LOG_TRACE("opendir fake empty new/, tmp/ maildir: %s\n", path);
dir_fd->type = OPENDIR_TYPE_EMPTY_DIR;
}
else if (strcmp(last_slash + 1, "cur") == 0) {
/* Listing '/<query>/cur', so parse the query from the pathname, and
* execute it to get the iterator, and remember it.
*/
dir_fd->type = OPENDIR_TYPE_NOTMUCH_QUERY;
char trans_name[PATH_MAX];
strncpy(trans_name, path + 1, last_slash - path - 1);
trans_name[last_slash - path - 1] = '\0';
/* If it's a symlink, dereference it. */
struct stat stbuf;
while (res == 0) {
LOG_TRACE("opendir stat(%s)\n", trans_name);
if (lstat(trans_name, &stbuf) == 0 &&
S_ISLNK(stbuf.st_mode)) {
char link_name[PATH_MAX + 1];
LOG_TRACE("opendir dereference symlink %s for query\n", trans_name);
res = readlink(trans_name, link_name, PATH_MAX);
if (res >= 0) {
link_name[res] = '\0';
memcpy(trans_name, link_name, res + 1);
res = 0;
}
else
res = -errno;
}
else
break;
}
LOG_TRACE("opendir notmuch query: '%s'\n", trans_name);
struct fuse_context *p_fuse_ctx = fuse_get_context();
notmuch_context_t *p_ctx = (notmuch_context_t *)p_fuse_ctx->private_data;
database_open(p_ctx, FALSE);
dir_fd->next_offset = 1;
dir_fd->p_query = notmuch_query_create(p_ctx->db, trans_name);
if (dir_fd->p_query != NULL) {
/* Exclude messages that match the 'excluded' tags. */
char *exclude_tag = strtok(p_ctx->excluded_tags, "\n");
while (exclude_tag != NULL) {
notmuch_query_add_tag_exclude(dir_fd->p_query, exclude_tag);
exclude_tag = strtok(NULL, "\n");
}
notmuch_query_set_omit_excluded(dir_fd->p_query, NOTMUCH_EXCLUDE_ALL);
/* Run the query. */
notmuch_status_t status =
notmuch_query_search_messages(dir_fd->p_query, &dir_fd->p_messages);
if (status != NOTMUCH_STATUS_SUCCESS) {
notmuch_query_destroy(dir_fd->p_query);
dir_fd->p_query = NULL;
database_close(p_ctx);
res = -EIO;
}
else {
/* On success, the database is left open here. */
}
}
else {
database_close(p_ctx);
res = -EIO;
}
}
else {
/* Trying to open an unrecognized directory, that we did not put there.
* Error it, since this is not supported behavior.
*/
res = -ENOENT;
}
}
if (res == 0) {
fi->fh = (uint64_t)(uintptr_t)dir_fd;
}
else {
free(dir_fd);
}
return res;
}
/*============================================================================*/
static int notmuchfs_releasedir (const char *path, struct fuse_file_info *fi)
{
(void)path;
opendir_t *dir_fd = (opendir_t *)(uintptr_t)fi->fh;
if (dir_fd != NULL) {
if (dir_fd->type == OPENDIR_TYPE_NOTMUCH_QUERY) {
if (dir_fd->p_messages != NULL)
notmuch_messages_destroy(dir_fd->p_messages);
if (dir_fd->p_query != NULL)
notmuch_query_destroy(dir_fd->p_query);
struct fuse_context *p_fuse_ctx = fuse_get_context();
notmuch_context_t *p_ctx = (notmuch_context_t *)p_fuse_ctx->private_data;
database_close(p_ctx);
}
else if (dir_fd->type == OPENDIR_TYPE_BACKING_DIR) {
int ret = closedir(dir_fd->fd);
/* The only possible error value is EBADF, which would be a programming
* error.
*/
assert(ret == 0);
}
free(dir_fd);
dir_fd = NULL;
}
return 0;
}
/*============================================================================*/
/**
* Adds an entry to a readdir() buffer with a maildir file, representing the
* given notmuch messages.
*
* @param[in,out] dir_fd The opendir context.
* @param[in] p_message The notmuch message to add to the directory
* listing.
* @param[in,out] buf The readdir() buffer.
* @param[in] filler The filler function.
*
* @return A negative errno on error, 0 on success, #INT_MAX if the
* directory is too full to add this message.
*/
static int fill_dir_with_message (opendir_t *dir_fd,
notmuch_message_t *p_message,
void *buf,
fuse_fill_dir_t filler)
{
assert(p_message != NULL);
int res = 0;
const char *fname = notmuch_message_get_filename(p_message);
if (fname != NULL) {
struct stat stbuf;
if (stat(fname, &stbuf) == 0) {
char trans_name[PATH_MAX];
strncpy(trans_name, fname, PATH_MAX - 1);
trans_name[PATH_MAX - 1] = '\0';
string_replace(trans_name, '/', '#');
/* Perpetuate the file size inflation lie told in getattr(). */
stbuf.st_size += MAX_XLABEL_LENGTH;
LOG_TRACE("readdir filling dir %s at %ld\n",
trans_name, dir_fd->next_offset);
if (filler(buf, trans_name, &stbuf, dir_fd->next_offset++) != 0) {
LOG_TRACE("readdir filler full \"%s\".\n", trans_name);
dir_fd->next_offset--;
res = INT_MAX;
}
}
else if (errno == ENOENT) {
/* If a message is gone, don't stop the whole readdir(). */
fprintf(stderr, "WARNING: Skipping missing file \"%s\".\n", fname);
}
else {
fprintf(stderr, "ERROR: notmuch message stat error \"%s\" %s.\n", fname,
strerror(errno));
res = -errno;
}
}
else {
/* There's nothing we can do about this case, which I doubt can ever
* happen. Just ignore it.
*/
}
return res;
}
/*============================================================================*/
static int notmuchfs_readdir (const char *path,
void *buf,
fuse_fill_dir_t filler,
off_t offset_in,
struct fuse_file_info *fi)
{
(void)path;
int res = 0;
opendir_t *dir_fd = (opendir_t *)(uintptr_t)fi->fh;
switch (dir_fd->type) {
case OPENDIR_TYPE_NOTMUCH_QUERY:
{
if (offset_in == 0) {
filler(buf, ".", NULL, dir_fd->next_offset++);
filler(buf, "..", NULL, dir_fd->next_offset++);
}
else if (offset_in + 1 != dir_fd->next_offset) {
fprintf(stderr, "ERROR: discontiguous dir offsets %ld %ld.\n",
(long int)offset_in, (long int)dir_fd->next_offset);
res = -EDOM;
break;
}
notmuch_message_t *p_message = NULL;
while (res == 0 &&
(p_message = notmuch_messages_get(dir_fd->p_messages)) != NULL) {
res = fill_dir_with_message(dir_fd, p_message, buf, filler);
notmuch_message_destroy(p_message);
if (res == INT_MAX) {
res = 0;
break;
}
notmuch_messages_move_to_next(dir_fd->p_messages);
}
break;
}
case OPENDIR_TYPE_BACKING_DIR:
{
LOG_TRACE("readdir read from backing directory:\n");
seekdir(dir_fd->fd, offset_in);
struct dirent *de;
while ((de = readdir(dir_fd->fd)) != NULL) {
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, telldir(dir_fd->fd)) != 0) {
res = 0;
break;
}
}
break;
}
case OPENDIR_TYPE_EMPTY_DIR:
{
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
break;
}
case OPENDIR_TYPE_MAIL_DIR:
{
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "cur", NULL, 0);
filler(buf, "new", NULL, 0);
filler(buf, "tmp", NULL, 0);
break;
}
}
return res;
}
/*============================================================================*/
/**
* The string to replace the list of message tags with in the X-Label header,
* if the header will not fit in #MAX_XLABEL_LENGTH.
*/
#define TAG_ERROR_STRING "ERROR"
/**
* Fill the provided buffer with all the tags of the given message, comma
* separated. If they don't all fit, replace the whole string with
* #TAG_ERROR_STRING. No NULL termination.
*
* @param[in,out] buf_in The buffer to fill.
* @param[in] length The length of 'buf_in'.
* @param[in] p_message The message to read tags from.
* @return The number of bytes written to the buffer.
*/
static size_t fill_string_with_tags (char *buf_in,
size_t length,
notmuch_message_t *p_message)
{
char *buf = buf_in;
notmuch_tags_t *tags = notmuch_message_get_tags(p_message);
const char *tag_str = NULL;
bool error = FALSE;
while ((tag_str = notmuch_tags_get(tags)) != NULL) {
LOG_TRACE("Adding tag \"%s\" to X-label\n", tag_str);
/* If this tag can fit in the buffer, append it. Otherwise, error out. */
if (strlen(tag_str) >= length - (buf - buf_in)) {
error = TRUE;
break;
}
memcpy(buf, tag_str, strlen(tag_str));
buf += strlen(tag_str);
notmuch_tags_move_to_next(tags);
if (notmuch_tags_valid(tags)) {
/* There's another one coming, add separator. */
if (length - (buf - buf_in) < 1) {
error = TRUE;
break;
}
buf[0] = ',';
buf++;
}
}
if (error) {
LOG_TRACE("X-Label buffer overflow\n");
buf = buf_in;
memcpy(buf, TAG_ERROR_STRING, strlen(TAG_ERROR_STRING));
buf += strlen(TAG_ERROR_STRING);
}
notmuch_tags_destroy(tags);
return buf - buf_in;
}
/*============================================================================*/
/**
* A notmuchfs open file handle type, created by notmuchfs_open().
*/
typedef struct
{
/** The actual file handle. */
int fh;
/** The X-Label header - filled by open(), used later. */
char x_label[MAX_XLABEL_LENGTH];
} open_t;
static int notmuchfs_open (const char *path, struct fuse_file_info *fi)
{
if ((fi->flags & 3) != O_RDONLY)
return -EACCES;
open_t *p_open = malloc(sizeof(open_t));
memset(p_open, 0, sizeof(open_t));
char *last_slash = strrchr(path + 1, '/');
if (last_slash == NULL) {
p_open->fh = open(path + 1, O_RDONLY);
if (p_open->fh == -1) {
int err = errno;
free(p_open);
return -err;
}
}
else {
char trans_name[PATH_MAX];
strncpy(trans_name, last_slash + 1, PATH_MAX - 1);
trans_name[PATH_MAX - 1] = '\0';
char *first_pslash = strchr(trans_name, '#');
if (first_pslash != NULL) {
string_replace(trans_name, '#', '/');
struct fuse_context *p_fuse_ctx = fuse_get_context();
notmuch_context_t *p_ctx =
(notmuch_context_t *)p_fuse_ctx->private_data;
/**
* @todo shouldn't need writeable database here, but otherwise get:
* "Internal error: Failure to ensure database is writable"
* why?
*/
database_open(p_ctx, TRUE);
LOG_TRACE("open notmuch lookup by name: %s\n", trans_name);
notmuch_message_t *p_message;
if (notmuch_database_find_message_by_filename(p_ctx->db, trans_name,
&p_message) ==
NOTMUCH_STATUS_SUCCESS) {
if (p_message == NULL) {
LOG_TRACE("WARNING: Message not found in DB - ignoring.");
}
else {
char *buf = p_open->x_label;
/* Make sure the buffer is big enough to at least take the
* representation of overflow.
*/
assert(MAX_XLABEL_LENGTH >
strlen(XLABEL) + strlen(TAG_ERROR_STRING) + 1);
memcpy(buf, XLABEL, strlen(XLABEL));
buf += strlen(XLABEL);
buf += fill_string_with_tags(buf,
MAX_XLABEL_LENGTH - strlen(XLABEL) - 1,
p_message);
/* Pad the header out. RFC5322 doesn't say anything about this that I
* can see. NULs don't work, nor \n's, so spaces are used.
*/
while (buf - p_open->x_label < (MAX_XLABEL_LENGTH - 1)) {
assert(1 <= MAX_XLABEL_LENGTH - (buf - p_open->x_label));
buf[0] = ' ';
buf++;
}
assert(1 <= MAX_XLABEL_LENGTH - (buf - p_open->x_label));
buf[0] = '\n';
buf++;
notmuch_message_destroy(p_message);
}
database_close(p_ctx);
}
else {
/* Notmuch somehow failed to do anything successfully, fail the open. */
database_close(p_ctx);
free(p_open);
return -EIO;
}
}
LOG_TRACE("open(%s)\n", trans_name);
p_open->fh = open(trans_name, O_RDONLY);
if (p_open->fh == -1) {
int err = errno;
free(p_open);
return -err;
}
}
fi->fh = (uint64_t)(uintptr_t)p_open;
return 0;
}
/*============================================================================*/
static int notmuchfs_release (const char *path, struct fuse_file_info *fi)
{
(void)path;
open_t *p_open = (open_t *)(uintptr_t)fi->fh;
assert(p_open != NULL);
LOG_TRACE("close(%d)\n", p_open->fh);
int res = close(p_open->fh);
assert(res == 0);
free(p_open);
fi->fh = (uint64_t)(uintptr_t)NULL;
return 0; /* Documentation says this is ignored. */
}
/*============================================================================*/
static int notmuchfs_read (const char *path,
char *buf_in,
size_t size,
off_t offset,
struct fuse_file_info *fi)
{
(void)path;
char *buf = buf_in;
size_t offset_adj = MAX_XLABEL_LENGTH;
open_t *p_open = (open_t *)(uintptr_t)fi->fh;
assert(p_open != NULL);
if (offset < MAX_XLABEL_LENGTH) {
size_t bytes_to_copy = MIN((size_t)(MAX_XLABEL_LENGTH - offset), size);
memcpy(buf, p_open->x_label + offset, bytes_to_copy);
buf += bytes_to_copy;
offset_adj = offset + bytes_to_copy;
offset += bytes_to_copy;
}
size_t bytes_to_read = size - (buf - buf_in);
if (bytes_to_read > 0) {
LOG_TRACE("read(%s, %ld, %ld)\n", path, offset - offset_adj,
bytes_to_read);
ssize_t bytes_read = pread(p_open->fh, buf, bytes_to_read,
offset - offset_adj);
if (bytes_read == -1)
return -errno;
buf += bytes_read;
}
return (int)(buf - buf_in);
}
/*============================================================================*/
static int notmuchfs_mkdir (const char* path, mode_t mode)
{
assert(path[0] == '/');
if (mkdir(path + 1, mode) == -1)
return -errno;
return 0;
}
/*============================================================================*/
static int notmuchfs_rmdir (const char* path)
{
assert(path[0] == '/');
if (rmdir(path + 1) == -1)
return -errno;
return 0;
}
/*============================================================================*/
static int notmuchfs_rename (const char* from, const char* to)
{
assert(from[0] == '/');
assert(to[0] == '/');
char *last_pslash_from = strrchr(from + 1, '#');
char *last_pslash_to = strrchr(to + 1, '#');
char *last_slash_from = strrchr(from + 1, '/');
char *last_slash_to = strrchr(to + 1, '/');
/* Values are 0 (no workaround), 1 or 2 (see below). */
unsigned mutt_2476_workaround = 0;
if (last_pslash_from == NULL && last_pslash_to == NULL) {
/* Renaming from a non-maildir name to another non-maildir name - just pass
* it through.
*/
LOG_TRACE("rename(%s, %s)\n", from + 1, to + 1);
if (rename(from + 1, to + 1) == 0)
return 0;
else
return -errno;
}
if (last_pslash_from == NULL || last_pslash_to == NULL) {
/* Renaming from a non-maildir name to a maildir name, or vice versa.
* Doesn't make much sense - deny it.
*/
LOG_TRACE("ERROR: Rename die 1\n");
return -ENOTSUP;