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