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