fix segfault
[apps/madmutt.git] / compress.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <lib-lib/mem.h>
11
12 #include "mutt.h"
13
14 #ifdef USE_COMPRESSED
15
16 #include "mx.h"
17 #include "mbox.h"
18 #include "mutt_curses.h"
19
20 #include "lib/mem.h"
21 #include "lib/intl.h"
22 #include "lib/str.h"
23 #include "lib/debug.h"
24
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29
30 typedef struct {
31   const char *close;            /* close-hook  command */
32   const char *open;             /* open-hook   command */
33   const char *append;           /* append-hook command */
34   off_t size;                   /* size of real folder */
35 } COMPRESS_INFO;
36
37 char echo_cmd[HUGE_STRING];
38
39 /* parameters:
40  * ctx - context to lock
41  * excl - exclusive lock?
42  * retry - should retry if unable to lock?
43  */
44 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
45 {
46   int r;
47
48   if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
49     ctx->locked = 1;
50   else if (retry && !excl) {
51     ctx->readonly = 1;
52     return 0;
53   }
54
55   return (r);
56 }
57
58 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
59 {
60   if (ctx->locked) {
61     fflush (fp);
62
63     mx_unlock_file (ctx->realpath, fileno (fp), 1);
64     ctx->locked = 0;
65   }
66 }
67
68 static int is_new (const char *path)
69 {
70   return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
71 }
72
73 static const char *find_compress_hook (int type, const char *path)
74 {
75   const char *c = mutt_find_hook (type, path);
76
77   return (!c || !*c) ? NULL : c;
78 }
79
80 int mutt_can_read_compressed (const char *path)
81 {
82   return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
83 }
84
85 /* if the file is new, we really do not append, but create, and so use
86  * close-hook, and not append-hook 
87  */
88 static const char *get_append_command (const char *path, const CONTEXT * ctx)
89 {
90   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
91
92   return (is_new (path)) ? ci->close : ci->append;
93 }
94
95 int mutt_can_append_compressed (const char *path)
96 {
97   int magic;
98
99   if (is_new (path))
100     return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
101
102   magic = mx_get_magic (path);
103
104   if (magic != 0 && magic != M_COMPRESSED)
105     return 0;
106
107   return (find_compress_hook (M_APPENDHOOK, path)
108           || (find_compress_hook (M_OPENHOOK, path)
109               && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
110 }
111
112 /* open a compressed mailbox */
113 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
114 {
115   COMPRESS_INFO *ci;
116
117   /* Now lets uncompress this thing */
118   ci = p_new(COMPRESS_INFO, 1);
119   ctx->compressinfo = (void *) ci;
120   ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
121   ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
122   ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
123   return ci;
124 }
125
126 static void set_path (CONTEXT * ctx)
127 {
128   char tmppath[_POSIX_PATH_MAX];
129
130   /* Setup the right paths */
131   ctx->realpath = ctx->path;
132
133   /* Uncompress to /tmp */
134   mutt_mktemp (tmppath);
135   ctx->path = p_dupstr(tmppath, str_len(tmppath));
136 }
137
138 static int get_size (const char *path)
139 {
140   struct stat sb;
141
142   if (stat (path, &sb) != 0)
143     return 0;
144   return (sb.st_size);
145 }
146
147 static void store_size (CONTEXT * ctx)
148 {
149   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
150
151   ci->size = get_size (ctx->realpath);
152 }
153
154 static const char *compresshook_format_str (char *dest, size_t destlen,
155                                             char op, const char *src,
156                                             const char *fmt,
157                                             const char *ifstring,
158                                             const char *elsestring,
159                                             unsigned long data,
160                                             format_flag flags)
161 {
162   char tmp[SHORT_STRING];
163
164   CONTEXT *ctx = (CONTEXT *) data;
165
166   switch (op) {
167   case 'f':
168     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
169     snprintf (dest, destlen, tmp, ctx->realpath);
170     break;
171   case 't':
172     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
173     snprintf (dest, destlen, tmp, ctx->path);
174     break;
175   }
176   return (src);
177 }
178
179 /* check that the command has both %f and %t
180  * 0 means OK, -1 means error
181  */
182 int mutt_test_compress_command (const char *cmd)
183 {
184   return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
185 }
186
187 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
188 {
189   char expanded[_POSIX_PATH_MAX];
190
191   mutt_FormatString (expanded, sizeof (expanded), cmd,
192                      compresshook_format_str, (unsigned long) ctx, 0);
193   return str_dup (expanded);
194 }
195
196 int mutt_check_mailbox_compressed (CONTEXT * ctx)
197 {
198   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
199
200   if (ci->size != get_size (ctx->realpath)) {
201     p_delete(&ctx->compressinfo);
202     p_delete(&ctx->realpath);
203     mutt_error _("Mailbox was corrupted!");
204
205     return (-1);
206   }
207   return (0);
208 }
209
210 int mutt_open_read_compressed (CONTEXT * ctx)
211 {
212   char *cmd;
213   FILE *fp;
214   int rc;
215
216   COMPRESS_INFO *ci = set_compress_info (ctx);
217
218   if (!ci->open) {
219     ctx->magic = 0;
220     p_delete(&ctx->compressinfo);
221     return (-1);
222   }
223   if (!ci->close || access (ctx->path, W_OK) != 0)
224     ctx->readonly = 1;
225
226   set_path (ctx);
227   store_size (ctx);
228
229   if (!ctx->quiet)
230     mutt_message (_("Decompressing %s..."), ctx->realpath);
231
232   cmd = get_compression_cmd (ci->open, ctx);
233   if (cmd == NULL)
234     return (-1);
235   debug_print (2, ("DecompressCmd: '%s'\n", cmd));
236
237   if ((fp = fopen (ctx->realpath, "r")) == NULL) {
238     mutt_perror (ctx->realpath);
239     p_delete(&cmd);
240     return (-1);
241   }
242   mutt_block_signals ();
243   if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
244     fclose (fp);
245     mutt_unblock_signals ();
246     mutt_error _("Unable to lock mailbox!");
247
248     p_delete(&cmd);
249     return (-1);
250   }
251
252   endwin ();
253   fflush (stdout);
254   sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
255   mutt_system (echo_cmd);
256   rc = mutt_system (cmd);
257   mbox_unlock_compressed (ctx, fp);
258   mutt_unblock_signals ();
259   fclose (fp);
260
261   if (rc) {
262     mutt_any_key_to_continue (NULL);
263     ctx->magic = 0;
264     p_delete(&ctx->compressinfo);
265     mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
266                 cmd);
267   }
268   p_delete(&cmd);
269   if (rc)
270     return (-1);
271
272   if (mutt_check_mailbox_compressed (ctx))
273     return (-1);
274
275   ctx->magic = mx_get_magic (ctx->path);
276
277   return (0);
278 }
279
280 void restore_path (CONTEXT * ctx)
281 {
282   p_delete(&ctx->path);
283   ctx->path = ctx->realpath;
284 }
285
286 /* remove the temporary mailbox */
287 void remove_file (CONTEXT * ctx)
288 {
289   if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
290     remove (ctx->path);
291 }
292
293 int mutt_open_append_compressed (CONTEXT * ctx)
294 {
295   FILE *fh;
296   COMPRESS_INFO *ci = set_compress_info (ctx);
297
298   if (!get_append_command (ctx->path, ctx)) {
299     if (ci->open && ci->close)
300       return (mutt_open_read_compressed (ctx));
301
302     ctx->magic = 0;
303     p_delete(&ctx->compressinfo);
304     return (-1);
305   }
306
307   set_path (ctx);
308
309   ctx->magic = DefaultMagic;
310
311   if (!is_new (ctx->realpath))
312     if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
313       if ((fh = safe_fopen (ctx->path, "w")))
314         fclose (fh);
315   /* No error checking - the parent function will catch it */
316
317   return (0);
318 }
319
320 /* close a compressed mailbox */
321 void mutt_fast_close_compressed (CONTEXT * ctx)
322 {
323   debug_print (2, ("called on '%s'\n", ctx->path));
324
325   if (ctx->compressinfo) {
326     if (ctx->fp)
327       fclose (ctx->fp);
328     ctx->fp = NULL;
329     /* if the folder was removed, remove the gzipped folder too */
330     if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
331       remove (ctx->realpath);
332     else
333       remove_file (ctx);
334
335     restore_path (ctx);
336     p_delete(&ctx->compressinfo);
337   }
338 }
339
340 /* return 0 on success, -1 on failure */
341 int mutt_sync_compressed (CONTEXT * ctx)
342 {
343   char *cmd;
344   int rc = 0;
345   FILE *fp;
346   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
347
348   if (!ctx->quiet)
349     mutt_message (_("Compressing %s..."), ctx->realpath);
350
351   cmd = get_compression_cmd (ci->close, ctx);
352   if (cmd == NULL)
353     return (-1);
354
355   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
356     mutt_perror (ctx->realpath);
357     p_delete(&cmd);
358     return (-1);
359   }
360   mutt_block_signals ();
361   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
362     fclose (fp);
363     mutt_unblock_signals ();
364     mutt_error _("Unable to lock mailbox!");
365
366     store_size (ctx);
367
368     p_delete(&cmd);
369     return (-1);
370   }
371
372   debug_print (2, ("CompressCommand: '%s'\n", cmd));
373
374   endwin ();
375   fflush (stdout);
376   sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
377   mutt_system (echo_cmd);
378   if (mutt_system (cmd)) {
379     mutt_any_key_to_continue (NULL);
380     mutt_error (_
381                 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
382                 ctx->path);
383     rc = -1;
384   }
385
386   mbox_unlock_compressed (ctx, fp);
387   mutt_unblock_signals ();
388   fclose (fp);
389
390   p_delete(&cmd);
391
392   store_size (ctx);
393
394   return (rc);
395 }
396
397 int mutt_slow_close_compressed (CONTEXT * ctx)
398 {
399   FILE *fp;
400   const char *append;
401   char *cmd;
402   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
403
404   debug_print (2, ("called on '%s'\n", ctx->path));
405
406   if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
407                         || (append = ci->close)))) {    /* if we can not or should not append,
408                                                          * we only have to remove the compressed info, because sync was already
409                                                          * called 
410                                                          */
411     mutt_fast_close_compressed (ctx);
412     return (0);
413   }
414
415   if (ctx->fp)
416     fclose (ctx->fp);
417   ctx->fp = NULL;
418
419   if (!ctx->quiet) {
420     if (append == ci->close)
421       mutt_message (_("Compressing %s..."), ctx->realpath);
422     else
423       mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
424   }
425
426   cmd = get_compression_cmd (append, ctx);
427   if (cmd == NULL)
428     return (-1);
429
430   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
431     mutt_perror (ctx->realpath);
432     p_delete(&cmd);
433     return (-1);
434   }
435   mutt_block_signals ();
436   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
437     fclose (fp);
438     mutt_unblock_signals ();
439     mutt_error _("Unable to lock mailbox!");
440
441     p_delete(&cmd);
442     return (-1);
443   }
444
445   debug_print (2, ("CompressCmd: '%s'\n", cmd));
446
447   endwin ();
448   fflush (stdout);
449
450   if (append == ci->close)
451     sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
452   else
453     sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
454              ctx->realpath);
455   mutt_system (echo_cmd);
456
457   if (mutt_system (cmd)) {
458     mutt_any_key_to_continue (NULL);
459     mutt_error (_
460                 (" %s: Error compressing mailbox!  Uncompressed one kept!\n"),
461                 ctx->path);
462     p_delete(&cmd);
463     mbox_unlock_compressed (ctx, fp);
464     mutt_unblock_signals ();
465     fclose (fp);
466     return (-1);
467   }
468
469   mbox_unlock_compressed (ctx, fp);
470   mutt_unblock_signals ();
471   fclose (fp);
472   remove_file (ctx);
473   restore_path (ctx);
474   p_delete(&cmd);
475   p_delete(&ctx->compressinfo);
476
477   return (0);
478 }
479
480 mx_t* compress_reg_mx (void) {
481   mx_t* fmt = p_new(mx_t, 1);
482   fmt->type = M_COMPRESSED;
483   fmt->local = 1;
484   fmt->mx_is_magic = mbox_is_magic;
485   fmt->mx_check_empty = mbox_check_empty;
486   fmt->mx_access = access;
487   fmt->mx_open_mailbox = mutt_open_read_compressed;
488   return (fmt);
489 }
490
491 #endif /* USE_COMPRESSED */