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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-sys/mutt_signal.h>
13 #include <lib-sys/unix.h>
15 #include <lib-ui/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 static 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 static 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 = p_new(COMPRESS_INFO, 1);
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 = p_dupstr(tmppath, m_strlen(tmppath));
131 static int get_size (const char *path)
135 if (stat (path, &sb) != 0)
140 static void store_size (CONTEXT * ctx)
142 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
144 ci->size = get_size (ctx->realpath);
147 static const char *compresshook_format_str (char *dest, ssize_t destlen,
148 char op, const char *src,
150 const char *ifstring __attribute__ ((unused)),
151 const char *elsestring __attribute__ ((unused)),
153 format_flag flags __attribute__ ((unused)))
155 char tmp[SHORT_STRING];
157 CONTEXT *ctx = (CONTEXT *) data;
161 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
162 snprintf (dest, destlen, tmp, ctx->realpath);
165 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
166 snprintf (dest, destlen, tmp, ctx->path);
172 /* check that the command has both %f and %t
173 * 0 means OK, -1 means error
175 int mutt_test_compress_command (const char *cmd)
177 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
180 static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
182 char expanded[_POSIX_PATH_MAX];
184 mutt_FormatString (expanded, sizeof (expanded), cmd,
185 compresshook_format_str, (unsigned long) ctx, 0);
186 return m_strdup(expanded);
189 int mutt_check_mailbox_compressed (CONTEXT * ctx)
191 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
193 if (ci->size != get_size (ctx->realpath)) {
194 p_delete(&ctx->compressinfo);
195 p_delete(&ctx->realpath);
196 mutt_error _("Mailbox was corrupted!");
203 int mutt_open_read_compressed (CONTEXT * ctx)
209 COMPRESS_INFO *ci = set_compress_info (ctx);
213 p_delete(&ctx->compressinfo);
216 if (!ci->close || access (ctx->path, W_OK) != 0)
223 mutt_message (_("Decompressing %s..."), ctx->realpath);
225 cmd = get_compression_cmd (ci->open, ctx);
229 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
230 mutt_perror (ctx->realpath);
234 mutt_block_signals ();
235 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
237 mutt_unblock_signals ();
238 mutt_error _("Unable to lock mailbox!");
246 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
247 mutt_system (echo_cmd);
248 rc = mutt_system (cmd);
249 mbox_unlock_compressed (ctx, fp);
250 mutt_unblock_signals ();
254 mutt_any_key_to_continue (NULL);
256 p_delete(&ctx->compressinfo);
257 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
264 if (mutt_check_mailbox_compressed (ctx))
267 ctx->magic = mx_get_magic (ctx->path);
272 static void restore_path (CONTEXT * ctx)
274 p_delete(&ctx->path);
275 ctx->path = ctx->realpath;
278 /* remove the temporary mailbox */
279 static void remove_file (CONTEXT * ctx)
281 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
285 int mutt_open_append_compressed (CONTEXT * ctx)
288 COMPRESS_INFO *ci = set_compress_info (ctx);
290 if (!get_append_command (ctx->path, ctx)) {
291 if (ci->open && ci->close)
292 return (mutt_open_read_compressed (ctx));
295 p_delete(&ctx->compressinfo);
301 ctx->magic = DefaultMagic;
303 if (!is_new (ctx->realpath))
304 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
305 if ((fh = safe_fopen (ctx->path, "w")))
307 /* No error checking - the parent function will catch it */
312 /* close a compressed mailbox */
313 void mutt_fast_close_compressed (CONTEXT * ctx)
315 if (ctx->compressinfo) {
319 /* if the folder was removed, remove the gzipped folder too */
320 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
321 remove (ctx->realpath);
326 p_delete(&ctx->compressinfo);
330 /* return 0 on success, -1 on failure */
331 int mutt_sync_compressed (CONTEXT * ctx)
336 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
339 mutt_message (_("Compressing %s..."), ctx->realpath);
341 cmd = get_compression_cmd (ci->close, ctx);
345 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
346 mutt_perror (ctx->realpath);
350 mutt_block_signals ();
351 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
353 mutt_unblock_signals ();
354 mutt_error _("Unable to lock mailbox!");
364 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
365 mutt_system (echo_cmd);
366 if (mutt_system (cmd)) {
367 mutt_any_key_to_continue (NULL);
369 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
374 mbox_unlock_compressed (ctx, fp);
375 mutt_unblock_signals ();
385 int mutt_slow_close_compressed (CONTEXT * ctx)
390 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
392 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
393 || (append = ci->close)))) { /* if we can not or should not append,
394 * we only have to remove the compressed info, because sync was already
397 mutt_fast_close_compressed (ctx);
406 if (append == ci->close)
407 mutt_message (_("Compressing %s..."), ctx->realpath);
409 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
412 cmd = get_compression_cmd (append, ctx);
416 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
417 mutt_perror (ctx->realpath);
421 mutt_block_signals ();
422 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
424 mutt_unblock_signals ();
425 mutt_error _("Unable to lock mailbox!");
434 if (append == ci->close)
435 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
437 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
439 mutt_system (echo_cmd);
441 if (mutt_system (cmd)) {
442 mutt_any_key_to_continue (NULL);
444 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
447 mbox_unlock_compressed (ctx, fp);
448 mutt_unblock_signals ();
453 mbox_unlock_compressed (ctx, fp);
454 mutt_unblock_signals ();
459 p_delete(&ctx->compressinfo);
464 mx_t* compress_reg_mx (void) {
465 mx_t* fmt = p_new(mx_t, 1);
466 fmt->type = M_COMPRESSED;
468 fmt->mx_is_magic = mbox_is_magic;
469 fmt->mx_check_empty = mbox_check_empty;
470 fmt->mx_access = access;
471 fmt->mx_open_mailbox = mutt_open_read_compressed;