forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.c
993 lines (832 loc) · 22.5 KB
/
compress.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
/**
* @file
* Compressed mbox local mailbox type
*
* @authors
* Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
* Copyright (C) 2016-2018 Richard Russon <rich@flatcap.org>
*
* @copyright
* This program 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 2 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
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page compress Compressed mbox local mailbox type
*
* Compressed mbox local mailbox type
*
* @note
* Any references to compressed files also apply to encrypted files.
* - mailbox->path == plaintext file
* - mailbox->realpath == compressed file
*/
#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "compress.h"
#include "account.h"
#include "context.h"
#include "curs_lib.h"
#include "format_flags.h"
#include "globals.h"
#include "hook.h"
#include "mailbox.h"
#include "mutt_curses.h"
#include "muttlib.h"
#include "mx.h"
#include "protos.h"
struct Email;
/**
* lock_realpath - Try to lock the ctx->realpath
* @param m Mailbox to lock
* @param excl Lock exclusively?
* @retval true Success (locked or readonly)
* @retval false Error (can't lock the file)
*
* Try to (exclusively) lock the mailbox. If we succeed, then we mark the
* mailbox as locked. If we fail, but we didn't want exclusive rights, then
* the mailbox will be marked readonly.
*/
static bool lock_realpath(struct Mailbox *m, bool excl)
{
if (!m || !m->compress_info)
return false;
struct CompressInfo *ci = m->compress_info;
if (ci->locked)
return true;
if (excl)
ci->fp_lock = fopen(m->realpath, "a");
else
ci->fp_lock = fopen(m->realpath, "r");
if (!ci->fp_lock)
{
mutt_perror(m->realpath);
return false;
}
int r = mutt_file_lock(fileno(ci->fp_lock), excl, true);
if (r == 0)
ci->locked = true;
else if (excl)
{
mutt_file_fclose(&ci->fp_lock);
m->readonly = true;
return true;
}
return r == 0;
}
/**
* unlock_realpath - Unlock the mailbox->realpath
* @param m Mailbox to unlock
*
* Unlock a mailbox previously locked by lock_mailbox().
*/
static void unlock_realpath(struct Mailbox *m)
{
if (!m || !m->compress_info)
return;
struct CompressInfo *ci = m->compress_info;
if (!ci->locked)
return;
mutt_file_unlock(fileno(ci->fp_lock));
ci->locked = false;
mutt_file_fclose(&ci->fp_lock);
}
/**
* setup_paths - Set the mailbox paths
* @param m Mailbox to modify
* @retval 0 Success
* @retval -1 Error
*
* Save the compressed filename in mailbox->realpath.
* Create a temporary filename and put its name in mailbox->path.
* The temporary file is created to prevent symlink attacks.
*/
static int setup_paths(struct Mailbox *m)
{
if (!m)
return -1;
char tmp[PATH_MAX];
/* Setup the right paths */
mutt_str_strfcpy(m->realpath, m->path, sizeof(m->realpath));
/* We will uncompress to /tmp */
mutt_mktemp(tmp, sizeof(tmp));
mutt_str_strfcpy(m->path, tmp, sizeof(m->path));
FILE *fp = mutt_file_fopen(m->path, "w");
if (!fp)
return -1;
mutt_file_fclose(&fp);
return 0;
}
/**
* store_size - Save the size of the compressed file
* @param m Mailbox
*
* Save the compressed file size in the compress_info struct.
*/
static void store_size(const struct Mailbox *m)
{
if (!m || !m->compress_info)
return;
struct CompressInfo *ci = m->compress_info;
ci->size = mutt_file_get_size(m->realpath);
}
/**
* find_hook - Find a hook to match a path
* @param type Type of hook, see #HookFlags
* @param path Filename to test
* @retval ptr Matching hook command
* @retval NULL No matches
*
* Each hook has a type and a pattern.
* Find a command that matches the type and path supplied. e.g.
*
* User config:
* open-hook '\.gz$' "gzip -cd '%f' > '%t'"
*
* Call:
* find_hook (#MUTT_OPEN_HOOK, "myfile.gz");
*/
static const char *find_hook(HookFlags type, const char *path)
{
if (!path)
return NULL;
const char *c = mutt_find_hook(type, path);
if (!c || !*c)
return NULL;
return c;
}
/**
* set_compress_info - Find the compress hooks for a mailbox
* @param m Mailbox to examine
* @retval ptr CompressInfo Hook info for the mailbox's path
* @retval NULL Error
*
* When a mailbox is opened, we check if there are any matching hooks.
*/
static struct CompressInfo *set_compress_info(struct Mailbox *m)
{
if (!m)
return NULL;
if (m->compress_info)
return m->compress_info;
/* Open is compulsory */
const char *o = find_hook(MUTT_OPEN_HOOK, m->path);
if (!o)
return NULL;
const char *c = find_hook(MUTT_CLOSE_HOOK, m->path);
const char *a = find_hook(MUTT_APPEND_HOOK, m->path);
struct CompressInfo *ci = mutt_mem_calloc(1, sizeof(struct CompressInfo));
m->compress_info = ci;
ci->cmd_open = mutt_str_strdup(o);
ci->cmd_close = mutt_str_strdup(c);
ci->cmd_append = mutt_str_strdup(a);
return ci;
}
/**
* free_compress_info - Frees the compress info members and structure
* @param m Mailbox to free compress_info for
*/
static void free_compress_info(struct Mailbox *m)
{
if (!m || !m->compress_info)
return;
struct CompressInfo *ci = m->compress_info;
FREE(&ci->cmd_open);
FREE(&ci->cmd_close);
FREE(&ci->cmd_append);
unlock_realpath(m);
FREE(&m->compress_info);
}
/**
* compress_format_str - Expand the filenames in a command string - Implements ::format_t
*
* | Expando | Description
* |:--------|:--------------------------------------------------------
* | \%f | Compressed file
* | \%t | Plaintext, temporary file
*/
static const char *compress_format_str(char *buf, size_t buflen, size_t col, int cols,
char op, const char *src, const char *prec,
const char *if_str, const char *else_str,
unsigned long data, MuttFormatFlags flags)
{
if (!buf || (data == 0))
return src;
struct Mailbox *m = (struct Mailbox *) data;
const char *stuffing = NULL;
switch (op)
{
case 'f':
/* Compressed file */
stuffing = mutt_path_escape(m->realpath);
break;
case 't':
/* Plaintext, temporary file */
stuffing = mutt_path_escape(m->path);
break;
}
snprintf(buf, buflen, "%s", NONULL(stuffing));
return src;
}
/**
* expand_command_str - Expand placeholders in command string
* @param m Mailbox for paths
* @param cmd Template command to be expanded
* @param buf Buffer to store the command
* @param buflen Size of the buffer
*
* This function takes a hook command and expands the filename placeholders
* within it. The function calls mutt_expando_format() to do the replacement
* which calls our callback function compress_format_str(). e.g.
*
* Template command:
* gzip -cd '%f' > '%t'
*
* Result:
* gzip -dc '~/mail/abc.gz' > '/tmp/xyz'
*/
static void expand_command_str(const struct Mailbox *m, const char *cmd, char *buf, int buflen)
{
if (!m || !cmd || !buf)
return;
mutt_expando_format(buf, buflen, 0, buflen, cmd, compress_format_str,
(unsigned long) m, 0);
}
/**
* execute_command - Run a system command
* @param m Mailbox to work with
* @param command Command string to execute
* @param progress Message to show the user
* @retval 1 Success
* @retval 0 Failure
*
* Run the supplied command, taking care of all the NeoMutt requirements,
* such as locking files and blocking signals.
*/
static int execute_command(struct Mailbox *m, const char *command, const char *progress)
{
int rc = 1;
char sys_cmd[STR_COMMAND];
if (!m || !command || !progress)
return 0;
if (!m->quiet)
{
mutt_message(progress, m->realpath);
}
mutt_sig_block();
endwin();
fflush(stdout);
expand_command_str(m, command, sys_cmd, sizeof(sys_cmd));
if (mutt_system(sys_cmd) != 0)
{
rc = 0;
mutt_any_key_to_continue(NULL);
mutt_error(_("Error running \"%s\""), sys_cmd);
}
mutt_sig_unblock();
return rc;
}
/**
* mutt_comp_can_append - Can we append to this path?
* @param m Mailbox
* @retval true Yes, we can append to the file
* @retval false No, appending isn't possible
*
* To append to a file we can either use an 'append-hook' or a combination of
* 'open-hook' and 'close-hook'.
*
* A match means it's our responsibility to append to the file.
*/
bool mutt_comp_can_append(struct Mailbox *m)
{
if (!m)
return false;
/* If this succeeds, we know there's an open-hook */
struct CompressInfo *ci = set_compress_info(m);
if (!ci)
return false;
/* We have an open-hook, so to append we need an append-hook,
* or a close-hook. */
if (ci->cmd_append || ci->cmd_close)
return true;
mutt_error(_("Cannot append without an append-hook or close-hook : %s"), m->path);
return false;
}
/**
* mutt_comp_can_read - Can we read from this file?
* @param path Pathname of file to be tested
* @retval true Yes, we can read the file
* @retval false No, we cannot read the file
*
* Search for an 'open-hook' with a regex that matches the path.
*
* A match means it's our responsibility to open the file.
*/
bool mutt_comp_can_read(const char *path)
{
if (!path)
return false;
if (find_hook(MUTT_OPEN_HOOK, path))
return true;
else
return false;
}
/**
* mutt_comp_valid_command - Is this command string allowed?
* @param cmd Command string
* @retval 1 Valid command
* @retval 0 "%f" and/or "%t" is missing
*
* A valid command string must have both "%f" (from file) and "%t" (to file).
* We don't check if we can actually run the command.
*/
int mutt_comp_valid_command(const char *cmd)
{
if (!cmd)
return 0;
return strstr(cmd, "%f") && strstr(cmd, "%t");
}
/**
* comp_ac_find - Find a Account that matches a Mailbox path
*/
struct Account *comp_ac_find(struct Account *a, const char *path)
{
return NULL;
}
/**
* comp_ac_add - Add a Mailbox to a Account
*/
int comp_ac_add(struct Account *a, struct Mailbox *m)
{
if (!a || !m)
return -1;
if (m->magic != MUTT_COMPRESSED)
return -1;
m->account = a;
struct MailboxNode *np = mutt_mem_calloc(1, sizeof(*np));
np->mailbox = m;
STAILQ_INSERT_TAIL(&a->mailboxes, np, entries);
return 0;
}
/**
* comp_mbox_open - Implements MxOps::mbox_open()
*
* Set up a compressed mailbox to be read.
* Decompress the mailbox and set up the paths and hooks needed.
* Then determine the type of the mailbox so we can delegate the handling of
* messages.
*/
static int comp_mbox_open(struct Mailbox *m)
{
if (!m || (m->magic != MUTT_COMPRESSED))
return -1;
struct CompressInfo *ci = set_compress_info(m);
if (!ci)
return -1;
/* If there's no close-hook, or the file isn't writable */
if (!ci->cmd_close || (access(m->path, W_OK) != 0))
m->readonly = true;
if (setup_paths(m) != 0)
goto cmo_fail;
store_size(m);
if (!lock_realpath(m, false))
{
mutt_error(_("Unable to lock mailbox"));
goto cmo_fail;
}
int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
if (rc == 0)
goto cmo_fail;
unlock_realpath(m);
m->magic = mx_path_probe(m->path, NULL);
if (m->magic == MUTT_UNKNOWN)
{
mutt_error(_("Can't identify the contents of the compressed file"));
goto cmo_fail;
}
ci->child_ops = mx_get_ops(m->magic);
if (!ci->child_ops)
{
mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->magic);
goto cmo_fail;
}
m->account->magic = m->magic;
return ci->child_ops->mbox_open(m);
cmo_fail:
/* remove the partial uncompressed file */
remove(m->path);
free_compress_info(m);
return -1;
}
/**
* comp_mbox_open_append - Implements MxOps::mbox_open_append()
*
* flags may also contain #MUTT_NEWFOLDER
*
* To append to a compressed mailbox we need an append-hook (or both open- and
* close-hooks).
*/
static int comp_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
{
if (!m)
return -1;
/* If this succeeds, we know there's an open-hook */
struct CompressInfo *ci = set_compress_info(m);
if (!ci)
return -1;
/* To append we need an append-hook or a close-hook */
if (!ci->cmd_append && !ci->cmd_close)
{
mutt_error(_("Cannot append without an append-hook or close-hook : %s"), m->path);
goto cmoa_fail1;
}
if (setup_paths(m) != 0)
goto cmoa_fail2;
/* Lock the realpath for the duration of the append.
* It will be unlocked in the close */
if (!lock_realpath(m, true))
{
mutt_error(_("Unable to lock mailbox"));
goto cmoa_fail2;
}
/* Open the existing mailbox, unless we are appending */
if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
{
int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
if (rc == 0)
{
mutt_error(_("Compress command failed: %s"), ci->cmd_open);
goto cmoa_fail2;
}
m->magic = mx_path_probe(m->path, NULL);
}
else
m->magic = C_MboxType;
/* We can only deal with mbox and mmdf mailboxes */
if ((m->magic != MUTT_MBOX) && (m->magic != MUTT_MMDF))
{
mutt_error(_("Unsupported mailbox type for appending"));
goto cmoa_fail2;
}
ci->child_ops = mx_get_ops(m->magic);
if (!ci->child_ops)
{
mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->magic);
goto cmoa_fail2;
}
if (ci->child_ops->mbox_open_append(m, flags) != 0)
goto cmoa_fail2;
return 0;
cmoa_fail2:
/* remove the partial uncompressed file */
remove(m->path);
cmoa_fail1:
/* Free the compress_info to prevent close from trying to recompress */
free_compress_info(m);
return -1;
}
/**
* comp_mbox_check - Implements MxOps::mbox_check()
* @param m Mailbox
* @param index_hint Currently selected mailbox
* @retval 0 Mailbox OK
* @retval #MUTT_REOPENED The mailbox was closed and reopened
* @retval -1 Mailbox bad
*
* If the compressed file changes in size but the mailbox hasn't been changed
* in NeoMutt, then we can close and reopen the mailbox.
*
* If the mailbox has been changed in NeoMutt, warn the user.
*
* The return codes are picked to match mx_mbox_check().
*/
static int comp_mbox_check(struct Mailbox *m, int *index_hint)
{
if (!m || !m->compress_info)
return -1;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops)
return -1;
int size = mutt_file_get_size(m->realpath);
if (size == ci->size)
return 0;
if (!lock_realpath(m, false))
{
mutt_error(_("Unable to lock mailbox"));
return -1;
}
int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
store_size(m);
unlock_realpath(m);
if (rc == 0)
return -1;
return ops->mbox_check(m, index_hint);
}
/**
* comp_mbox_sync - Implements MxOps::mbox_sync()
*
* Changes in NeoMutt only affect the tmp file.
* Calling comp_mbox_sync() will commit them to the compressed file.
*/
static int comp_mbox_sync(struct Mailbox *m, int *index_hint)
{
if (!m || !m->compress_info)
return -1;
struct CompressInfo *ci = m->compress_info;
if (!ci->cmd_close)
{
mutt_error(_("Can't sync a compressed file without a close-hook"));
return -1;
}
const struct MxOps *ops = ci->child_ops;
if (!ops)
return -1;
if (!lock_realpath(m, true))
{
mutt_error(_("Unable to lock mailbox"));
return -1;
}
int rc = comp_mbox_check(m, index_hint);
if (rc != 0)
goto sync_cleanup;
rc = ops->mbox_sync(m, index_hint);
if (rc != 0)
goto sync_cleanup;
rc = execute_command(m, ci->cmd_close, _("Compressing %s"));
if (rc == 0)
{
rc = -1;
goto sync_cleanup;
}
rc = 0;
sync_cleanup:
store_size(m);
unlock_realpath(m);
return rc;
}
/**
* comp_mbox_close - Implements MxOps::mbox_close()
*
* If the mailbox has been changed then re-compress the tmp file.
* Then delete the tmp file.
*/
static int comp_mbox_close(struct Mailbox *m)
{
if (!m || !m->compress_info)
return -1;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops)
{
free_compress_info(m);
return -1;
}
ops->mbox_close(m);
/* sync has already been called, so we only need to delete some files */
if (m->append)
{
const char *append = NULL;
const char *msg = NULL;
/* The file exists and we can append */
if ((access(m->realpath, F_OK) == 0) && ci->cmd_append)
{
append = ci->cmd_append;
msg = _("Compressed-appending to %s...");
}
else
{
append = ci->cmd_close;
msg = _("Compressing %s...");
}
int rc = execute_command(m, append, msg);
if (rc == 0)
{
mutt_any_key_to_continue(NULL);
mutt_error(_("Error. Preserving temporary file: %s"), m->path);
}
else
remove(m->path);
unlock_realpath(m);
}
else
{
/* If the file was removed, remove the compressed folder too */
if ((access(m->path, F_OK) != 0) && !C_SaveEmpty)
{
remove(m->realpath);
}
else
{
remove(m->path);
}
}
free_compress_info(m);
return 0;
}
/**
* comp_msg_open - Implements MxOps::msg_open()
*/
static int comp_msg_open(struct Mailbox *m, struct Message *msg, int msgno)
{
if (!m || !m->compress_info)
return -1;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops)
return -1;
/* Delegate */
return ops->msg_open(m, msg, msgno);
}
/**
* comp_msg_open_new - Implements MxOps::msg_open_new()
*/
static int comp_msg_open_new(struct Mailbox *m, struct Message *msg, struct Email *e)
{
if (!m || !m->compress_info)
return -1;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops)
return -1;
/* Delegate */
return ops->msg_open_new(m, msg, e);
}
/**
* comp_msg_commit - Implements MxOps::msg_commit()
*/
static int comp_msg_commit(struct Mailbox *m, struct Message *msg)
{
if (!m || !m->compress_info)
return -1;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops)
return -1;
/* Delegate */
return ops->msg_commit(m, msg);
}
/**
* comp_msg_close - Implements MxOps::msg_close()
*/
static int comp_msg_close(struct Mailbox *m, struct Message *msg)
{
if (!m || !m->compress_info)
return -1;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops)
return -1;
/* Delegate */
return ops->msg_close(m, msg);
}
/**
* comp_msg_padding_size - Bytes of padding between messages - Implements MxOps::msg_padding_size()
*/
static int comp_msg_padding_size(struct Mailbox *m)
{
if (!m || !m->compress_info)
return 0;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops || !ops->msg_padding_size)
return 0;
return ops->msg_padding_size(m);
}
/**
* comp_msg_save_hcache - Save message to the header cache - Implements MxOps::msg_save_hcache()
*/
static int comp_msg_save_hcache(struct Mailbox *m, struct Email *e)
{
if (!m || !m->compress_info)
return 0;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops || !ops->msg_save_hcache)
return 0;
return ops->msg_save_hcache(m, e);
}
/**
* comp_tags_edit - Implements MxOps::tags_edit()
*/
static int comp_tags_edit(struct Mailbox *m, const char *tags, char *buf, size_t buflen)
{
if (!m || !m->compress_info)
return 0;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops || !ops->tags_edit)
return 0;
return ops->tags_edit(m, tags, buf, buflen);
}
/**
* comp_tags_commit - Implements MxOps::tags_commit()
*/
static int comp_tags_commit(struct Mailbox *m, struct Email *e, char *buf)
{
if (!m || !m->compress_info)
return 0;
struct CompressInfo *ci = m->compress_info;
const struct MxOps *ops = ci->child_ops;
if (!ops || !ops->tags_commit)
return 0;
return ops->tags_commit(m, e, buf);
}
/**
* comp_path_probe - Is this a compressed mailbox? - Implements MxOps::path_probe()
*/
enum MailboxType comp_path_probe(const char *path, const struct stat *st)
{
if (!path)
return MUTT_UNKNOWN;
if (!st || !S_ISREG(st->st_mode))
return MUTT_UNKNOWN;
if (mutt_comp_can_read(path))
return MUTT_COMPRESSED;
return MUTT_UNKNOWN;
}
/**
* comp_path_canon - Canonicalise a mailbox path - Implements MxOps::path_canon()
*/
int comp_path_canon(char *buf, size_t buflen)
{
if (!buf)
return -1;
mutt_path_canon(buf, buflen, HomeDir);
return 0;
}
/**
* comp_path_pretty - Implements MxOps::path_pretty()
*/
int comp_path_pretty(char *buf, size_t buflen, const char *folder)
{
if (!buf)
return -1;
if (mutt_path_abbr_folder(buf, buflen, folder))
return 0;
if (mutt_path_pretty(buf, buflen, HomeDir))
return 0;
return -1;
}
/**
* comp_path_parent - Implements MxOps::path_parent()
*/
int comp_path_parent(char *buf, size_t buflen)
{
if (!buf)
return -1;
if (mutt_path_parent(buf, buflen))
return 0;
if (buf[0] == '~')
mutt_path_canon(buf, buflen, HomeDir);
if (mutt_path_parent(buf, buflen))
return 0;
return -1;
}
// clang-format off
/**
* MxCompOps - Compressed mailbox - Implements ::MxOps
*
* Compress only uses open, close and check.
* The message functions are delegated to mbox.
*/
struct MxOps MxCompOps = {
.magic = MUTT_COMPRESSED,
.name = "compressed",
.ac_find = comp_ac_find,
.ac_add = comp_ac_add,
.mbox_open = comp_mbox_open,
.mbox_open_append = comp_mbox_open_append,
.mbox_check = comp_mbox_check,
.mbox_sync = comp_mbox_sync,
.mbox_close = comp_mbox_close,
.msg_open = comp_msg_open,
.msg_open_new = comp_msg_open_new,
.msg_commit = comp_msg_commit,
.msg_close = comp_msg_close,
.msg_padding_size = comp_msg_padding_size,
.msg_save_hcache = comp_msg_save_hcache,
.tags_edit = comp_tags_edit,
.tags_commit = comp_tags_commit,
.path_probe = comp_path_probe,
.path_canon = comp_path_canon,
.path_pretty = comp_path_pretty,
.path_parent = comp_path_parent,
};
// clang-format on