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 <lib-lib/mem.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/file.h>
20 #include <lib-sys/mutt_signal.h>
21 #include <lib-sys/unix.h>
23 #include <lib-ui/curses.h>
31 const char *close; /* close-hook command */
32 const char *open; /* open-hook command */
33 const char *append; /* append-hook command */
34 off_t size; /* size of real folder */
37 char echo_cmd[HUGE_STRING];
40 * ctx - context to lock
41 * excl - exclusive lock?
42 * retry - should retry if unable to lock?
44 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
48 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
50 else if (retry && !excl) {
58 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
63 mx_unlock_file (ctx->realpath, fileno (fp), 1);
68 static int is_new (const char *path)
70 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
73 static const char *find_compress_hook (int type, const char *path)
75 const char *c = mutt_find_hook (type, path);
77 return (!c || !*c) ? NULL : c;
80 int mutt_can_read_compressed (const char *path)
82 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
85 /* if the file is new, we really do not append, but create, and so use
86 * close-hook, and not append-hook
88 static const char *get_append_command (const char *path, const CONTEXT * ctx)
90 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
92 return (is_new (path)) ? ci->close : ci->append;
95 int mutt_can_append_compressed (const char *path)
100 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
102 magic = mx_get_magic (path);
104 if (magic != 0 && magic != M_COMPRESSED)
107 return (find_compress_hook (M_APPENDHOOK, path)
108 || (find_compress_hook (M_OPENHOOK, path)
109 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
112 /* open a compressed mailbox */
113 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
117 /* Now lets uncompress this thing */
118 ci = p_new(COMPRESS_INFO, 1);
119 ctx->compressinfo = (void *) ci;
120 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
121 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
122 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
126 static void set_path (CONTEXT * ctx)
128 char tmppath[_POSIX_PATH_MAX];
130 /* Setup the right paths */
131 ctx->realpath = ctx->path;
133 /* Uncompress to /tmp */
134 mutt_mktemp (tmppath);
135 ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
138 static int get_size (const char *path)
142 if (stat (path, &sb) != 0)
147 static void store_size (CONTEXT * ctx)
149 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
151 ci->size = get_size (ctx->realpath);
154 static const char *compresshook_format_str (char *dest, ssize_t destlen,
155 char op, const char *src,
157 const char *ifstring __attribute__ ((unused)),
158 const char *elsestring __attribute__ ((unused)),
160 format_flag flags __attribute__ ((unused)))
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];
191 mutt_FormatString (expanded, sizeof (expanded), cmd,
192 compresshook_format_str, (unsigned long) ctx, 0);
193 return m_strdup(expanded);
196 int mutt_check_mailbox_compressed (CONTEXT * ctx)
198 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
200 if (ci->size != get_size (ctx->realpath)) {
201 p_delete(&ctx->compressinfo);
202 p_delete(&ctx->realpath);
203 mutt_error _("Mailbox was corrupted!");
210 int mutt_open_read_compressed (CONTEXT * ctx)
216 COMPRESS_INFO *ci = set_compress_info (ctx);
220 p_delete(&ctx->compressinfo);
223 if (!ci->close || access (ctx->path, W_OK) != 0)
230 mutt_message (_("Decompressing %s..."), ctx->realpath);
232 cmd = get_compression_cmd (ci->open, ctx);
236 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
237 mutt_perror (ctx->realpath);
241 mutt_block_signals ();
242 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
244 mutt_unblock_signals ();
245 mutt_error _("Unable to lock mailbox!");
253 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
254 mutt_system (echo_cmd);
255 rc = mutt_system (cmd);
256 mbox_unlock_compressed (ctx, fp);
257 mutt_unblock_signals ();
261 mutt_any_key_to_continue (NULL);
263 p_delete(&ctx->compressinfo);
264 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
271 if (mutt_check_mailbox_compressed (ctx))
274 ctx->magic = mx_get_magic (ctx->path);
279 void restore_path (CONTEXT * ctx)
281 p_delete(&ctx->path);
282 ctx->path = ctx->realpath;
285 /* remove the temporary mailbox */
286 void remove_file (CONTEXT * ctx)
288 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
292 int mutt_open_append_compressed (CONTEXT * ctx)
295 COMPRESS_INFO *ci = set_compress_info (ctx);
297 if (!get_append_command (ctx->path, ctx)) {
298 if (ci->open && ci->close)
299 return (mutt_open_read_compressed (ctx));
302 p_delete(&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 if (ctx->compressinfo) {
326 /* if the folder was removed, remove the gzipped folder too */
327 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
328 remove (ctx->realpath);
333 p_delete(&ctx->compressinfo);
337 /* return 0 on success, -1 on failure */
338 int mutt_sync_compressed (CONTEXT * ctx)
343 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
346 mutt_message (_("Compressing %s..."), ctx->realpath);
348 cmd = get_compression_cmd (ci->close, ctx);
352 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
353 mutt_perror (ctx->realpath);
357 mutt_block_signals ();
358 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
360 mutt_unblock_signals ();
361 mutt_error _("Unable to lock mailbox!");
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 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
400 || (append = ci->close)))) { /* if we can not or should not append,
401 * we only have to remove the compressed info, because sync was already
404 mutt_fast_close_compressed (ctx);
413 if (append == ci->close)
414 mutt_message (_("Compressing %s..."), ctx->realpath);
416 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
419 cmd = get_compression_cmd (append, ctx);
423 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
424 mutt_perror (ctx->realpath);
428 mutt_block_signals ();
429 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
431 mutt_unblock_signals ();
432 mutt_error _("Unable to lock mailbox!");
441 if (append == ci->close)
442 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
444 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
446 mutt_system (echo_cmd);
448 if (mutt_system (cmd)) {
449 mutt_any_key_to_continue (NULL);
451 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
454 mbox_unlock_compressed (ctx, fp);
455 mutt_unblock_signals ();
460 mbox_unlock_compressed (ctx, fp);
461 mutt_unblock_signals ();
466 p_delete(&ctx->compressinfo);
471 mx_t* compress_reg_mx (void) {
472 mx_t* fmt = p_new(mx_t, 1);
473 fmt->type = M_COMPRESSED;
475 fmt->mx_is_magic = mbox_is_magic;
476 fmt->mx_check_empty = mbox_check_empty;
477 fmt->mx_access = access;
478 fmt->mx_open_mailbox = mutt_open_read_compressed;