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"
24 const char *close; /* close-hook command */
25 const char *open; /* open-hook command */
26 const char *append; /* append-hook command */
27 off_t size; /* size of real folder */
30 char echo_cmd[HUGE_STRING];
33 * ctx - context to lock
34 * excl - exclusive lock?
35 * retry - should retry if unable to lock?
37 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
41 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
43 else if (retry && !excl) {
51 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
56 mx_unlock_file (ctx->realpath, fileno (fp), 1);
61 static int is_new (const char *path)
63 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
66 static const char *find_compress_hook (int type, const char *path)
68 const char *c = mutt_find_hook (type, path);
70 return (!c || !*c) ? NULL : c;
73 int mutt_can_read_compressed (const char *path)
75 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
78 /* if the file is new, we really do not append, but create, and so use
79 * close-hook, and not append-hook
81 static const char *get_append_command (const char *path, const CONTEXT * ctx)
83 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
85 return (is_new (path)) ? ci->close : ci->append;
88 int mutt_can_append_compressed (const char *path)
93 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
95 magic = mx_get_magic (path);
97 if (magic != 0 && magic != M_COMPRESSED)
100 return (find_compress_hook (M_APPENDHOOK, path)
101 || (find_compress_hook (M_OPENHOOK, path)
102 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
105 /* open a compressed mailbox */
106 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
110 /* Now lets uncompress this thing */
111 ci = safe_malloc (sizeof (COMPRESS_INFO));
112 ctx->compressinfo = (void *) ci;
113 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
114 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
115 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
119 static void set_path (CONTEXT * ctx)
121 char tmppath[_POSIX_PATH_MAX];
123 /* Setup the right paths */
124 ctx->realpath = ctx->path;
126 /* Uncompress to /tmp */
127 mutt_mktemp (tmppath);
128 ctx->path = safe_malloc (mutt_strlen (tmppath) + 1);
129 strcpy (ctx->path, tmppath);
132 static int get_size (const char *path)
136 if (stat (path, &sb) != 0)
141 static void store_size (CONTEXT * ctx)
143 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
145 ci->size = get_size (ctx->realpath);
148 static const char *compresshook_format_str (char *dest, size_t destlen,
149 char op, const char *src,
151 const char *ifstring,
152 const char *elsestring,
156 char tmp[SHORT_STRING];
158 CONTEXT *ctx = (CONTEXT *) data;
162 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
163 snprintf (dest, destlen, tmp, ctx->realpath);
166 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
167 snprintf (dest, destlen, tmp, ctx->path);
173 /* check that the command has both %f and %t
174 * 0 means OK, -1 means error
176 int mutt_test_compress_command (const char *cmd)
178 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
181 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
183 char expanded[_POSIX_PATH_MAX];
185 mutt_FormatString (expanded, sizeof (expanded), cmd,
186 compresshook_format_str, (unsigned long) ctx, 0);
187 return safe_strdup (expanded);
190 int mutt_check_mailbox_compressed (CONTEXT * ctx)
192 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
194 if (ci->size != get_size (ctx->realpath)) {
195 FREE (&ctx->compressinfo);
196 FREE (&ctx->realpath);
197 mutt_error _("Mailbox was corrupted!");
204 int mutt_open_read_compressed (CONTEXT * ctx)
210 COMPRESS_INFO *ci = set_compress_info (ctx);
214 FREE (ctx->compressinfo);
217 if (!ci->close || access (ctx->path, W_OK) != 0)
224 mutt_message (_("Decompressing %s..."), ctx->realpath);
226 cmd = get_compression_cmd (ci->open, ctx);
229 dprint (2, (debugfile, "DecompressCmd: '%s'\n", cmd));
231 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
232 mutt_perror (ctx->realpath);
236 mutt_block_signals ();
237 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
239 mutt_unblock_signals ();
240 mutt_error _("Unable to lock mailbox!");
248 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
249 mutt_system (echo_cmd);
250 rc = mutt_system (cmd);
251 mbox_unlock_compressed (ctx, fp);
252 mutt_unblock_signals ();
256 mutt_any_key_to_continue (NULL);
258 FREE (ctx->compressinfo);
259 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
266 if (mutt_check_mailbox_compressed (ctx))
269 ctx->magic = mx_get_magic (ctx->path);
274 void restore_path (CONTEXT * ctx)
277 ctx->path = ctx->realpath;
280 /* remove the temporary mailbox */
281 void remove_file (CONTEXT * ctx)
283 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
287 int mutt_open_append_compressed (CONTEXT * ctx)
290 COMPRESS_INFO *ci = set_compress_info (ctx);
292 if (!get_append_command (ctx->path, ctx)) {
293 if (ci->open && ci->close)
294 return (mutt_open_read_compressed (ctx));
297 FREE (&ctx->compressinfo);
303 ctx->magic = DefaultMagic;
305 if (!is_new (ctx->realpath))
306 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
307 if ((fh = safe_fopen (ctx->path, "w")))
309 /* No error checking - the parent function will catch it */
314 /* close a compressed mailbox */
315 void mutt_fast_close_compressed (CONTEXT * ctx)
317 dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
320 if (ctx->compressinfo) {
324 /* if the folder was removed, remove the gzipped folder too */
325 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
326 remove (ctx->realpath);
331 FREE (&ctx->compressinfo);
335 /* return 0 on success, -1 on failure */
336 int mutt_sync_compressed (CONTEXT * ctx)
341 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
344 mutt_message (_("Compressing %s..."), ctx->realpath);
346 cmd = get_compression_cmd (ci->close, ctx);
350 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
351 mutt_perror (ctx->realpath);
355 mutt_block_signals ();
356 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
358 mutt_unblock_signals ();
359 mutt_error _("Unable to lock mailbox!");
367 dprint (2, (debugfile, "CompressCommand: '%s'\n", cmd));
371 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
372 mutt_system (echo_cmd);
373 if (mutt_system (cmd)) {
374 mutt_any_key_to_continue (NULL);
376 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
381 mbox_unlock_compressed (ctx, fp);
382 mutt_unblock_signals ();
392 int mutt_slow_close_compressed (CONTEXT * ctx)
397 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
399 dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n",
402 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
403 || (append = ci->close)))) { /* if we can not or should not append,
404 * we only have to remove the compressed info, because sync was already
407 mutt_fast_close_compressed (ctx);
416 if (append == ci->close)
417 mutt_message (_("Compressing %s..."), ctx->realpath);
419 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
422 cmd = get_compression_cmd (append, ctx);
426 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
427 mutt_perror (ctx->realpath);
431 mutt_block_signals ();
432 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
434 mutt_unblock_signals ();
435 mutt_error _("Unable to lock mailbox!");
441 dprint (2, (debugfile, "CompressCmd: '%s'\n", cmd));
446 if (append == ci->close)
447 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
449 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
451 mutt_system (echo_cmd);
453 if (mutt_system (cmd)) {
454 mutt_any_key_to_continue (NULL);
456 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
459 mbox_unlock_compressed (ctx, fp);
460 mutt_unblock_signals ();
465 mbox_unlock_compressed (ctx, fp);
466 mutt_unblock_signals ();
471 FREE (&ctx->compressinfo);
476 #endif /* USE_COMPRESSED */