code simplifications.
[apps/madmutt.git] / lib-mx / 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/lib-lib.h>
11
12 #include <lib-sys/mutt_signal.h>
13 #include <lib-sys/unix.h>
14
15 #include <lib-ui/curses.h>
16
17 #include "mutt.h"
18
19 #include "mx.h"
20 #include "mbox.h"
21 #include "compress.h"
22
23 struct compress_info {
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 };
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 static 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 static 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) && errno == ENOENT);
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   return is_new(path) ? ctx->cinfo->close : ctx->cinfo->append;
84 }
85
86 int mutt_can_append_compressed (const char *path)
87 {
88   int magic;
89
90   if (is_new (path))
91     return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
92
93   magic = mx_get_magic (path);
94
95   if (magic != 0 && magic != M_COMPRESSED)
96     return 0;
97
98   return (find_compress_hook (M_APPENDHOOK, path)
99           || (find_compress_hook (M_OPENHOOK, path)
100               && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
101 }
102
103 /* open a compressed mailbox */
104 static compress_info *set_compress_info (CONTEXT * ctx)
105 {
106   compress_info *ci = p_new(compress_info, 1);
107
108   /* Now lets uncompress this thing */
109   ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
110   ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
111   ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
112
113   return (ctx->cinfo = ci);
114 }
115
116 static int get_size (const char *path)
117 {
118   struct stat sb;
119
120   if (stat (path, &sb) != 0)
121     return 0;
122   return sb.st_size;
123 }
124
125 static const char *
126 compresshook_format_str(char *dest, ssize_t destlen,
127                         char op, const char *src, const char *fmt,
128                         const char *ifstr __attribute__ ((unused)),
129                         const char *elstr __attribute__ ((unused)),
130                         anytype data,
131                         format_flag flags __attribute__ ((unused)))
132 {
133   char tmp[STRING];
134
135   CONTEXT *ctx = data.ptr;
136
137   switch (op) {
138   case 'f':
139     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
140     snprintf (dest, destlen, tmp, ctx->realpath);
141     break;
142   case 't':
143     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
144     snprintf (dest, destlen, tmp, ctx->path);
145     break;
146   }
147   return src;
148 }
149
150 /* check that the command has both %f and %t
151  * 0 means OK, -1 means error
152  */
153 int mutt_test_compress_command (const char *cmd)
154 {
155   return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
156 }
157
158 static char *get_compression_cmd(const char *cmd, const CONTEXT *ctx)
159 {
160     char buf[_POSIX_PATH_MAX];
161     m_strformat(buf, sizeof(buf), 0, cmd, compresshook_format_str, (void*)ctx, 0);
162     return m_strdup(buf);
163 }
164
165 int mutt_check_mailbox_compressed (CONTEXT * ctx)
166 {
167   if (ctx->cinfo->size != get_size (ctx->realpath)) {
168     p_delete(&ctx->cinfo);
169     p_delete(&ctx->realpath);
170     mutt_error _("Mailbox was corrupted!");
171
172     return -1;
173   }
174   return 0;
175 }
176
177 int mutt_open_read_compressed(CONTEXT * ctx)
178 {
179   char *cmd;
180   FILE *fp;
181   int rc;
182   char tmppath[_POSIX_PATH_MAX];
183   int tmpfd;
184
185   compress_info *ci = set_compress_info (ctx);
186
187   if (!ci->open) {
188     ctx->magic = 0;
189     p_delete(&ctx->cinfo);
190     return -1;
191   }
192   if (!ci->close || access (ctx->path, W_OK) != 0)
193     ctx->readonly = 1;
194
195   /* Setup the right paths */
196   ctx->realpath = ctx->path;
197
198   /* Uncompress to /tmp */
199   tmpfd = m_tempfd(tmppath, sizeof(tmppath), NONULL(MCore.tmpdir), NULL);
200   /* If we cannot open tempfile, that means the file already exists (!?)
201    * or we are following a symlink, which is bad and insecure.
202    */
203   if(tmpfd < 0) {
204       return -1;
205   }
206   close(tmpfd);
207
208   ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
209   ctx->cinfo->size = get_size(ctx->realpath);
210
211   if (!ctx->quiet)
212     mutt_message (_("Decompressing %s..."), ctx->realpath);
213
214   cmd = get_compression_cmd (ci->open, ctx);
215   if (cmd == NULL)
216     return -1;
217
218   if ((fp = fopen (ctx->realpath, "r")) == NULL) {
219     mutt_perror (ctx->realpath);
220     p_delete(&cmd);
221     return -1;
222   }
223   mutt_block_signals ();
224   if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
225     m_fclose(&fp);
226     mutt_unblock_signals ();
227     mutt_error _("Unable to lock mailbox!");
228
229     p_delete(&cmd);
230     return -1;
231   }
232
233   endwin ();
234   fflush (stdout);
235   sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
236   mutt_system (echo_cmd);
237   rc = mutt_system (cmd);
238   mbox_unlock_compressed (ctx, fp);
239   mutt_unblock_signals ();
240   m_fclose(&fp);
241
242   if (rc) {
243     mutt_any_key_to_continue (NULL);
244     ctx->magic = 0;
245     p_delete(&ctx->cinfo);
246     mutt_error(_("Error executing: %s : unable to open the mailbox!\n"), cmd);
247   }
248   p_delete(&cmd);
249   if (rc)
250     return -1;
251
252   if (mutt_check_mailbox_compressed (ctx))
253     return -1;
254
255   ctx->magic = mx_get_magic (ctx->path);
256
257   return 0;
258 }
259
260 static void restore_path (CONTEXT * ctx)
261 {
262   p_delete(&ctx->path);
263   ctx->path = ctx->realpath;
264 }
265
266 /* remove the temporary mailbox */
267 static void remove_file (CONTEXT * ctx)
268 {
269   if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
270     remove (ctx->path);
271 }
272
273 int mutt_open_append_compressed (CONTEXT * ctx)
274 {
275   FILE *fh;
276   compress_info *ci = set_compress_info (ctx);
277   char tmppath[_POSIX_PATH_MAX];
278
279   if (!get_append_command (ctx->path, ctx)) {
280     if (ci->open && ci->close)
281       return mutt_open_read_compressed(ctx);
282
283     ctx->magic = 0;
284     p_delete(&ctx->cinfo);
285     return -1;
286   }
287
288   /* Setup the right paths */
289   ctx->realpath = ctx->path;
290
291   /* Uncompress to /tmp */
292   fh = m_tempfile(tmppath, sizeof(tmppath), NONULL(MCore.tmpdir), NULL);
293   m_fclose(&fh);
294
295   ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
296
297   ctx->magic = DefaultMagic;
298
299   if (is_new (ctx->realpath) || 
300       (ctx->magic != M_MBOX &&
301        ctx->magic != M_MMDF))
302       unlink(tmppath);
303
304   /* No error checking - the parent function will catch it */
305
306   return 0;
307 }
308
309 /* close a compressed mailbox */
310 void mutt_fast_close_compressed (CONTEXT * ctx)
311 {
312   if (ctx->cinfo) {
313     m_fclose(&ctx->fp);
314
315     /* if the folder was removed, remove the gzipped folder too */
316     if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
317       remove (ctx->realpath);
318     else
319       remove_file (ctx);
320
321     restore_path (ctx);
322     p_delete(&ctx->cinfo);
323   }
324 }
325
326 /* return 0 on success, -1 on failure */
327 int mutt_sync_compressed (CONTEXT * ctx)
328 {
329   char *cmd;
330   int rc = 0;
331   FILE *fp;
332
333   if (!ctx->quiet)
334     mutt_message (_("Compressing %s..."), ctx->realpath);
335
336   cmd = get_compression_cmd (ctx->cinfo->close, ctx);
337   if (cmd == NULL)
338     return -1;
339
340   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
341     mutt_perror (ctx->realpath);
342     p_delete(&cmd);
343     return -1;
344   }
345   mutt_block_signals ();
346   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
347     m_fclose(&fp);
348     mutt_unblock_signals ();
349     mutt_error _("Unable to lock mailbox!");
350
351     ctx->cinfo->size = get_size(ctx->realpath);
352
353     p_delete(&cmd);
354     return -1;
355   }
356
357   endwin ();
358   fflush (stdout);
359   sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
360   mutt_system (echo_cmd);
361   if (mutt_system (cmd)) {
362     mutt_any_key_to_continue (NULL);
363     mutt_error (_
364                 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
365                 ctx->path);
366     rc = -1;
367   }
368
369   mbox_unlock_compressed (ctx, fp);
370   mutt_unblock_signals ();
371   m_fclose(&fp);
372
373   p_delete(&cmd);
374
375   ctx->cinfo->size = get_size(ctx->realpath);
376
377   return rc;
378 }
379
380 int mutt_slow_close_compressed (CONTEXT * ctx)
381 {
382   FILE *fp;
383   const char *append;
384   char *cmd;
385   compress_info *ci = ctx->cinfo;
386
387   if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
388                         || (append = ci->close)))) {
389       /* if we can not or should not append, we only have to remove the
390          compressed info, because sync was already called */
391     mutt_fast_close_compressed (ctx);
392     return 0;
393   }
394
395   m_fclose(&ctx->fp);
396
397   if (!ctx->quiet) {
398     if (append == ci->close)
399       mutt_message (_("Compressing %s..."), ctx->realpath);
400     else
401       mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
402   }
403
404   cmd = get_compression_cmd (append, ctx);
405   if (cmd == NULL)
406     return -1;
407
408   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
409     mutt_perror (ctx->realpath);
410     p_delete(&cmd);
411     return -1;
412   }
413   mutt_block_signals ();
414   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
415     m_fclose(&fp);
416     mutt_unblock_signals ();
417     mutt_error _("Unable to lock mailbox!");
418
419     p_delete(&cmd);
420     return -1;
421   }
422
423   endwin ();
424   fflush (stdout);
425
426   if (append == ci->close)
427     sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
428   else
429     sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
430              ctx->realpath);
431   mutt_system (echo_cmd);
432
433   if (mutt_system (cmd)) {
434     mutt_any_key_to_continue (NULL);
435     mutt_error (_(" %s: Error compressing mailbox!  Uncompressed one kept!\n"),
436                 ctx->path);
437     p_delete(&cmd);
438     mbox_unlock_compressed (ctx, fp);
439     mutt_unblock_signals ();
440     m_fclose(&fp);
441     return -1;
442   }
443
444   mbox_unlock_compressed (ctx, fp);
445   mutt_unblock_signals ();
446   m_fclose(&fp);
447   remove_file (ctx);
448   restore_path (ctx);
449   p_delete(&cmd);
450   p_delete(&ctx->cinfo);
451
452   return 0;
453 }
454
455 mx_t const compress_mx = {
456     M_COMPRESSED,
457     1,
458     mbox_is_magic,
459     mbox_check_empty,
460     access,
461     mutt_open_read_compressed,
462     NULL,
463     NULL,
464     NULL,
465     NULL,
466     NULL,
467     NULL,
468 };