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.
16 #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 = mem_malloc (sizeof (COMPRESS_INFO));
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 = mem_malloc (str_len (tmppath) + 1);
134 strcpy (ctx->path, tmppath);
137 static int get_size (const char *path)
141 if (stat (path, &sb) != 0)
146 static void store_size (CONTEXT * ctx)
148 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
150 ci->size = get_size (ctx->realpath);
153 static const char *compresshook_format_str (char *dest, size_t destlen,
154 char op, const char *src,
156 const char *ifstring,
157 const char *elsestring,
161 char tmp[SHORT_STRING];
163 CONTEXT *ctx = (CONTEXT *) data;
167 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
168 snprintf (dest, destlen, tmp, ctx->realpath);
171 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
172 snprintf (dest, destlen, tmp, ctx->path);
178 /* check that the command has both %f and %t
179 * 0 means OK, -1 means error
181 int mutt_test_compress_command (const char *cmd)
183 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
186 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
188 char expanded[_POSIX_PATH_MAX];
190 mutt_FormatString (expanded, sizeof (expanded), cmd,
191 compresshook_format_str, (unsigned long) ctx, 0);
192 return str_dup (expanded);
195 int mutt_check_mailbox_compressed (CONTEXT * ctx)
197 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
199 if (ci->size != get_size (ctx->realpath)) {
200 mem_free (&ctx->compressinfo);
201 mem_free (&ctx->realpath);
202 mutt_error _("Mailbox was corrupted!");
209 int mutt_open_read_compressed (CONTEXT * ctx)
215 COMPRESS_INFO *ci = set_compress_info (ctx);
219 mem_free (ctx->compressinfo);
222 if (!ci->close || access (ctx->path, W_OK) != 0)
229 mutt_message (_("Decompressing %s..."), ctx->realpath);
231 cmd = get_compression_cmd (ci->open, ctx);
234 debug_print (2, ("DecompressCmd: '%s'\n", cmd));
236 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
237 mutt_perror (ctx->realpath);
241 mutt_block_signals ();
242 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
244 mutt_unblock_signals ();
245 mutt_error _("Unable to lock mailbox!");
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 ();
261 mutt_any_key_to_continue (NULL);
263 mem_free (ctx->compressinfo);
264 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
271 if (mutt_check_mailbox_compressed (ctx))
274 ctx->magic = mx_get_magic (ctx->path);
279 void restore_path (CONTEXT * ctx)
281 mem_free (&ctx->path);
282 ctx->path = ctx->realpath;
285 /* remove the temporary mailbox */
286 void remove_file (CONTEXT * ctx)
288 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
292 int mutt_open_append_compressed (CONTEXT * ctx)
295 COMPRESS_INFO *ci = set_compress_info (ctx);
297 if (!get_append_command (ctx->path, ctx)) {
298 if (ci->open && ci->close)
299 return (mutt_open_read_compressed (ctx));
302 mem_free (&ctx->compressinfo);
308 ctx->magic = DefaultMagic;
310 if (!is_new (ctx->realpath))
311 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
312 if ((fh = safe_fopen (ctx->path, "w")))
314 /* No error checking - the parent function will catch it */
319 /* close a compressed mailbox */
320 void mutt_fast_close_compressed (CONTEXT * ctx)
322 debug_print (2, ("called on '%s'\n", ctx->path));
324 if (ctx->compressinfo) {
328 /* if the folder was removed, remove the gzipped folder too */
329 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
330 remove (ctx->realpath);
335 mem_free (&ctx->compressinfo);
339 /* return 0 on success, -1 on failure */
340 int mutt_sync_compressed (CONTEXT * ctx)
345 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
348 mutt_message (_("Compressing %s..."), ctx->realpath);
350 cmd = get_compression_cmd (ci->close, ctx);
354 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
355 mutt_perror (ctx->realpath);
359 mutt_block_signals ();
360 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
362 mutt_unblock_signals ();
363 mutt_error _("Unable to lock mailbox!");
371 debug_print (2, ("CompressCommand: '%s'\n", cmd));
375 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
376 mutt_system (echo_cmd);
377 if (mutt_system (cmd)) {
378 mutt_any_key_to_continue (NULL);
380 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
385 mbox_unlock_compressed (ctx, fp);
386 mutt_unblock_signals ();
396 int mutt_slow_close_compressed (CONTEXT * ctx)
401 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
403 debug_print (2, ("called on '%s'\n", ctx->path));
405 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
406 || (append = ci->close)))) { /* if we can not or should not append,
407 * we only have to remove the compressed info, because sync was already
410 mutt_fast_close_compressed (ctx);
419 if (append == ci->close)
420 mutt_message (_("Compressing %s..."), ctx->realpath);
422 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
425 cmd = get_compression_cmd (append, ctx);
429 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
430 mutt_perror (ctx->realpath);
434 mutt_block_signals ();
435 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
437 mutt_unblock_signals ();
438 mutt_error _("Unable to lock mailbox!");
444 debug_print (2, ("CompressCmd: '%s'\n", cmd));
449 if (append == ci->close)
450 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
452 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
454 mutt_system (echo_cmd);
456 if (mutt_system (cmd)) {
457 mutt_any_key_to_continue (NULL);
459 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
462 mbox_unlock_compressed (ctx, fp);
463 mutt_unblock_signals ();
468 mbox_unlock_compressed (ctx, fp);
469 mutt_unblock_signals ();
474 mem_free (&ctx->compressinfo);
479 mx_t* compress_reg_mx (void) {
480 mx_t* fmt = mem_calloc (1, sizeof (mx_t));
481 fmt->type = M_COMPRESSED;
483 fmt->mx_is_magic = mbox_is_magic;
484 fmt->mx_check_empty = mbox_check_empty;
485 fmt->mx_access = access;
486 fmt->mx_open_mailbox = mutt_open_read_compressed;
490 #endif /* USE_COMPRESSED */