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>
11 #include <lib-lib/str.h>
12 #include <lib-lib/macros.h>
13 #include <lib-lib/file.h>
19 #include "mutt_curses.h"
21 #include "lib/debug.h"
29 const char *close; /* close-hook command */
30 const char *open; /* open-hook command */
31 const char *append; /* append-hook command */
32 off_t size; /* size of real folder */
35 char echo_cmd[HUGE_STRING];
38 * ctx - context to lock
39 * excl - exclusive lock?
40 * retry - should retry if unable to lock?
42 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
46 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
48 else if (retry && !excl) {
56 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
61 mx_unlock_file (ctx->realpath, fileno (fp), 1);
66 static int is_new (const char *path)
68 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
71 static const char *find_compress_hook (int type, const char *path)
73 const char *c = mutt_find_hook (type, path);
75 return (!c || !*c) ? NULL : c;
78 int mutt_can_read_compressed (const char *path)
80 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
83 /* if the file is new, we really do not append, but create, and so use
84 * close-hook, and not append-hook
86 static const char *get_append_command (const char *path, const CONTEXT * ctx)
88 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
90 return (is_new (path)) ? ci->close : ci->append;
93 int mutt_can_append_compressed (const char *path)
98 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
100 magic = mx_get_magic (path);
102 if (magic != 0 && magic != M_COMPRESSED)
105 return (find_compress_hook (M_APPENDHOOK, path)
106 || (find_compress_hook (M_OPENHOOK, path)
107 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
110 /* open a compressed mailbox */
111 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
115 /* Now lets uncompress this thing */
116 ci = p_new(COMPRESS_INFO, 1);
117 ctx->compressinfo = (void *) ci;
118 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
119 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
120 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
124 static void set_path (CONTEXT * ctx)
126 char tmppath[_POSIX_PATH_MAX];
128 /* Setup the right paths */
129 ctx->realpath = ctx->path;
131 /* Uncompress to /tmp */
132 mutt_mktemp (tmppath);
133 ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
136 static int get_size (const char *path)
140 if (stat (path, &sb) != 0)
145 static void store_size (CONTEXT * ctx)
147 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
149 ci->size = get_size (ctx->realpath);
152 static const char *compresshook_format_str (char *dest, size_t destlen,
153 char op, const char *src,
155 const char *ifstring,
156 const char *elsestring,
160 char tmp[SHORT_STRING];
162 CONTEXT *ctx = (CONTEXT *) data;
166 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
167 snprintf (dest, destlen, tmp, ctx->realpath);
170 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
171 snprintf (dest, destlen, tmp, ctx->path);
177 /* check that the command has both %f and %t
178 * 0 means OK, -1 means error
180 int mutt_test_compress_command (const char *cmd)
182 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
185 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
187 char expanded[_POSIX_PATH_MAX];
189 mutt_FormatString (expanded, sizeof (expanded), cmd,
190 compresshook_format_str, (unsigned long) ctx, 0);
191 return m_strdup(expanded);
194 int mutt_check_mailbox_compressed (CONTEXT * ctx)
196 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
198 if (ci->size != get_size (ctx->realpath)) {
199 p_delete(&ctx->compressinfo);
200 p_delete(&ctx->realpath);
201 mutt_error _("Mailbox was corrupted!");
208 int mutt_open_read_compressed (CONTEXT * ctx)
214 COMPRESS_INFO *ci = set_compress_info (ctx);
218 p_delete(&ctx->compressinfo);
221 if (!ci->close || access (ctx->path, W_OK) != 0)
228 mutt_message (_("Decompressing %s..."), ctx->realpath);
230 cmd = get_compression_cmd (ci->open, ctx);
233 debug_print (2, ("DecompressCmd: '%s'\n", cmd));
235 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
236 mutt_perror (ctx->realpath);
240 mutt_block_signals ();
241 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
243 mutt_unblock_signals ();
244 mutt_error _("Unable to lock mailbox!");
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 ();
260 mutt_any_key_to_continue (NULL);
262 p_delete(&ctx->compressinfo);
263 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
270 if (mutt_check_mailbox_compressed (ctx))
273 ctx->magic = mx_get_magic (ctx->path);
278 void restore_path (CONTEXT * ctx)
280 p_delete(&ctx->path);
281 ctx->path = ctx->realpath;
284 /* remove the temporary mailbox */
285 void remove_file (CONTEXT * ctx)
287 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
291 int mutt_open_append_compressed (CONTEXT * ctx)
294 COMPRESS_INFO *ci = set_compress_info (ctx);
296 if (!get_append_command (ctx->path, ctx)) {
297 if (ci->open && ci->close)
298 return (mutt_open_read_compressed (ctx));
301 p_delete(&ctx->compressinfo);
307 ctx->magic = DefaultMagic;
309 if (!is_new (ctx->realpath))
310 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
311 if ((fh = safe_fopen (ctx->path, "w")))
313 /* No error checking - the parent function will catch it */
318 /* close a compressed mailbox */
319 void mutt_fast_close_compressed (CONTEXT * ctx)
321 debug_print (2, ("called on '%s'\n", ctx->path));
323 if (ctx->compressinfo) {
327 /* if the folder was removed, remove the gzipped folder too */
328 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
329 remove (ctx->realpath);
334 p_delete(&ctx->compressinfo);
338 /* return 0 on success, -1 on failure */
339 int mutt_sync_compressed (CONTEXT * ctx)
344 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
347 mutt_message (_("Compressing %s..."), ctx->realpath);
349 cmd = get_compression_cmd (ci->close, ctx);
353 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
354 mutt_perror (ctx->realpath);
358 mutt_block_signals ();
359 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
361 mutt_unblock_signals ();
362 mutt_error _("Unable to lock mailbox!");
370 debug_print (2, ("CompressCommand: '%s'\n", cmd));
374 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
375 mutt_system (echo_cmd);
376 if (mutt_system (cmd)) {
377 mutt_any_key_to_continue (NULL);
379 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
384 mbox_unlock_compressed (ctx, fp);
385 mutt_unblock_signals ();
395 int mutt_slow_close_compressed (CONTEXT * ctx)
400 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
402 debug_print (2, ("called on '%s'\n", ctx->path));
404 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
405 || (append = ci->close)))) { /* if we can not or should not append,
406 * we only have to remove the compressed info, because sync was already
409 mutt_fast_close_compressed (ctx);
418 if (append == ci->close)
419 mutt_message (_("Compressing %s..."), ctx->realpath);
421 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
424 cmd = get_compression_cmd (append, ctx);
428 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
429 mutt_perror (ctx->realpath);
433 mutt_block_signals ();
434 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
436 mutt_unblock_signals ();
437 mutt_error _("Unable to lock mailbox!");
443 debug_print (2, ("CompressCmd: '%s'\n", cmd));
448 if (append == ci->close)
449 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
451 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
453 mutt_system (echo_cmd);
455 if (mutt_system (cmd)) {
456 mutt_any_key_to_continue (NULL);
458 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
461 mbox_unlock_compressed (ctx, fp);
462 mutt_unblock_signals ();
467 mbox_unlock_compressed (ctx, fp);
468 mutt_unblock_signals ();
473 p_delete(&ctx->compressinfo);
478 mx_t* compress_reg_mx (void) {
479 mx_t* fmt = p_new(mx_t, 1);
480 fmt->type = M_COMPRESSED;
482 fmt->mx_is_magic = mbox_is_magic;
483 fmt->mx_check_empty = mbox_check_empty;
484 fmt->mx_access = access;
485 fmt->mx_open_mailbox = mutt_open_read_compressed;