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