2 * Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "mutt_curses.h"
34 const char *close; /* close-hook command */
35 const char *open; /* open-hook command */
36 const char *append; /* append-hook command */
37 off_t size; /* size of real folder */
40 char echo_cmd[HUGE_STRING];
43 * ctx - context to lock
44 * excl - exclusive lock?
45 * retry - should retry if unable to lock?
47 int mbox_lock_compressed (CONTEXT *ctx, FILE *fp, int excl, int retry)
51 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
53 else if (retry && !excl)
62 void mbox_unlock_compressed (CONTEXT *ctx, FILE *fp)
68 mx_unlock_file (ctx->realpath, fileno (fp), 1);
73 static int is_new (const char *path)
75 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
78 static const char* find_compress_hook (int type, const char *path)
80 const char* c = mutt_find_hook (type, path);
81 return (!c || !*c) ? NULL : c;
84 int mutt_can_read_compressed (const char *path)
86 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
89 /* if the file is new, we really do not append, but create, and so use
90 * close-hook, and not append-hook
92 static const char* get_append_command (const char *path, const CONTEXT* ctx)
94 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
95 return (is_new (path)) ? ci->close : ci->append;
98 int mutt_can_append_compressed (const char *path)
103 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
105 magic = mx_get_magic (path);
107 if (magic != 0 && magic != M_COMPRESSED)
110 return (find_compress_hook (M_APPENDHOOK, path)
111 || (find_compress_hook (M_OPENHOOK, path)
112 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
115 /* open a compressed mailbox */
116 static COMPRESS_INFO *set_compress_info (CONTEXT *ctx)
120 /* Now lets uncompress this thing */
121 ci = safe_malloc (sizeof (COMPRESS_INFO));
122 ctx->compressinfo = (void*) ci;
123 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
124 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
125 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
129 static void set_path (CONTEXT* ctx)
131 char tmppath[_POSIX_PATH_MAX];
133 /* Setup the right paths */
134 ctx->realpath = ctx->path;
136 /* Uncompress to /tmp */
137 mutt_mktemp (tmppath);
138 ctx->path = safe_malloc (strlen (tmppath) + 1);
139 strcpy (ctx->path, tmppath);
142 static int get_size (const char* path)
145 if (stat (path, &sb) != 0)
150 static void store_size (CONTEXT* ctx)
152 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
153 ci->size = get_size (ctx->realpath);
157 compresshook_format_str (char *dest, size_t destlen, char op, const char *src,
158 const char *fmt, const char *ifstring,
159 const char *elsestring, unsigned long data,
162 char tmp[SHORT_STRING];
164 CONTEXT *ctx = (CONTEXT *) data;
168 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
169 snprintf (dest, destlen, tmp, ctx->realpath);
172 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
173 snprintf (dest, destlen, tmp, ctx->path);
179 /* check that the command has both %f and %t
180 * 0 means OK, -1 means error
182 int mutt_test_compress_command (const char* cmd)
184 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
187 static char *get_compression_cmd (const char* cmd, const CONTEXT* ctx)
189 char expanded[_POSIX_PATH_MAX];
190 mutt_FormatString (expanded, sizeof (expanded), cmd, compresshook_format_str,
191 (unsigned long) ctx, 0);
192 return safe_strdup (expanded);
195 int mutt_check_mailbox_compressed (CONTEXT* ctx)
197 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
198 if (ci->size != get_size (ctx->realpath))
200 FREE (&ctx->compressinfo);
201 FREE (&ctx->realpath);
202 mutt_error _("Mailbox was corrupted!");
208 int mutt_open_read_compressed (CONTEXT *ctx)
214 COMPRESS_INFO *ci = set_compress_info (ctx);
217 FREE (ctx->compressinfo);
220 if (!ci->close || access (ctx->path, W_OK) != 0)
227 mutt_message (_("Decompressing %s..."), ctx->realpath);
229 cmd = get_compression_cmd (ci->open, ctx);
232 dprint (2, (debugfile, "DecompressCmd: '%s'\n", cmd));
234 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)
244 mutt_unblock_signals ();
245 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 ();
261 mutt_any_key_to_continue (NULL);
263 FREE (ctx->compressinfo);
264 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"), cmd);
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))
298 if (ci->open && ci->close)
299 return (mutt_open_read_compressed (ctx));
302 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 dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
325 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 FREE (&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)
358 mutt_perror (ctx->realpath);
362 mutt_block_signals ();
363 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1)
366 mutt_unblock_signals ();
367 mutt_error _("Unable to lock mailbox!");
375 dprint (2, (debugfile, "CompressCommand: '%s'\n", cmd));
379 sprintf(echo_cmd,_("echo Compressing %s..."), ctx->realpath);
380 mutt_system(echo_cmd);
381 if (mutt_system (cmd))
383 mutt_any_key_to_continue (NULL);
384 mutt_error (_("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"), ctx->path);
388 mbox_unlock_compressed (ctx, fp);
389 mutt_unblock_signals ();
399 int mutt_slow_close_compressed (CONTEXT *ctx)
404 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
406 dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n",
410 && ((append = get_append_command (ctx->realpath, ctx))
411 || (append = ci->close))))
412 { /* if we can not or should not append,
413 * we only have to remove the compressed info, because sync was already
416 mutt_fast_close_compressed (ctx);
426 if (append == ci->close)
427 mutt_message (_("Compressing %s..."), ctx->realpath);
429 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
432 cmd = get_compression_cmd (append, ctx);
436 if ((fp = fopen (ctx->realpath, "a")) == NULL)
438 mutt_perror (ctx->realpath);
442 mutt_block_signals ();
443 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1)
446 mutt_unblock_signals ();
447 mutt_error _("Unable to lock mailbox!");
452 dprint (2, (debugfile, "CompressCmd: '%s'\n", cmd));
457 if (append == ci->close)
458 sprintf(echo_cmd,_("echo Compressing %s..."), ctx->realpath);
460 sprintf(echo_cmd,_("echo Compressed-appending to %s..."), ctx->realpath);
461 mutt_system(echo_cmd);
463 if (mutt_system (cmd))
465 mutt_any_key_to_continue (NULL);
466 mutt_error (_(" %s: Error compressing mailbox! Uncompressed one kept!\n"),
469 mbox_unlock_compressed (ctx, fp);
470 mutt_unblock_signals ();
475 mbox_unlock_compressed (ctx, fp);
476 mutt_unblock_signals ();
481 FREE (&ctx->compressinfo);
486 #endif /* USE_COMPRESSED */