2 * Copyright notice from original mutt:
3 * Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
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.
10 #include <lib-lib/mem.h>
18 #include "mutt_curses.h"
23 #include "lib/debug.h"
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 */
37 char echo_cmd[HUGE_STRING];
40 * ctx - context to lock
41 * excl - exclusive lock?
42 * retry - should retry if unable to lock?
44 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
48 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
50 else if (retry && !excl) {
58 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
63 mx_unlock_file (ctx->realpath, fileno (fp), 1);
68 static int is_new (const char *path)
70 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
73 static const char *find_compress_hook (int type, const char *path)
75 const char *c = mutt_find_hook (type, path);
77 return (!c || !*c) ? NULL : c;
80 int mutt_can_read_compressed (const char *path)
82 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
85 /* if the file is new, we really do not append, but create, and so use
86 * close-hook, and not append-hook
88 static const char *get_append_command (const char *path, const CONTEXT * ctx)
90 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
92 return (is_new (path)) ? ci->close : ci->append;
95 int mutt_can_append_compressed (const char *path)
100 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
102 magic = mx_get_magic (path);
104 if (magic != 0 && magic != M_COMPRESSED)
107 return (find_compress_hook (M_APPENDHOOK, path)
108 || (find_compress_hook (M_OPENHOOK, path)
109 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
112 /* open a compressed mailbox */
113 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
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);
126 static void set_path (CONTEXT * ctx)
128 char tmppath[_POSIX_PATH_MAX];
130 /* Setup the right paths */
131 ctx->realpath = ctx->path;
133 /* Uncompress to /tmp */
134 mutt_mktemp (tmppath);
135 ctx->path = p_dupstr(tmppath, str_len(tmppath));
138 static int get_size (const char *path)
142 if (stat (path, &sb) != 0)
147 static void store_size (CONTEXT * ctx)
149 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
151 ci->size = get_size (ctx->realpath);
154 static const char *compresshook_format_str (char *dest, size_t destlen,
155 char op, const char *src,
157 const char *ifstring,
158 const char *elsestring,
162 char tmp[SHORT_STRING];
164 CONTEXT *ctx = (CONTEXT *) data;
168 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
169 snprintf (dest, destlen, tmp, ctx->realpath);
172 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
173 snprintf (dest, destlen, tmp, ctx->path);
179 /* check that the command has both %f and %t
180 * 0 means OK, -1 means error
182 int mutt_test_compress_command (const char *cmd)
184 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
187 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
189 char expanded[_POSIX_PATH_MAX];
191 mutt_FormatString (expanded, sizeof (expanded), cmd,
192 compresshook_format_str, (unsigned long) ctx, 0);
193 return str_dup (expanded);
196 int mutt_check_mailbox_compressed (CONTEXT * ctx)
198 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
200 if (ci->size != get_size (ctx->realpath)) {
201 p_delete(&ctx->compressinfo);
202 p_delete(&ctx->realpath);
203 mutt_error _("Mailbox was corrupted!");
210 int mutt_open_read_compressed (CONTEXT * ctx)
216 COMPRESS_INFO *ci = set_compress_info (ctx);
220 p_delete(&ctx->compressinfo);
223 if (!ci->close || access (ctx->path, W_OK) != 0)
230 mutt_message (_("Decompressing %s..."), ctx->realpath);
232 cmd = get_compression_cmd (ci->open, ctx);
235 debug_print (2, ("DecompressCmd: '%s'\n", cmd));
237 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
238 mutt_perror (ctx->realpath);
242 mutt_block_signals ();
243 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
245 mutt_unblock_signals ();
246 mutt_error _("Unable to lock mailbox!");
254 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
255 mutt_system (echo_cmd);
256 rc = mutt_system (cmd);
257 mbox_unlock_compressed (ctx, fp);
258 mutt_unblock_signals ();
262 mutt_any_key_to_continue (NULL);
264 p_delete(&ctx->compressinfo);
265 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
272 if (mutt_check_mailbox_compressed (ctx))
275 ctx->magic = mx_get_magic (ctx->path);
280 void restore_path (CONTEXT * ctx)
282 p_delete(&ctx->path);
283 ctx->path = ctx->realpath;
286 /* remove the temporary mailbox */
287 void remove_file (CONTEXT * ctx)
289 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
293 int mutt_open_append_compressed (CONTEXT * ctx)
296 COMPRESS_INFO *ci = set_compress_info (ctx);
298 if (!get_append_command (ctx->path, ctx)) {
299 if (ci->open && ci->close)
300 return (mutt_open_read_compressed (ctx));
303 p_delete(&ctx->compressinfo);
309 ctx->magic = DefaultMagic;
311 if (!is_new (ctx->realpath))
312 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
313 if ((fh = safe_fopen (ctx->path, "w")))
315 /* No error checking - the parent function will catch it */
320 /* close a compressed mailbox */
321 void mutt_fast_close_compressed (CONTEXT * ctx)
323 debug_print (2, ("called on '%s'\n", ctx->path));
325 if (ctx->compressinfo) {
329 /* if the folder was removed, remove the gzipped folder too */
330 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
331 remove (ctx->realpath);
336 p_delete(&ctx->compressinfo);
340 /* return 0 on success, -1 on failure */
341 int mutt_sync_compressed (CONTEXT * ctx)
346 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
349 mutt_message (_("Compressing %s..."), ctx->realpath);
351 cmd = get_compression_cmd (ci->close, ctx);
355 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
356 mutt_perror (ctx->realpath);
360 mutt_block_signals ();
361 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
363 mutt_unblock_signals ();
364 mutt_error _("Unable to lock mailbox!");
372 debug_print (2, ("CompressCommand: '%s'\n", cmd));
376 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
377 mutt_system (echo_cmd);
378 if (mutt_system (cmd)) {
379 mutt_any_key_to_continue (NULL);
381 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
386 mbox_unlock_compressed (ctx, fp);
387 mutt_unblock_signals ();
397 int mutt_slow_close_compressed (CONTEXT * ctx)
402 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
404 debug_print (2, ("called on '%s'\n", ctx->path));
406 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
407 || (append = ci->close)))) { /* if we can not or should not append,
408 * we only have to remove the compressed info, because sync was already
411 mutt_fast_close_compressed (ctx);
420 if (append == ci->close)
421 mutt_message (_("Compressing %s..."), ctx->realpath);
423 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
426 cmd = get_compression_cmd (append, ctx);
430 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
431 mutt_perror (ctx->realpath);
435 mutt_block_signals ();
436 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
438 mutt_unblock_signals ();
439 mutt_error _("Unable to lock mailbox!");
445 debug_print (2, ("CompressCmd: '%s'\n", cmd));
450 if (append == ci->close)
451 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
453 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
455 mutt_system (echo_cmd);
457 if (mutt_system (cmd)) {
458 mutt_any_key_to_continue (NULL);
460 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
463 mbox_unlock_compressed (ctx, fp);
464 mutt_unblock_signals ();
469 mbox_unlock_compressed (ctx, fp);
470 mutt_unblock_signals ();
475 p_delete(&ctx->compressinfo);
480 mx_t* compress_reg_mx (void) {
481 mx_t* fmt = p_new(mx_t, 1);
482 fmt->type = M_COMPRESSED;
484 fmt->mx_is_magic = mbox_is_magic;
485 fmt->mx_check_empty = mbox_check_empty;
486 fmt->mx_access = access;
487 fmt->mx_open_mailbox = mutt_open_read_compressed;
491 #endif /* USE_COMPRESSED */