dead code
[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
20 #include <lib-sys/mutt_signal.h>
21 #include <lib-sys/unix.h>
22
23 #include <lib-ui/curses.h>
24
25 #include "mutt.h"
26
27 #include "mx.h"
28 #include "mbox.h"
29
30 typedef struct {
31   const char *close;            /* close-hook  command */
32   const char *open;             /* open-hook   command */
33   const char *append;           /* append-hook command */
34   off_t size;                   /* size of real folder */
35 } COMPRESS_INFO;
36
37 char echo_cmd[HUGE_STRING];
38
39 /* parameters:
40  * ctx - context to lock
41  * excl - exclusive lock?
42  * retry - should retry if unable to lock?
43  */
44 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
45 {
46   int r;
47
48   if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
49     ctx->locked = 1;
50   else if (retry && !excl) {
51     ctx->readonly = 1;
52     return 0;
53   }
54
55   return (r);
56 }
57
58 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
59 {
60   if (ctx->locked) {
61     fflush (fp);
62
63     mx_unlock_file (ctx->realpath, fileno (fp), 1);
64     ctx->locked = 0;
65   }
66 }
67
68 static int is_new (const char *path)
69 {
70   return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
71 }
72
73 static const char *find_compress_hook (int type, const char *path)
74 {
75   const char *c = mutt_find_hook (type, path);
76
77   return (!c || !*c) ? NULL : c;
78 }
79
80 int mutt_can_read_compressed (const char *path)
81 {
82   return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
83 }
84
85 /* if the file is new, we really do not append, but create, and so use
86  * close-hook, and not append-hook 
87  */
88 static const char *get_append_command (const char *path, const CONTEXT * ctx)
89 {
90   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
91
92   return (is_new (path)) ? ci->close : ci->append;
93 }
94
95 int mutt_can_append_compressed (const char *path)
96 {
97   int magic;
98
99   if (is_new (path))
100     return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
101
102   magic = mx_get_magic (path);
103
104   if (magic != 0 && magic != M_COMPRESSED)
105     return 0;
106
107   return (find_compress_hook (M_APPENDHOOK, path)
108           || (find_compress_hook (M_OPENHOOK, path)
109               && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
110 }
111
112 /* open a compressed mailbox */
113 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
114 {
115   COMPRESS_INFO *ci;
116
117   /* Now lets uncompress this thing */
118   ci = p_new(COMPRESS_INFO, 1);
119   ctx->compressinfo = (void *) ci;
120   ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
121   ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
122   ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
123   return ci;
124 }
125
126 static void set_path (CONTEXT * ctx)
127 {
128   char tmppath[_POSIX_PATH_MAX];
129
130   /* Setup the right paths */
131   ctx->realpath = ctx->path;
132
133   /* Uncompress to /tmp */
134   mutt_mktemp (tmppath);
135   ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
136 }
137
138 static int get_size (const char *path)
139 {
140   struct stat sb;
141
142   if (stat (path, &sb) != 0)
143     return 0;
144   return (sb.st_size);
145 }
146
147 static void store_size (CONTEXT * ctx)
148 {
149   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
150
151   ci->size = get_size (ctx->realpath);
152 }
153
154 static const char *compresshook_format_str (char *dest, ssize_t destlen,
155                                             char op, const char *src,
156                                             const char *fmt,
157                                             const char *ifstring,
158                                             const char *elsestring,
159                                             unsigned long data,
160                                             format_flag flags)
161 {
162   char tmp[SHORT_STRING];
163
164   CONTEXT *ctx = (CONTEXT *) data;
165
166   switch (op) {
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
191   mutt_FormatString (expanded, sizeof (expanded), cmd,
192                      compresshook_format_str, (unsigned long) ctx, 0);
193   return m_strdup(expanded);
194 }
195
196 int mutt_check_mailbox_compressed (CONTEXT * ctx)
197 {
198   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
199
200   if (ci->size != get_size (ctx->realpath)) {
201     p_delete(&ctx->compressinfo);
202     p_delete(&ctx->realpath);
203     mutt_error _("Mailbox was corrupted!");
204
205     return (-1);
206   }
207   return (0);
208 }
209
210 int mutt_open_read_compressed (CONTEXT * ctx)
211 {
212   char *cmd;
213   FILE *fp;
214   int rc;
215
216   COMPRESS_INFO *ci = set_compress_info (ctx);
217
218   if (!ci->open) {
219     ctx->magic = 0;
220     p_delete(&ctx->compressinfo);
221     return (-1);
222   }
223   if (!ci->close || access (ctx->path, W_OK) != 0)
224     ctx->readonly = 1;
225
226   set_path (ctx);
227   store_size (ctx);
228
229   if (!ctx->quiet)
230     mutt_message (_("Decompressing %s..."), ctx->realpath);
231
232   cmd = get_compression_cmd (ci->open, ctx);
233   if (cmd == NULL)
234     return (-1);
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   if (ctx->compressinfo) {
323     if (ctx->fp)
324       fclose (ctx->fp);
325     ctx->fp = NULL;
326     /* if the folder was removed, remove the gzipped folder too */
327     if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
328       remove (ctx->realpath);
329     else
330       remove_file (ctx);
331
332     restore_path (ctx);
333     p_delete(&ctx->compressinfo);
334   }
335 }
336
337 /* return 0 on success, -1 on failure */
338 int mutt_sync_compressed (CONTEXT * ctx)
339 {
340   char *cmd;
341   int rc = 0;
342   FILE *fp;
343   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
344
345   if (!ctx->quiet)
346     mutt_message (_("Compressing %s..."), ctx->realpath);
347
348   cmd = get_compression_cmd (ci->close, ctx);
349   if (cmd == NULL)
350     return (-1);
351
352   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
353     mutt_perror (ctx->realpath);
354     p_delete(&cmd);
355     return (-1);
356   }
357   mutt_block_signals ();
358   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
359     fclose (fp);
360     mutt_unblock_signals ();
361     mutt_error _("Unable to lock mailbox!");
362
363     store_size (ctx);
364
365     p_delete(&cmd);
366     return (-1);
367   }
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   p_delete(&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   if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
400                         || (append = ci->close)))) {    /* if we can not or should not append,
401                                                          * we only have to remove the compressed info, because sync was already
402                                                          * called 
403                                                          */
404     mutt_fast_close_compressed (ctx);
405     return (0);
406   }
407
408   if (ctx->fp)
409     fclose (ctx->fp);
410   ctx->fp = NULL;
411
412   if (!ctx->quiet) {
413     if (append == ci->close)
414       mutt_message (_("Compressing %s..."), ctx->realpath);
415     else
416       mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
417   }
418
419   cmd = get_compression_cmd (append, ctx);
420   if (cmd == NULL)
421     return (-1);
422
423   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
424     mutt_perror (ctx->realpath);
425     p_delete(&cmd);
426     return (-1);
427   }
428   mutt_block_signals ();
429   if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
430     fclose (fp);
431     mutt_unblock_signals ();
432     mutt_error _("Unable to lock mailbox!");
433
434     p_delete(&cmd);
435     return (-1);
436   }
437
438   endwin ();
439   fflush (stdout);
440
441   if (append == ci->close)
442     sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
443   else
444     sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
445              ctx->realpath);
446   mutt_system (echo_cmd);
447
448   if (mutt_system (cmd)) {
449     mutt_any_key_to_continue (NULL);
450     mutt_error (_
451                 (" %s: Error compressing mailbox!  Uncompressed one kept!\n"),
452                 ctx->path);
453     p_delete(&cmd);
454     mbox_unlock_compressed (ctx, fp);
455     mutt_unblock_signals ();
456     fclose (fp);
457     return (-1);
458   }
459
460   mbox_unlock_compressed (ctx, fp);
461   mutt_unblock_signals ();
462   fclose (fp);
463   remove_file (ctx);
464   restore_path (ctx);
465   p_delete(&cmd);
466   p_delete(&ctx->compressinfo);
467
468   return (0);
469 }
470
471 mx_t* compress_reg_mx (void) {
472   mx_t* fmt = p_new(mx_t, 1);
473   fmt->type = M_COMPRESSED;
474   fmt->local = 1;
475   fmt->mx_is_magic = mbox_is_magic;
476   fmt->mx_check_empty = mbox_check_empty;
477   fmt->mx_access = access;
478   fmt->mx_open_mailbox = mutt_open_read_compressed;
479   return (fmt);
480 }