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"
28 const char *close; /* close-hook command */
29 const char *open; /* open-hook command */
30 const char *append; /* append-hook command */
31 off_t size; /* size of real folder */
34 char echo_cmd[HUGE_STRING];
37 * ctx - context to lock
38 * excl - exclusive lock?
39 * retry - should retry if unable to lock?
41 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
45 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
47 else if (retry && !excl) {
55 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
60 mx_unlock_file (ctx->realpath, fileno (fp), 1);
65 static int is_new (const char *path)
67 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
70 static const char *find_compress_hook (int type, const char *path)
72 const char *c = mutt_find_hook (type, path);
74 return (!c || !*c) ? NULL : c;
77 int mutt_can_read_compressed (const char *path)
79 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
82 /* if the file is new, we really do not append, but create, and so use
83 * close-hook, and not append-hook
85 static const char *get_append_command (const char *path, const CONTEXT * ctx)
87 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
89 return (is_new (path)) ? ci->close : ci->append;
92 int mutt_can_append_compressed (const char *path)
97 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
99 magic = mx_get_magic (path);
101 if (magic != 0 && magic != M_COMPRESSED)
104 return (find_compress_hook (M_APPENDHOOK, path)
105 || (find_compress_hook (M_OPENHOOK, path)
106 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
109 /* open a compressed mailbox */
110 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
114 /* Now lets uncompress this thing */
115 ci = safe_malloc (sizeof (COMPRESS_INFO));
116 ctx->compressinfo = (void *) ci;
117 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
118 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
119 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
123 static void set_path (CONTEXT * ctx)
125 char tmppath[_POSIX_PATH_MAX];
127 /* Setup the right paths */
128 ctx->realpath = ctx->path;
130 /* Uncompress to /tmp */
131 mutt_mktemp (tmppath);
132 ctx->path = safe_malloc (mutt_strlen (tmppath) + 1);
133 strcpy (ctx->path, 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 safe_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 FREE (&ctx->compressinfo);
200 FREE (&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 FREE (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 dprint (2, (debugfile, "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 FREE (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)
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 FREE (&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 dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
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 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 dprint (2, (debugfile, "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 dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n",
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 dprint (2, (debugfile, "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 FREE (&ctx->compressinfo);
480 #endif /* USE_COMPRESSED */