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.
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 #include <lib-lib/debug.h>
21 #include <lib-sys/mutt_signal.h>
22 #include <lib-sys/unix.h>
24 #include <lib-ui/curses.h>
32 const char *close; /* close-hook command */
33 const char *open; /* open-hook command */
34 const char *append; /* append-hook command */
35 off_t size; /* size of real folder */
38 char echo_cmd[HUGE_STRING];
41 * ctx - context to lock
42 * excl - exclusive lock?
43 * retry - should retry if unable to lock?
45 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
49 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
51 else if (retry && !excl) {
59 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
64 mx_unlock_file (ctx->realpath, fileno (fp), 1);
69 static int is_new (const char *path)
71 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
74 static const char *find_compress_hook (int type, const char *path)
76 const char *c = mutt_find_hook (type, path);
78 return (!c || !*c) ? NULL : c;
81 int mutt_can_read_compressed (const char *path)
83 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
86 /* if the file is new, we really do not append, but create, and so use
87 * close-hook, and not append-hook
89 static const char *get_append_command (const char *path, const CONTEXT * ctx)
91 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
93 return (is_new (path)) ? ci->close : ci->append;
96 int mutt_can_append_compressed (const char *path)
101 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
103 magic = mx_get_magic (path);
105 if (magic != 0 && magic != M_COMPRESSED)
108 return (find_compress_hook (M_APPENDHOOK, path)
109 || (find_compress_hook (M_OPENHOOK, path)
110 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
113 /* open a compressed mailbox */
114 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
118 /* Now lets uncompress this thing */
119 ci = p_new(COMPRESS_INFO, 1);
120 ctx->compressinfo = (void *) ci;
121 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
122 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
123 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
127 static void set_path (CONTEXT * ctx)
129 char tmppath[_POSIX_PATH_MAX];
131 /* Setup the right paths */
132 ctx->realpath = ctx->path;
134 /* Uncompress to /tmp */
135 mutt_mktemp (tmppath);
136 ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
139 static int get_size (const char *path)
143 if (stat (path, &sb) != 0)
148 static void store_size (CONTEXT * ctx)
150 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
152 ci->size = get_size (ctx->realpath);
155 static const char *compresshook_format_str (char *dest, size_t destlen,
156 char op, const char *src,
158 const char *ifstring,
159 const char *elsestring,
163 char tmp[SHORT_STRING];
165 CONTEXT *ctx = (CONTEXT *) data;
169 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
170 snprintf (dest, destlen, tmp, ctx->realpath);
173 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
174 snprintf (dest, destlen, tmp, ctx->path);
180 /* check that the command has both %f and %t
181 * 0 means OK, -1 means error
183 int mutt_test_compress_command (const char *cmd)
185 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
188 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
190 char expanded[_POSIX_PATH_MAX];
192 mutt_FormatString (expanded, sizeof (expanded), cmd,
193 compresshook_format_str, (unsigned long) ctx, 0);
194 return m_strdup(expanded);
197 int mutt_check_mailbox_compressed (CONTEXT * ctx)
199 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
201 if (ci->size != get_size (ctx->realpath)) {
202 p_delete(&ctx->compressinfo);
203 p_delete(&ctx->realpath);
204 mutt_error _("Mailbox was corrupted!");
211 int mutt_open_read_compressed (CONTEXT * ctx)
217 COMPRESS_INFO *ci = set_compress_info (ctx);
221 p_delete(&ctx->compressinfo);
224 if (!ci->close || access (ctx->path, W_OK) != 0)
231 mutt_message (_("Decompressing %s..."), ctx->realpath);
233 cmd = get_compression_cmd (ci->open, ctx);
236 debug_print (2, ("DecompressCmd: '%s'\n", cmd));
238 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
239 mutt_perror (ctx->realpath);
243 mutt_block_signals ();
244 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
246 mutt_unblock_signals ();
247 mutt_error _("Unable to lock mailbox!");
255 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
256 mutt_system (echo_cmd);
257 rc = mutt_system (cmd);
258 mbox_unlock_compressed (ctx, fp);
259 mutt_unblock_signals ();
263 mutt_any_key_to_continue (NULL);
265 p_delete(&ctx->compressinfo);
266 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
273 if (mutt_check_mailbox_compressed (ctx))
276 ctx->magic = mx_get_magic (ctx->path);
281 void restore_path (CONTEXT * ctx)
283 p_delete(&ctx->path);
284 ctx->path = ctx->realpath;
287 /* remove the temporary mailbox */
288 void remove_file (CONTEXT * ctx)
290 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
294 int mutt_open_append_compressed (CONTEXT * ctx)
297 COMPRESS_INFO *ci = set_compress_info (ctx);
299 if (!get_append_command (ctx->path, ctx)) {
300 if (ci->open && ci->close)
301 return (mutt_open_read_compressed (ctx));
304 p_delete(&ctx->compressinfo);
310 ctx->magic = DefaultMagic;
312 if (!is_new (ctx->realpath))
313 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
314 if ((fh = safe_fopen (ctx->path, "w")))
316 /* No error checking - the parent function will catch it */
321 /* close a compressed mailbox */
322 void mutt_fast_close_compressed (CONTEXT * ctx)
324 debug_print (2, ("called on '%s'\n", ctx->path));
326 if (ctx->compressinfo) {
330 /* if the folder was removed, remove the gzipped folder too */
331 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
332 remove (ctx->realpath);
337 p_delete(&ctx->compressinfo);
341 /* return 0 on success, -1 on failure */
342 int mutt_sync_compressed (CONTEXT * ctx)
347 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
350 mutt_message (_("Compressing %s..."), ctx->realpath);
352 cmd = get_compression_cmd (ci->close, ctx);
356 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
357 mutt_perror (ctx->realpath);
361 mutt_block_signals ();
362 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
364 mutt_unblock_signals ();
365 mutt_error _("Unable to lock mailbox!");
373 debug_print (2, ("CompressCommand: '%s'\n", cmd));
377 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
378 mutt_system (echo_cmd);
379 if (mutt_system (cmd)) {
380 mutt_any_key_to_continue (NULL);
382 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
387 mbox_unlock_compressed (ctx, fp);
388 mutt_unblock_signals ();
398 int mutt_slow_close_compressed (CONTEXT * ctx)
403 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
405 debug_print (2, ("called on '%s'\n", ctx->path));
407 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
408 || (append = ci->close)))) { /* if we can not or should not append,
409 * we only have to remove the compressed info, because sync was already
412 mutt_fast_close_compressed (ctx);
421 if (append == ci->close)
422 mutt_message (_("Compressing %s..."), ctx->realpath);
424 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
427 cmd = get_compression_cmd (append, ctx);
431 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
432 mutt_perror (ctx->realpath);
436 mutt_block_signals ();
437 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
439 mutt_unblock_signals ();
440 mutt_error _("Unable to lock mailbox!");
446 debug_print (2, ("CompressCmd: '%s'\n", cmd));
451 if (append == ci->close)
452 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
454 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
456 mutt_system (echo_cmd);
458 if (mutt_system (cmd)) {
459 mutt_any_key_to_continue (NULL);
461 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
464 mbox_unlock_compressed (ctx, fp);
465 mutt_unblock_signals ();
470 mbox_unlock_compressed (ctx, fp);
471 mutt_unblock_signals ();
476 p_delete(&ctx->compressinfo);
481 mx_t* compress_reg_mx (void) {
482 mx_t* fmt = p_new(mx_t, 1);
483 fmt->type = M_COMPRESSED;
485 fmt->mx_is_magic = mbox_is_magic;
486 fmt->mx_check_empty = mbox_check_empty;
487 fmt->mx_access = access;
488 fmt->mx_open_mailbox = mutt_open_read_compressed;