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