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/lib-lib.h>
17 #include <lib-sys/mutt_signal.h>
18 #include <lib-sys/unix.h>
20 #include <lib-ui/curses.h>
28 const char *close; /* close-hook command */
29 const char *open; /* open-hook command */
30 const char *append; /* append-hook command */
31 off_t size; /* size of real folder */
34 char echo_cmd[HUGE_STRING];
37 * ctx - context to lock
38 * excl - exclusive lock?
39 * retry - should retry if unable to lock?
41 int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
45 if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
47 else if (retry && !excl) {
55 void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
60 mx_unlock_file (ctx->realpath, fileno (fp), 1);
65 static int is_new (const char *path)
67 return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
70 static const char *find_compress_hook (int type, const char *path)
72 const char *c = mutt_find_hook (type, path);
74 return (!c || !*c) ? NULL : c;
77 int mutt_can_read_compressed (const char *path)
79 return find_compress_hook (M_OPENHOOK, path) ? 1 : 0;
82 /* if the file is new, we really do not append, but create, and so use
83 * close-hook, and not append-hook
85 static const char *get_append_command (const char *path, const CONTEXT * ctx)
87 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
89 return (is_new (path)) ? ci->close : ci->append;
92 int mutt_can_append_compressed (const char *path)
97 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
99 magic = mx_get_magic (path);
101 if (magic != 0 && magic != M_COMPRESSED)
104 return (find_compress_hook (M_APPENDHOOK, path)
105 || (find_compress_hook (M_OPENHOOK, path)
106 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
109 /* open a compressed mailbox */
110 static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
114 /* Now lets uncompress this thing */
115 ci = p_new(COMPRESS_INFO, 1);
116 ctx->compressinfo = (void *) ci;
117 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
118 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
119 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
123 static void set_path (CONTEXT * ctx)
125 char tmppath[_POSIX_PATH_MAX];
127 /* Setup the right paths */
128 ctx->realpath = ctx->path;
130 /* Uncompress to /tmp */
131 mutt_mktemp (tmppath);
132 ctx->path = p_dupstr(tmppath, m_strlen(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, ssize_t destlen,
152 char op, const char *src,
154 const char *ifstring __attribute__ ((unused)),
155 const char *elsestring __attribute__ ((unused)),
157 format_flag flags __attribute__ ((unused)))
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 m_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 p_delete(&ctx->compressinfo);
199 p_delete(&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 p_delete(&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);
233 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
234 mutt_perror (ctx->realpath);
238 mutt_block_signals ();
239 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
241 mutt_unblock_signals ();
242 mutt_error _("Unable to lock mailbox!");
250 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
251 mutt_system (echo_cmd);
252 rc = mutt_system (cmd);
253 mbox_unlock_compressed (ctx, fp);
254 mutt_unblock_signals ();
258 mutt_any_key_to_continue (NULL);
260 p_delete(&ctx->compressinfo);
261 mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
268 if (mutt_check_mailbox_compressed (ctx))
271 ctx->magic = mx_get_magic (ctx->path);
276 void restore_path (CONTEXT * ctx)
278 p_delete(&ctx->path);
279 ctx->path = ctx->realpath;
282 /* remove the temporary mailbox */
283 void remove_file (CONTEXT * ctx)
285 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
289 int mutt_open_append_compressed (CONTEXT * ctx)
292 COMPRESS_INFO *ci = set_compress_info (ctx);
294 if (!get_append_command (ctx->path, ctx)) {
295 if (ci->open && ci->close)
296 return (mutt_open_read_compressed (ctx));
299 p_delete(&ctx->compressinfo);
305 ctx->magic = DefaultMagic;
307 if (!is_new (ctx->realpath))
308 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
309 if ((fh = safe_fopen (ctx->path, "w")))
311 /* No error checking - the parent function will catch it */
316 /* close a compressed mailbox */
317 void mutt_fast_close_compressed (CONTEXT * ctx)
319 if (ctx->compressinfo) {
323 /* if the folder was removed, remove the gzipped folder too */
324 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
325 remove (ctx->realpath);
330 p_delete(&ctx->compressinfo);
334 /* return 0 on success, -1 on failure */
335 int mutt_sync_compressed (CONTEXT * ctx)
340 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
343 mutt_message (_("Compressing %s..."), ctx->realpath);
345 cmd = get_compression_cmd (ci->close, ctx);
349 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
350 mutt_perror (ctx->realpath);
354 mutt_block_signals ();
355 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
357 mutt_unblock_signals ();
358 mutt_error _("Unable to lock mailbox!");
368 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
369 mutt_system (echo_cmd);
370 if (mutt_system (cmd)) {
371 mutt_any_key_to_continue (NULL);
373 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
378 mbox_unlock_compressed (ctx, fp);
379 mutt_unblock_signals ();
389 int mutt_slow_close_compressed (CONTEXT * ctx)
394 COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
396 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
397 || (append = ci->close)))) { /* if we can not or should not append,
398 * we only have to remove the compressed info, because sync was already
401 mutt_fast_close_compressed (ctx);
410 if (append == ci->close)
411 mutt_message (_("Compressing %s..."), ctx->realpath);
413 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
416 cmd = get_compression_cmd (append, ctx);
420 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
421 mutt_perror (ctx->realpath);
425 mutt_block_signals ();
426 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
428 mutt_unblock_signals ();
429 mutt_error _("Unable to lock mailbox!");
438 if (append == ci->close)
439 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
441 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
443 mutt_system (echo_cmd);
445 if (mutt_system (cmd)) {
446 mutt_any_key_to_continue (NULL);
448 (" %s: Error compressing mailbox! Uncompressed one kept!\n"),
451 mbox_unlock_compressed (ctx, fp);
452 mutt_unblock_signals ();
457 mbox_unlock_compressed (ctx, fp);
458 mutt_unblock_signals ();
463 p_delete(&ctx->compressinfo);
468 mx_t* compress_reg_mx (void) {
469 mx_t* fmt = p_new(mx_t, 1);
470 fmt->type = M_COMPRESSED;
472 fmt->mx_is_magic = mbox_is_magic;
473 fmt->mx_check_empty = mbox_check_empty;
474 fmt->mx_access = access;
475 fmt->mx_open_mailbox = mutt_open_read_compressed;