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"
33 const char *close; /* close-hook command */
34 const char *open; /* open-hook command */
35 const char *append; /* append-hook command */
36 off_t size; /* size of real folder */
39 char echo_cmd[HUGE_STRING];
42 * ctx - context to lock
43 * excl - exclusive lock?
44 * retry - should retry if unable to lock?
46 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
50 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
52 else if (retry && !excl) {
60 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
65 mx_unlock_file (ctx->realpath, fileno (fp), 1);
70 static int is_new (const char *path)
72 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
75 static const char *find_compress_hook (int type, const char *path)
77 const char *c = mutt_find_hook (type, path);
79 return (!c || !*c) ? NULL : c;
82 int mutt_can_read_compressed (const char *path)
84 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
87 /* if the file is new, we really do not append, but create, and so use
88 * close-hook, and not append-hook
90 static const char *get_append_command (const char *path, const CONTEXT * ctx)
92 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
94 return (is_new (path)) ? ci->close : ci->append;
97 int mutt_can_append_compressed (const char *path)
102 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
104 magic = mx_get_magic (path);
106 if (magic != 0 && magic != M_COMPRESSED)
109 return (find_compress_hook (M_APPENDHOOK, path)
110 || (find_compress_hook (M_OPENHOOK, path)
111 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
114 /* open a compressed mailbox */
115 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
119 /* Now lets uncompress this thing */
120 ci = safe_malloc (sizeof (COMPRESS_INFO));
121 ctx->compressinfo = (void *) ci;
122 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
123 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
124 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
128 static void set_path (CONTEXT * ctx)
130 char tmppath[_POSIX_PATH_MAX];
132 /* Setup the right paths */
133 ctx->realpath = ctx->path;
135 /* Uncompress to /tmp */
136 mutt_mktemp (tmppath);
137 ctx->path = safe_malloc (strlen (tmppath) + 1);
138 strcpy (ctx->path, tmppath);
141 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;
154 ci->size = get_size (ctx->realpath);
157 static const char *compresshook_format_str (char *dest, size_t destlen,
158 char op, const char *src,
160 const char *ifstring,
161 const char *elsestring,
165 char tmp[SHORT_STRING];
167 CONTEXT *ctx = (CONTEXT *) data;
171 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
172 snprintf (dest, destlen, tmp, ctx->realpath);
175 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
176 snprintf (dest, destlen, tmp, ctx->path);
182 /* check that the command has both %f and %t
183 * 0 means OK, -1 means error
185 int mutt_test_compress_command (const char *cmd)
187 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
190 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
192 char expanded[_POSIX_PATH_MAX];
194 mutt_FormatString (expanded, sizeof (expanded), cmd,
195 compresshook_format_str, (unsigned long) ctx, 0);
196 return safe_strdup (expanded);
199 int mutt_check_mailbox_compressed (CONTEXT * ctx)
201 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
203 if (ci->size != get_size (ctx->realpath)) {
204 FREE (&ctx->compressinfo);
205 FREE (&ctx->realpath);
206 mutt_error _("Mailbox was corrupted!");
213 int mutt_open_read_compressed (CONTEXT * ctx)
219 COMPRESS_INFO *ci = set_compress_info (ctx);
223 FREE (ctx->compressinfo);
226 if (!ci->close || access (ctx->path, W_OK) != 0)
233 mutt_message (_("Decompressing %s..."), ctx->realpath);
235 cmd = get_compression_cmd (ci->open, ctx);
238 dprint (2, (debugfile, "DecompressCmd: '%s'\n", cmd));
240 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
241 mutt_perror (ctx->realpath);
245 mutt_block_signals ();
246 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
248 mutt_unblock_signals ();
249 mutt_error _("Unable to lock mailbox!");
257 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
258 mutt_system (echo_cmd);
259 rc = mutt_system (cmd);
260 mbox_unlock_compressed (ctx, fp);
261 mutt_unblock_signals ();
265 mutt_any_key_to_continue (NULL);
267 FREE (ctx->compressinfo);
268 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
275 if (mutt_check_mailbox_compressed (ctx))
278 ctx->magic = mx_get_magic (ctx->path);
283 void restore_path (CONTEXT * ctx)
286 ctx->path = ctx->realpath;
289 /* remove the temporary mailbox */
290 void remove_file (CONTEXT * ctx)
292 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
296 int mutt_open_append_compressed (CONTEXT * ctx)
299 COMPRESS_INFO *ci = set_compress_info (ctx);
301 if (!get_append_command (ctx->path, ctx)) {
302 if (ci->open && ci->close)
303 return (mutt_open_read_compressed (ctx));
306 FREE (&ctx->compressinfo);
312 ctx->magic = DefaultMagic;
314 if (!is_new (ctx->realpath))
315 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
316 if ((fh = safe_fopen (ctx->path, "w")))
318 /* No error checking - the parent function will catch it */
323 /* close a compressed mailbox */
324 void mutt_fast_close_compressed (CONTEXT * ctx)
326 dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
329 if (ctx->compressinfo) {
333 /* if the folder was removed, remove the gzipped folder too */
334 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
335 remove (ctx->realpath);
340 FREE (&ctx->compressinfo);
344 /* return 0 on success, -1 on failure */
345 int mutt_sync_compressed (CONTEXT * ctx)
350 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
353 mutt_message (_("Compressing %s..."), ctx->realpath);
355 cmd = get_compression_cmd (ci->close, ctx);
359 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
360 mutt_perror (ctx->realpath);
364 mutt_block_signals ();
365 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
367 mutt_unblock_signals ();
368 mutt_error _("Unable to lock mailbox!");
376 dprint (2, (debugfile, "CompressCommand: '%s'\n", cmd));
380 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
381 mutt_system (echo_cmd);
382 if (mutt_system (cmd)) {
383 mutt_any_key_to_continue (NULL);
385 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
390 mbox_unlock_compressed (ctx, fp);
391 mutt_unblock_signals ();
401 int mutt_slow_close_compressed (CONTEXT * ctx)
406 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
408 dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n",
411 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
412 || (append = ci->close)))) { /* 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);
425 if (append == ci->close)
426 mutt_message (_("Compressing %s..."), ctx->realpath);
428 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
431 cmd = get_compression_cmd (append, ctx);
435 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
436 mutt_perror (ctx->realpath);
440 mutt_block_signals ();
441 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
443 mutt_unblock_signals ();
444 mutt_error _("Unable to lock mailbox!");
450 dprint (2, (debugfile, "CompressCmd: '%s'\n", cmd));
455 if (append == ci->close)
456 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
458 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
460 mutt_system (echo_cmd);
462 if (mutt_system (cmd)) {
463 mutt_any_key_to_continue (NULL);
465 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
468 mbox_unlock_compressed (ctx, fp);
469 mutt_unblock_signals ();
474 mbox_unlock_compressed (ctx, fp);
475 mutt_unblock_signals ();
480 FREE (&ctx->compressinfo);
485 #endif /* USE_COMPRESSED */