Nico Golde:
[apps/madmutt.git] / compress.c
1 /*
2  * Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include "mutt.h"
20
21 #ifdef USE_COMPRESSED
22
23 #include "mx.h"
24 #include "mailbox.h"
25 #include "mutt_curses.h"
26
27 #include <errno.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 typedef struct
33 {
34   const char *close;    /* close-hook  command */
35   const char *open;     /* open-hook   command */
36   const char *append;   /* append-hook command */
37   off_t size;           /* size of real folder */
38 } COMPRESS_INFO;
39
40 char echo_cmd[HUGE_STRING];
41
42 /* parameters:
43  * ctx - context to lock
44  * excl - exclusive lock?
45  * retry - should retry if unable to lock?
46  */
47 int mbox_lock_compressed (CONTEXT *ctx, FILE *fp, int excl, int retry)
48 {
49   int r;
50
51   if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
52     ctx->locked = 1;
53   else if (retry && !excl)
54   {
55     ctx->readonly = 1;
56     return 0;
57   }
58
59   return (r);
60 }
61
62 void mbox_unlock_compressed (CONTEXT *ctx, FILE *fp)
63 {
64   if (ctx->locked)
65   {
66     fflush (fp);
67
68     mx_unlock_file (ctx->realpath, fileno (fp), 1);
69     ctx->locked = 0;
70   }
71 }
72
73 static int is_new (const char *path)
74 {
75   return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
76 }
77
78 static const char* find_compress_hook (int type, const char *path)
79 {
80   const char* c = mutt_find_hook (type, path);
81   return (!c || !*c) ? NULL : c;
82 }
83
84 int mutt_can_read_compressed (const char *path)
85 {
86   return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
87 }
88
89 /* if the file is new, we really do not append, but create, and so use
90  * close-hook, and not append-hook 
91  */
92 static const char* get_append_command (const char *path, const CONTEXT* ctx)
93 {
94   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
95   return (is_new (path)) ? ci->close : ci->append;
96 }
97     
98 int mutt_can_append_compressed (const char *path)
99 {
100   int magic;
101
102   if (is_new (path))
103     return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
104
105   magic = mx_get_magic (path);
106   
107   if (magic != 0 && magic != M_COMPRESSED)
108     return 0;
109
110   return (find_compress_hook (M_APPENDHOOK, path)
111           || (find_compress_hook (M_OPENHOOK, path) 
112               && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
113 }
114
115 /* open a compressed mailbox */
116 static COMPRESS_INFO *set_compress_info (CONTEXT *ctx)
117 {
118   COMPRESS_INFO *ci;
119
120   /* Now lets uncompress this thing */
121   ci = safe_malloc (sizeof (COMPRESS_INFO));
122   ctx->compressinfo = (void*) ci;
123   ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
124   ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
125   ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
126   return ci;
127 }
128   
129 static void set_path (CONTEXT* ctx)
130 {
131   char tmppath[_POSIX_PATH_MAX];
132
133   /* Setup the right paths */
134   ctx->realpath = ctx->path;
135
136   /* Uncompress to /tmp */
137   mutt_mktemp (tmppath);
138   ctx->path = safe_malloc (strlen (tmppath) + 1);
139   strcpy (ctx->path, tmppath);
140 }
141
142 static int get_size (const char* path) 
143 {
144   struct stat sb;
145   if (stat (path, &sb) != 0)
146     return 0;
147   return (sb.st_size);
148 }
149
150 static void store_size (CONTEXT* ctx) 
151 {
152   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
153   ci->size = get_size (ctx->realpath);
154 }
155
156 static const char *
157 compresshook_format_str (char *dest, size_t destlen, char op, const char *src,
158                          const char *fmt, const char *ifstring, 
159                          const char *elsestring, unsigned long data, 
160                          format_flag flags)
161 {
162   char tmp[SHORT_STRING];
163   
164   CONTEXT *ctx = (CONTEXT *) data;
165   switch (op)
166   {
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   mutt_FormatString (expanded, sizeof (expanded), cmd, compresshook_format_str,
191                      (unsigned long) ctx, 0);
192   return safe_strdup (expanded);
193 }
194
195 int mutt_check_mailbox_compressed (CONTEXT* ctx)
196 {
197   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
198   if (ci->size != get_size (ctx->realpath))
199   {
200     FREE (&ctx->compressinfo);
201     FREE (&ctx->realpath);
202     mutt_error _("Mailbox was corrupted!");
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   if (!ci->open) {
216     ctx->magic = 0;
217     FREE (ctx->compressinfo);
218     return (-1);
219   }
220   if (!ci->close || access (ctx->path, W_OK) != 0)
221     ctx->readonly = 1;
222
223   set_path (ctx);
224   store_size (ctx);
225
226   if (!ctx->quiet)
227     mutt_message (_("Decompressing %s..."), ctx->realpath);
228
229   cmd = get_compression_cmd (ci->open, ctx);
230   if (cmd == NULL) 
231     return (-1);
232   dprint (2, (debugfile, "DecompressCmd: '%s'\n", cmd));
233
234   if ((fp = fopen (ctx->realpath, "r")) == NULL)
235   {
236     mutt_perror (ctx->realpath);
237     FREE (&cmd);
238     return (-1);
239   }
240   mutt_block_signals ();
241   if (mbox_lock_compressed (ctx, fp, 0, 1) == -1)
242   {
243     fclose (fp);
244     mutt_unblock_signals ();
245     mutt_error _("Unable to lock mailbox!");
246     FREE (&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   {
261     mutt_any_key_to_continue (NULL);
262     ctx->magic = 0;
263     FREE (ctx->compressinfo);
264     mutt_error (_("Error executing: %s : unable to open the mailbox!\n"), cmd);
265   }
266   FREE (&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   FREE (&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   {
298     if (ci->open && ci->close)
299       return (mutt_open_read_compressed (ctx));
300
301     ctx->magic = 0;
302     FREE (&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   dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
323               ctx->path));
324
325   if (ctx->compressinfo)
326   {
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     FREE (&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   {
358     mutt_perror (ctx->realpath);
359     FREE (&cmd);
360     return (-1);
361   }
362   mutt_block_signals ();
363   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1)
364   {
365     fclose (fp);
366     mutt_unblock_signals ();
367     mutt_error _("Unable to lock mailbox!");
368
369   store_size (ctx);
370
371     FREE (&cmd);
372     return (-1);
373   }
374
375   dprint (2, (debugfile, "CompressCommand: '%s'\n", cmd));
376
377   endwin ();
378   fflush (stdout);
379   sprintf(echo_cmd,_("echo Compressing %s..."), ctx->realpath); 
380   mutt_system(echo_cmd);
381   if (mutt_system (cmd))
382   {
383     mutt_any_key_to_continue (NULL);
384     mutt_error (_("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"), ctx->path);
385     rc = -1;
386   }
387
388   mbox_unlock_compressed (ctx, fp);
389   mutt_unblock_signals ();
390   fclose (fp);
391
392   FREE (&cmd);
393   
394   store_size (ctx);
395
396   return (rc);
397 }
398
399 int mutt_slow_close_compressed (CONTEXT *ctx)
400 {
401   FILE *fp;
402   const char *append;
403   char *cmd;
404   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
405
406   dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n", 
407               ctx->path));
408
409   if (! (ctx->append 
410          && ((append = get_append_command (ctx->realpath, ctx))
411              || (append = ci->close))))
412   { /* if we can not or should not append,
413      * we only have to remove the compressed info, because sync was already
414      * called 
415      */
416     mutt_fast_close_compressed (ctx);
417     return (0);
418   }
419
420   if (ctx->fp)
421     fclose (ctx->fp);
422   ctx->fp = NULL;
423
424   if (!ctx->quiet)
425   {
426     if (append == ci->close)
427       mutt_message (_("Compressing %s..."), ctx->realpath);
428     else
429       mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
430   }
431
432   cmd = get_compression_cmd (append, ctx);
433   if (cmd == NULL) 
434     return (-1);
435
436   if ((fp = fopen (ctx->realpath, "a")) == NULL)
437   {
438     mutt_perror (ctx->realpath);
439     FREE (&cmd);
440     return (-1);
441   }
442   mutt_block_signals ();
443   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1)
444   {
445     fclose (fp);
446     mutt_unblock_signals ();
447     mutt_error _("Unable to lock mailbox!");
448     FREE (&cmd);
449     return (-1);
450   }
451
452   dprint (2, (debugfile, "CompressCmd: '%s'\n", cmd));
453
454   endwin ();
455   fflush (stdout);
456
457   if (append == ci->close)
458     sprintf(echo_cmd,_("echo Compressing %s..."), ctx->realpath); 
459   else
460     sprintf(echo_cmd,_("echo Compressed-appending to %s..."), ctx->realpath); 
461   mutt_system(echo_cmd);
462
463   if (mutt_system (cmd))
464   {
465     mutt_any_key_to_continue (NULL);
466     mutt_error (_(" %s: Error compressing mailbox!  Uncompressed one kept!\n"),
467                 ctx->path);
468     FREE (&cmd);
469     mbox_unlock_compressed (ctx, fp);
470     mutt_unblock_signals ();
471     fclose (fp);
472     return (-1);
473   }
474
475   mbox_unlock_compressed (ctx, fp);
476   mutt_unblock_signals ();
477   fclose (fp);
478   remove_file (ctx);
479   restore_path (ctx);
480   FREE (&cmd);
481   FREE (&ctx->compressinfo);
482
483   return (0);
484 }
485
486 #endif /* USE_COMPRESSED */