-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsmu.c
1635 lines (1485 loc) · 49.6 KB
/
fsmu.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
#define FUSE_USE_VERSION 31
#include <dirent.h>
#include <errno.h>
#include <fuse.h>
#include <limits.h>
#include <pwd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
#include <utime.h>
static struct options {
const char *backing_dir;
const char *mu_home;
const char *mu;
int refresh_timeout;
int delete_remove;
int help;
} options;
static char *backing_dir_reverse;
#define OPTION(t, p) \
{ t, offsetof(struct options, p), 1 }
static const struct fuse_opt option_spec[] = {
OPTION("--backing-dir=%s", backing_dir),
OPTION("--muhome=%s", mu_home),
OPTION("--mu=%s", mu),
OPTION("--refresh-timeout=%d", refresh_timeout),
OPTION("--delete-remove", delete_remove),
OPTION("--help", help),
FUSE_OPT_END
};
/* Check whether the path is OK for further use. */
static void verify_path(const char *path)
{
if (strlen(path) > PATH_MAX) {
syslog(LOG_ERR, "verify_path: '%s' is too long", path);
abort();
}
}
/* Resolve a mount directory path into a backing directory path. */
static int resolve_path_noexists(const char *path, char *buf)
{
if (strchr(path + 1, '/') != NULL) {
sprintf(buf, "%s/_%s", options.backing_dir, path + 1);
return 0;
} else {
return -ENOENT;
}
}
/* Resolve a mount directory path into a backing directory path.
* Returns an error code if the backing directory path does not exist. */
static int resolve_path(const char *path, char *buf)
{
if (strchr(path + 1, '/') != NULL) {
sprintf(buf, "%s/_%s", options.backing_dir, path + 1);
struct stat stbuf;
int res = stat(buf, &stbuf);
if (res != 0) {
return -ENOENT;
}
return 0;
} else {
return -ENOENT;
}
}
/* Define init as a no-op. */
static void *fsmu_init(struct fuse_conn_info *conn)
{
return NULL;
}
/* Define open as a no-op. */
static int fsmu_open(const char *path, struct fuse_file_info *info)
{
return 0;
}
/* Define release as a no-op. */
static int fsmu_release(const char *path, struct fuse_file_info *info)
{
return 0;
}
/* Define truncate as a no-op. */
static int fsmu_truncate(const char *path, off_t offset)
{
return 0;
}
/* Get the directory name for the file in path, and write it to buf. */
static int dirname(const char *path, char *buf)
{
const char *last_slash = strrchr(path, '/');
if (!last_slash) {
syslog(LOG_ERR, "dirname: cannot get directory name "
"for '%s'", path);
return -1;
}
int length = last_slash - path;
strncpy(buf, path, length);
buf[length] = 0;
return 0;
}
/* Get the basename for the file in path, and write it to buf. */
static int basename(const char *path, char *buf)
{
const char *last_slash = strrchr(path, '/');
if (!last_slash) {
syslog(LOG_ERR, "basename: cannot get base name "
"for '%s'", path);
return -1;
}
int length = strlen(path) - (last_slash - path) - 1;
strncpy(buf, last_slash + 1, length);
buf[length] = 0;
return 0;
}
/* Returns a boolean indicating whether entry is "." or "..". */
static int is_upwards(const char *entry)
{
return ((strcmp(entry, ".") == 0)
|| (strcmp(entry, "..") == 0));
}
/* Make a new backing directory (a maildir with "cur" and "new"
* subdirectories) at the specified path, if it doesn't already exist.
* */
static int make_backing_dir_if_required(const char *backing_path)
{
struct stat stbuf;
memset(&stbuf, 0, sizeof(struct stat));
int res = stat(backing_path, &stbuf);
if (res != 0) {
res = mkdir(backing_path, 0755);
if (res != 0) {
syslog(LOG_ERR, "make_backing_dir_if_required: "
"cannot create '%s': %s",
backing_path, strerror(errno));
return -1;
}
}
char backing_path_cur[PATH_MAX];
strcpy(backing_path_cur, backing_path);
strcat(backing_path_cur, "/cur");
memset(&stbuf, 0, sizeof(struct stat));
res = stat(backing_path_cur, &stbuf);
if (res != 0) {
res = mkdir(backing_path_cur, 0755);
if (res != 0) {
syslog(LOG_ERR, "make_backing_dir_if_required: "
"cannot create '%s': %s",
backing_path_cur, strerror(errno));
return -1;
}
}
char backing_path_new[PATH_MAX];
strcpy(backing_path_new, backing_path);
strcat(backing_path_new, "/new");
memset(&stbuf, 0, sizeof(struct stat));
res = stat(backing_path_new, &stbuf);
if (res != 0) {
res = mkdir(backing_path_new, 0755);
if (res != 0) {
syslog(LOG_ERR, "make_backing_dir_if_required: "
"cannot create '%s': %s",
backing_path_new, strerror(errno));
return -1;
}
}
return 0;
}
/* Make a new directory at path, as well as any parent directories
* that don't already exist. */
static int mkdirp(const char *path)
{
struct stat st_buf;
memset(&st_buf, 0, sizeof(struct stat));
int res = stat(path, &st_buf);
if (res != 0) {
res = mkdir(path, 0755);
if ((res != 0) && (errno == ENOENT)) {
char dir[PATH_MAX];
res = dirname(path, dir);
if (res != 0) {
return -1;
}
res = mkdirp(dir);
if (res != 0) {
return -1;
}
res = mkdir(path, 0755);
if (res != 0) {
syslog(LOG_ERR, "mkdirp: cannot make directory "
"'%s': %s",
path, strerror(errno));
return -1;
}
}
}
return 0;
}
/* Write the reverse path for this maildir path and backing path pair
* to buf. (The reverse path is the concatenation of the reverse
* backing directory (see backing_dir_reverse), the maildir path, and
* the last three segments of the backing path. It is used when
* constructing link mappings, which in turn are maintained so that
* it's possible to find all backing paths for a given maildir path.)
* */
static int get_reverse_path(const char *maildir_path,
const char *backing_path,
char *buf)
{
char filename[PATH_MAX];
int res = basename(backing_path, filename);
if (res != 0) {
return -1;
}
char backing_dir[PATH_MAX];
res = dirname(backing_path, backing_dir);
if (res != 0) {
return -1;
}
char backing_dir_single[PATH_MAX];
res = basename(backing_dir, backing_dir_single);
if (res != 0) {
return -1;
}
char backing_dir2[PATH_MAX];
res = dirname(backing_dir, backing_dir2);
if (res != 0) {
return -1;
}
char backing_dir_single2[PATH_MAX];
res = basename(backing_dir2, backing_dir_single2);
if (res != 0) {
return -1;
}
strcpy(buf, backing_dir_reverse);
strcat(buf, maildir_path);
strcat(buf, "/");
strcat(buf, backing_dir_single2);
strcat(buf, "/");
strcat(buf, backing_dir_single);
strcat(buf, "/");
strcat(buf, filename);
return 0;
}
/* Add a link mapping for the maildir path and backing path pair (i.e.
* a link from the reverse path for this pair to the backing path). */
static int add_link_mapping(const char *maildir_path,
const char *backing_path)
{
char reverse_path[PATH_MAX];
int res = get_reverse_path(maildir_path, backing_path,
reverse_path);
if (res != 0) {
return -1;
}
char reverse_path_dir[PATH_MAX];
res = dirname(reverse_path, reverse_path_dir);
if (res != 0) {
return -1;
}
res = mkdirp(reverse_path_dir);
if (res != 0) {
return -1;
}
res = symlink(backing_path, reverse_path);
if (res != 0) {
syslog(LOG_ERR, "add_link_mapping: failed for '%s' to '%s': %s",
backing_path, reverse_path, strerror(errno));
return -1;
}
return 0;
}
/* Remove the link mapping for the maildir path and backing path pair.
* This will also remove any empty directories from further up the
* reverse path, as required. */
static int remove_link_mapping(const char *maildir_path,
const char *backing_path)
{
char reverse_path[PATH_MAX];
int res = get_reverse_path(maildir_path, backing_path,
reverse_path);
if (res != 0) {
syslog(LOG_ERR, "remove_link_mapping: "
"can't get reverse path for '%s', '%s'",
maildir_path, backing_path);
return -1;
}
res = unlink(reverse_path);
if (res != 0) {
syslog(LOG_ERR, "remove_link_mapping: "
"can't delete reverse path '%s': %s",
reverse_path, strerror(errno));
return -1;
}
char *last_slash = strrchr(reverse_path, '/');
*last_slash = 0;
res = rmdir(reverse_path);
if (res != 0) {
syslog(LOG_ERR, "remove_link_mapping: "
"can't remove directory '%s': %s",
reverse_path, strerror(errno));
return -1;
}
last_slash = strrchr(reverse_path, '/');
*last_slash = 0;
res = rmdir(reverse_path);
if (res != 0) {
syslog(LOG_ERR, "remove_link_mapping: "
"can't remove directory '%s': %s",
reverse_path, strerror(errno));
return -1;
}
for (;;) {
last_slash = strrchr(reverse_path, '/');
*last_slash = 0;
int len = strlen(reverse_path);
if (len >= 9) {
const char *tail = reverse_path + len - 9;
if (strcmp(tail, "/_reverse") == 0) {
return 0;
}
}
DIR *reverse_handle = opendir(reverse_path);
if (!reverse_handle) {
syslog(LOG_ERR, "remove_link_mapping: unable "
"to open directory '%s'",
reverse_path);
return -1;
}
struct dirent *dent;
int count = 0;
while ((dent = readdir(reverse_handle)) != NULL) {
if (is_upwards(dent->d_name)) {
continue;
}
count++;
}
closedir(reverse_handle);
if (count != 0) {
break;
}
res = rmdir(reverse_path);
if (res != 0) {
syslog(LOG_ERR, "remove_link_mapping: "
"can't remove top level '%s': %s",
reverse_path, strerror(errno));
return -1;
}
}
return 0;
}
/* Update an existing backing directory with the contents from the
* search results directory (temp_path). */
static int update_backing_dir(const char *backing_dir,
const char *temp_path)
{
struct dirent *dent;
struct stat stbuf;
DIR *backing_dir_handle = opendir(backing_dir);
if (!backing_dir_handle) {
syslog(LOG_ERR, "update_backing_dir: cannot open '%s': %s",
backing_dir, strerror(errno));
return -1;
}
while ((dent = readdir(backing_dir_handle)) != NULL) {
if (is_upwards(dent->d_name)) {
continue;
}
char temp_path_ent[PATH_MAX];
strcpy(temp_path_ent, temp_path);
strcat(temp_path_ent, dent->d_name);
memset(&stbuf, 0, sizeof(struct stat));
int res = stat(temp_path_ent, &stbuf);
if (res == 0) {
res = unlink(temp_path_ent);
if (res != 0) {
syslog(LOG_ERR, "update_backing_dir: unable to remove link "
"'%s' that already exists: %s",
dent->d_name, strerror(errno));
closedir(backing_dir_handle);
return -1;
}
} else {
char backing_dir_ent[PATH_MAX];
strcpy(backing_dir_ent, backing_dir);
strcat(backing_dir_ent, dent->d_name);
char maildir_path[PATH_MAX];
ssize_t len = readlink(backing_dir_ent, maildir_path, PATH_MAX);
if (len == PATH_MAX) {
syslog(LOG_ERR, "update_backing_dir: too much path "
"data for '%s",
backing_dir_ent);
closedir(backing_dir_handle);
return -1;
}
if (len == -1) {
syslog(LOG_ERR, "update_backing_dir: unable to read "
"link for '%s': %s",
backing_dir_ent, strerror(errno));
closedir(backing_dir_handle);
return -1;
}
maildir_path[len] = 0;
res = remove_link_mapping(maildir_path, backing_dir_ent);
if (res != 0) {
syslog(LOG_ERR, "update_backing_dir: unable "
"to remove link mapping");
closedir(backing_dir_handle);
return -1;
}
res = unlink(backing_dir_ent);
if (res != 0) {
syslog(LOG_ERR, "update_backing_dir: unable "
"to remove previous backing path "
"'%s': %s",
backing_dir_ent, strerror(errno));
closedir(backing_dir_handle);
return -1;
}
}
}
closedir(backing_dir_handle);
DIR *temp_dir_handle = opendir(temp_path);
if (!temp_dir_handle) {
syslog(LOG_ERR, "update_backing_dir: cannot open '%s': %s",
temp_path, strerror(errno));
return -1;
}
while ((dent = readdir(temp_dir_handle)) != NULL) {
if (is_upwards(dent->d_name)) {
continue;
}
char backing_dir_ent[PATH_MAX];
strcpy(backing_dir_ent, backing_dir);
strcat(backing_dir_ent, dent->d_name);
char temp_path_ent[PATH_MAX];
strcpy(temp_path_ent, temp_path);
strcat(temp_path_ent, dent->d_name);
int res = rename(temp_path_ent, backing_dir_ent);
if (res != 0) {
syslog(LOG_ERR, "update_backing_dir: unable to "
"rename link ('%s' -> '%s'): %s",
temp_path_ent, backing_dir_ent, strerror(errno));
closedir(temp_dir_handle);
return -1;
}
char maildir_path[PATH_MAX];
ssize_t len = readlink(backing_dir_ent, maildir_path, PATH_MAX);
if (len == PATH_MAX) {
syslog(LOG_ERR, "update_backing_dir: too much path "
"data for '%s'",
backing_dir_ent);
closedir(temp_dir_handle);
return -1;
}
if (len == -1) {
syslog(LOG_ERR, "update_backing_dir: unable to read "
"link for '%s': %s",
backing_dir_ent, strerror(errno));
closedir(temp_dir_handle);
return -1;
}
maildir_path[len] = 0;
add_link_mapping(maildir_path, backing_dir_ent);
}
closedir(temp_dir_handle);
return 0;
}
/* Remove a temporary mail directory and its contents recursively.
* This will remove as many files/directories as possible before
* returning. */
static int remove_dir(const char *dir_path)
{
DIR *dir_handle = opendir(dir_path);
if (!dir_handle) {
syslog(LOG_ERR, "remove_dir: cannot open '%s': %s",
dir_handle, strerror(errno));
return -1;
}
struct dirent *dent;
struct stat stbuf;
while ((dent = readdir(dir_handle)) != NULL) {
if (is_upwards(dent->d_name)) {
continue;
}
char path[PATH_MAX];
strcpy(path, dir_path);
strcat(path, "/");
strcat(path, dent->d_name);
int res = lstat(path, &stbuf);
if (res != 0) {
syslog(LOG_INFO, "remove_dir: cannot lstat '%s': %s",
path, strerror(errno));
} else if (S_ISDIR(stbuf.st_mode)) {
int res = remove_dir(path);
if (res != 0) {
syslog(LOG_ERR, "remove_dir: cannot remove '%s': %s",
path, strerror(errno));
}
} else {
int res = unlink(path);
if (res != 0) {
syslog(LOG_ERR, "remove_dir: cannot unlink '%s': %s",
path, strerror(errno));
}
}
}
closedir(dir_handle);
int res = rmdir(dir_path);
if (res != 0) {
syslog(LOG_ERR, "remove_dir: cannot unlink directory '%s': %s",
dir_path, strerror(errno));
return -1;
}
return 0;
}
/* Refresh the search results for a given mount directory. If force
* is false, then refresh will not happen if the timeout for the
* corresponding backing directory has not been reached. If force is
* true, then refresh will always happen. */
static int refresh_dir(const char *path, int force)
{
syslog(LOG_DEBUG, "refresh_dir: '%s'", path);
verify_path(path);
if ((strcmp(path, "/") == 0)
|| (strlen(path) <= 1)
|| (path[1] == '_')) {
syslog(LOG_DEBUG, "refresh_dir: '%s' cannot be refreshed", path);
return 0;
}
char root_dirname[PATH_MAX];
strcpy(root_dirname, path);
char *separator = strchr(root_dirname + 1, '/');
if (separator != NULL) {
*separator = 0;
}
char search_path[PATH_MAX];
sprintf(search_path, "%s%s", options.backing_dir, root_dirname);
struct stat stbuf;
int res = stat(search_path, &stbuf);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: '%s' cannot be refreshed", path);
return -1;
}
char last_update_path[PATH_MAX];
sprintf(last_update_path, "%s.last-update", search_path);
res = stat(last_update_path, &stbuf);
if (res == 0) {
int threshold = time(NULL) - options.refresh_timeout;
if (!force && (stbuf.st_mtim.tv_sec > threshold)) {
syslog(LOG_DEBUG, "refresh_dir: '%s' refreshed "
"less than %ds ago", path,
options.refresh_timeout);
return 0;
}
} else if (errno == ENOENT) {
FILE *last_update_file = fopen(last_update_path, "w");
if (!last_update_file) {
syslog(LOG_ERR, "refresh_dir: cannot write "
"last-update for '%s': %s",
path, strerror(errno));
return -1;
}
res = fclose(last_update_file);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot close "
"last-update for '%s': %s",
path, strerror(errno));
return -1;
}
}
res = utime(last_update_path, NULL);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot update "
"last-update for '%s': %s",
path, strerror(errno));
return -1;
}
char query[PATH_MAX];
strcpy(query, root_dirname + 1);
char backing_path[PATH_MAX];
sprintf(backing_path, "%s/_%s", options.backing_dir, query);
int len = strlen(query);
for (int i = 0; i < len; i++) {
if (query[i] == '+') {
query[i] = '/';
}
}
char template[PATH_MAX];
strcpy(template, options.backing_dir);
strcat(template, "/_tempdir.XXXXXX");
char *temp_dirname = mkdtemp(template);
if (!temp_dirname) {
syslog(LOG_ERR, "refresh_dir: unable to make temporary "
"directory (%s): %s",
template, strerror(errno));
return -1;
}
char cmd[4 * PATH_MAX];
sprintf(cmd, "%s find %s%s --clearlinks --format=links "
"--linksdir='%s' '%s'",
options.mu,
(options.mu_home ? "--muhome=" : ""),
(options.mu_home ? options.mu_home : ""),
temp_dirname, query);
syslog(LOG_INFO, "refresh_dir: running mu find: '%s'", cmd);
res = system(cmd);
/* 2 is the documented return code for "no results found". 1024
* is the return code seen in practice. */
if ((res != 0) && (res != 2) && (res != 1024)) {
syslog(LOG_ERR, "refresh_dir: mu find failed");
remove_dir(temp_dirname);
return -1;
}
res = make_backing_dir_if_required(backing_path);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot make backing directory");
remove_dir(temp_dirname);
return -1;
}
char backing_path_cur[PATH_MAX];
strcpy(backing_path_cur, backing_path);
strcat(backing_path_cur, "/cur/");
char temp_path_cur[PATH_MAX];
strcpy(temp_path_cur, temp_dirname);
strcat(temp_path_cur, "/cur/");
res = update_backing_dir(backing_path_cur, temp_path_cur);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot update backing "
"directory '%s' (from '%s')",
backing_path_cur, temp_path_cur);
remove_dir(temp_dirname);
return -1;
}
char backing_path_new[PATH_MAX];
strcpy(backing_path_new, backing_path);
strcat(backing_path_new, "/new/");
char temp_path_new[PATH_MAX];
strcpy(temp_path_new, temp_dirname);
strcat(temp_path_new, "/new/");
res = update_backing_dir(backing_path_new, temp_path_new);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot update backing "
"directory '%s' (from '%s')",
backing_path_new, temp_path_new);
remove_dir(temp_dirname);
return -1;
}
char tempdir_part[PATH_MAX];
strcpy(tempdir_part, temp_dirname);
strcat(tempdir_part, "/new");
res = rmdir(tempdir_part);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot remove temp/new: %s",
strerror(errno));
remove_dir(temp_dirname);
return -1;
}
strcpy(tempdir_part, temp_dirname);
strcat(tempdir_part, "/cur");
res = rmdir(tempdir_part);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot remove temp/cur: %s",
strerror(errno));
remove_dir(temp_dirname);
return -1;
}
strcpy(tempdir_part, temp_dirname);
strcat(tempdir_part, "/tmp");
res = rmdir(tempdir_part);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot remove temp/tmp: %s",
strerror(errno));
remove_dir(temp_dirname);
return -1;
}
DIR *temp_dir_handle = opendir(temp_dirname);
if (!temp_dir_handle) {
syslog(LOG_ERR, "refresh_dir: cannot open '%s': %s",
temp_dirname, strerror(errno));
remove_dir(temp_dirname);
return -1;
}
struct dirent *dent;
while ((dent = readdir(temp_dir_handle)) != NULL) {
if (is_upwards(dent->d_name)) {
continue;
}
char path[PATH_MAX];
strcpy(path, temp_dirname);
strcat(path, "/");
strcat(path, dent->d_name);
res = unlink(path);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot unlink '%s': %s",
path, strerror(errno));
closedir(temp_dir_handle);
remove_dir(temp_dirname);
return -1;
}
}
closedir(temp_dir_handle);
res = rmdir(temp_dirname);
if (res != 0) {
syslog(LOG_ERR, "refresh_dir: cannot remove temp: %s",
strerror(errno));
remove_dir(temp_dirname);
return -1;
}
return 0;
}
/* Read the contents of the mount directory at path. */
static int fsmu_readdir(const char *path, void *buf,
fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *info)
{
syslog(LOG_DEBUG, "readdir: '%s'", path);
verify_path(path);
if (strcmp(path, "/") == 0) {
DIR *backing_dir_handle = opendir(options.backing_dir);
if (!backing_dir_handle) {
syslog(LOG_ERR, "readdir: cannot open '%s': %s",
path, strerror(errno));
return -1;
}
struct dirent *dent;
while ((dent = readdir(backing_dir_handle)) != NULL) {
if (dent->d_name[0] == '_') {
continue;
}
filler(buf, dent->d_name, 0, 0);
}
closedir(backing_dir_handle);
syslog(LOG_DEBUG, "readdir: '%s' completed", path);
return 0;
}
char backing_path[PATH_MAX];
int res = resolve_path(path, backing_path);
if (res != 0) {
if (path[1] == '_') {
return -ENOENT;
}
refresh_dir(path, 0);
sprintf(backing_path, "%s/_%s", options.backing_dir, path + 1);
}
DIR *dir_handle = opendir(backing_path);
if (!dir_handle) {
syslog(LOG_ERR, "readdir: cannot open '%s': %s",
path, strerror(errno));
return -1;
}
struct dirent *dent;
while ((dent = readdir(dir_handle)) != NULL) {
filler(buf, dent->d_name, 0, 0);
}
closedir(dir_handle);
syslog(LOG_DEBUG, "readdir: '%s' completed", path);
return 0;
}
/* Get the attributes for the specified mount path. */
static int fsmu_getattr(const char *path, struct stat *stbuf)
{
syslog(LOG_DEBUG, "getattr: '%s'", path);
verify_path(path);
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
syslog(LOG_DEBUG, "getattr: '%s' completed", path);
return 0;
}
char backing_path[PATH_MAX];
int res = resolve_path_noexists(path, backing_path);
if (res != 0) {
sprintf(backing_path, "%s%s", options.backing_dir, path);
}
int len = strlen(backing_path);
if (len >= 4) {
const char *tail = backing_path + len - 4;
if ( (strcmp(tail, "/cur") == 0)
|| (strcmp(tail, "/new") == 0)) {
syslog(LOG_INFO, "getattr: refreshing cur/new path");
refresh_dir(path, 0);
}
}
if (len >= 9) {
const char *tail = backing_path + len - 9;
if (strcmp(tail, "/.refresh") == 0) {
stbuf->st_mode = S_IFREG;
/* This previously used to report 0, but a change
* somewhere else (possibly in a newer version of FUSE)
* means that if the size is reported as 0, reading the
* file doesn't actually hit fsmu_read, and the refresh
* doesn't happen. Changing it to have a size of 1
* 'fixes' this. */
stbuf->st_size = 1;
return 0;
}
}
res = stat(backing_path, stbuf);
if (res != 0) {
syslog(LOG_ERR, "getattr: unable to stat '%s': %s",
path, strerror(errno));
return -1 * errno;
}
syslog(LOG_DEBUG, "getattr: '%s' completed", path);
return res;
}
/* Update the link mappings for the given maildir path (being renamed
* to new_maildir_path). If flags are not being set (this happens
* when the new path involves more than flag modification), then
* basename_new must be set. */
static int update_link_mapping(const char *maildir_path,
const char *new_maildir_path,
const char *basename_new,
const char *flags)
{
char reverse_path[PATH_MAX];
strcpy(reverse_path, backing_dir_reverse);
strcat(reverse_path, "/");
strcat(reverse_path, maildir_path);
struct dirent *dent;
struct dirent *dent_search;
struct dirent *dent_type;
DIR *reverse_handle = opendir(reverse_path);
if (!reverse_handle) {
syslog(LOG_ERR, "update_link_mapping: cannot open '%s': %s",
reverse_path, strerror(errno));
return -1;
}
int reverse_error = 0;
while ((dent = readdir(reverse_handle)) != NULL) {
if (is_upwards(dent->d_name)) {
continue;
}
char search_path[PATH_MAX];
strcpy(search_path, reverse_path);
strcat(search_path, "/");
strcat(search_path, dent->d_name);
DIR *search_dir_handle = opendir(search_path);
if (!search_dir_handle) {
syslog(LOG_ERR, "update_link_mapping: cannot open search path '%s': %s",
search_path, strerror(errno));
reverse_error = 1;
break;
}
int search_error = 0;
while ((dent_search = readdir(search_dir_handle)) != NULL) {
if (is_upwards(dent_search->d_name)) {
continue;
}
char type_path[PATH_MAX];
strcpy(type_path, search_path);
strcat(type_path, "/");
strcat(type_path, dent_search->d_name);
DIR *type_dir_handle = opendir(type_path);
if (!type_dir_handle) {
syslog(LOG_ERR, "update_link_mapping: cannot open type path '%s': %s",
type_path, strerror(errno));
search_error = 1;
break;
}
int type_error = 0;
while ((dent_type = readdir(type_dir_handle)) != NULL) {
if (is_upwards(dent_type->d_name)) {
continue;
}
char reverse_path_full[PATH_MAX];
strcpy(reverse_path_full, type_path);
strcat(reverse_path_full, "/");
strcat(reverse_path_full, dent_type->d_name);
char backing_path[PATH_MAX];
ssize_t len = readlink(reverse_path_full, backing_path, PATH_MAX);
if (len == PATH_MAX) {
syslog(LOG_ERR, "update_link_mapping: too much path data for '%s'",
reverse_path_full);
type_error = 1;
break;
}
if (len == -1) {
syslog(LOG_ERR, "update_link_mapping: unable to read link for '%s': %s",
reverse_path_full, strerror(errno));
type_error = 1;
break;
}
backing_path[len] = 0;
int res = remove_link_mapping(maildir_path, backing_path);
if (res != 0) {
syslog(LOG_ERR, "update_link_mapping: cannot remove old link mapping");
type_error = 1;
break;
}
res = unlink(backing_path);
if (res != 0) {
syslog(LOG_ERR, "update_link_mapping: cannot remove old backing path");
type_error = 1;
break;
}
char backing_path_new[PATH_MAX];
char backing_path_dir[PATH_MAX];
res = dirname(backing_path, backing_path_dir);
if (res != 0) {
type_error = 1;
break;
}
char backing_path_dir2[PATH_MAX];
res = dirname(backing_path_dir, backing_path_dir2);
if (res != 0) {
type_error = 1;
break;
}
char new_maildir_path_dir[PATH_MAX];