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 "mutt_curses.h"
27 const char *close; /* close-hook command */
28 const char *open; /* open-hook command */
29 const char *append; /* append-hook command */
30 off_t size; /* size of real folder */
33 char echo_cmd[HUGE_STRING];
36 * ctx - context to lock
37 * excl - exclusive lock?
38 * retry - should retry if unable to lock?
40 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
44 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
46 else if (retry && !excl) {
54 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
59 mx_unlock_file (ctx->realpath, fileno (fp), 1);
64 static int is_new (const char *path)
66 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
69 static const char *find_compress_hook (int type, const char *path)
71 const char *c = mutt_find_hook (type, path);
73 return (!c || !*c) ? NULL : c;
76 int mutt_can_read_compressed (const char *path)
78 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
81 /* if the file is new, we really do not append, but create, and so use
82 * close-hook, and not append-hook
84 static const char *get_append_command (const char *path, const CONTEXT * ctx)
86 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
88 return (is_new (path)) ? ci->close : ci->append;
91 int mutt_can_append_compressed (const char *path)
96 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
98 magic = mx_get_magic (path);
100 if (magic != 0 && magic != M_COMPRESSED)
103 return (find_compress_hook (M_APPENDHOOK, path)
104 || (find_compress_hook (M_OPENHOOK, path)
105 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
108 /* open a compressed mailbox */
109 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
113 /* Now lets uncompress this thing */
114 ci = safe_malloc (sizeof (COMPRESS_INFO));
115 ctx->compressinfo = (void *) ci;
116 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
117 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
118 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
122 static void set_path (CONTEXT * ctx)
124 char tmppath[_POSIX_PATH_MAX];
126 /* Setup the right paths */
127 ctx->realpath = ctx->path;
129 /* Uncompress to /tmp */
130 mutt_mktemp (tmppath);
131 ctx->path = safe_malloc (safe_strlen (tmppath) + 1);
132 strcpy (ctx->path, tmppath);
135 static int get_size (const char *path)
139 if (stat (path, &sb) != 0)
144 static void store_size (CONTEXT * ctx)
146 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
148 ci->size = get_size (ctx->realpath);
151 static const char *compresshook_format_str (char *dest, size_t destlen,
152 char op, const char *src,
154 const char *ifstring,
155 const char *elsestring,
159 char tmp[SHORT_STRING];
161 CONTEXT *ctx = (CONTEXT *) data;
165 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
166 snprintf (dest, destlen, tmp, ctx->realpath);
169 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
170 snprintf (dest, destlen, tmp, ctx->path);
176 /* check that the command has both %f and %t
177 * 0 means OK, -1 means error
179 int mutt_test_compress_command (const char *cmd)
181 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
184 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
186 char expanded[_POSIX_PATH_MAX];
188 mutt_FormatString (expanded, sizeof (expanded), cmd,
189 compresshook_format_str, (unsigned long) ctx, 0);
190 return safe_strdup (expanded);
193 int mutt_check_mailbox_compressed (CONTEXT * ctx)
195 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
197 if (ci->size != get_size (ctx->realpath)) {
198 FREE (&ctx->compressinfo);
199 FREE (&ctx->realpath);
200 mutt_error _("Mailbox was corrupted!");
207 int mutt_open_read_compressed (CONTEXT * ctx)
213 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) {
235 mutt_perror (ctx->realpath);
239 mutt_block_signals ();
240 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
242 mutt_unblock_signals ();
243 mutt_error _("Unable to lock mailbox!");
251 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
252 mutt_system (echo_cmd);
253 rc = mutt_system (cmd);
254 mbox_unlock_compressed (ctx, fp);
255 mutt_unblock_signals ();
259 mutt_any_key_to_continue (NULL);
261 FREE (ctx->compressinfo);
262 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
269 if (mutt_check_mailbox_compressed (ctx))
272 ctx->magic = mx_get_magic (ctx->path);
277 void restore_path (CONTEXT * ctx)
280 ctx->path = ctx->realpath;
283 /* remove the temporary mailbox */
284 void remove_file (CONTEXT * ctx)
286 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
290 int mutt_open_append_compressed (CONTEXT * ctx)
293 COMPRESS_INFO *ci = set_compress_info (ctx);
295 if (!get_append_command (ctx->path, ctx)) {
296 if (ci->open && ci->close)
297 return (mutt_open_read_compressed (ctx));
300 FREE (&ctx->compressinfo);
306 ctx->magic = DefaultMagic;
308 if (!is_new (ctx->realpath))
309 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
310 if ((fh = safe_fopen (ctx->path, "w")))
312 /* No error checking - the parent function will catch it */
317 /* close a compressed mailbox */
318 void mutt_fast_close_compressed (CONTEXT * ctx)
320 dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
323 if (ctx->compressinfo) {
327 /* if the folder was removed, remove the gzipped folder too */
328 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
329 remove (ctx->realpath);
334 FREE (&ctx->compressinfo);
338 /* return 0 on success, -1 on failure */
339 int mutt_sync_compressed (CONTEXT * ctx)
344 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
347 mutt_message (_("Compressing %s..."), ctx->realpath);
349 cmd = get_compression_cmd (ci->close, ctx);
353 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
354 mutt_perror (ctx->realpath);
358 mutt_block_signals ();
359 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
361 mutt_unblock_signals ();
362 mutt_error _("Unable to lock mailbox!");
370 dprint (2, (debugfile, "CompressCommand: '%s'\n", cmd));
374 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
375 mutt_system (echo_cmd);
376 if (mutt_system (cmd)) {
377 mutt_any_key_to_continue (NULL);
379 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
384 mbox_unlock_compressed (ctx, fp);
385 mutt_unblock_signals ();
395 int mutt_slow_close_compressed (CONTEXT * ctx)
400 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
402 dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n",
405 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
406 || (append = ci->close)))) { /* if we can not or should not append,
407 * we only have to remove the compressed info, because sync was already
410 mutt_fast_close_compressed (ctx);
419 if (append == ci->close)
420 mutt_message (_("Compressing %s..."), ctx->realpath);
422 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
425 cmd = get_compression_cmd (append, ctx);
429 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
430 mutt_perror (ctx->realpath);
434 mutt_block_signals ();
435 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
437 mutt_unblock_signals ();
438 mutt_error _("Unable to lock mailbox!");
444 dprint (2, (debugfile, "CompressCmd: '%s'\n", cmd));
449 if (append == ci->close)
450 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
452 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
454 mutt_system (echo_cmd);
456 if (mutt_system (cmd)) {
457 mutt_any_key_to_continue (NULL);
459 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
462 mbox_unlock_compressed (ctx, fp);
463 mutt_unblock_signals ();
468 mbox_unlock_compressed (ctx, fp);
469 mutt_unblock_signals ();
474 FREE (&ctx->compressinfo);
479 #endif /* USE_COMPRESSED */