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>
23 struct compress_info {
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) && errno == ENOENT);
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 return is_new(path) ? ctx->cinfo->close : ctx->cinfo->append;
86 int mutt_can_append_compressed (const char *path)
91 return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
93 magic = mx_get_magic (path);
95 if (magic != 0 && magic != M_COMPRESSED)
98 return (find_compress_hook (M_APPENDHOOK, path)
99 || (find_compress_hook (M_OPENHOOK, path)
100 && find_compress_hook (M_CLOSEHOOK, path))) ? 1 : 0;
103 /* open a compressed mailbox */
104 static compress_info *set_compress_info (CONTEXT * ctx)
106 compress_info *ci = p_new(compress_info, 1);
108 /* Now lets uncompress this thing */
109 ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
110 ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
111 ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
113 return (ctx->cinfo = ci);
116 static int get_size (const char *path)
120 if (stat (path, &sb) != 0)
126 compresshook_format_str(char *dest, ssize_t destlen,
127 char op, const char *src, const char *fmt,
128 const char *ifstr __attribute__ ((unused)),
129 const char *elstr __attribute__ ((unused)),
131 format_flag flags __attribute__ ((unused)))
135 CONTEXT *ctx = data.ptr;
139 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
140 snprintf (dest, destlen, tmp, ctx->realpath);
143 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
144 snprintf (dest, destlen, tmp, ctx->path);
150 /* check that the command has both %f and %t
151 * 0 means OK, -1 means error
153 int mutt_test_compress_command (const char *cmd)
155 return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1;
158 static char *get_compression_cmd(const char *cmd, const CONTEXT *ctx)
160 char buf[_POSIX_PATH_MAX];
161 m_strformat(buf, sizeof(buf), 0, cmd, compresshook_format_str, (void*)ctx, 0);
162 return m_strdup(buf);
165 int mutt_check_mailbox_compressed (CONTEXT * ctx)
167 if (ctx->cinfo->size != get_size (ctx->realpath)) {
168 p_delete(&ctx->cinfo);
169 p_delete(&ctx->realpath);
170 mutt_error _("Mailbox was corrupted!");
177 int mutt_open_read_compressed (CONTEXT * ctx)
182 char tmppath[_POSIX_PATH_MAX];
185 compress_info *ci = set_compress_info (ctx);
189 p_delete(&ctx->cinfo);
192 if (!ci->close || access (ctx->path, W_OK) != 0)
195 /* Setup the right paths */
196 ctx->realpath = ctx->path;
198 /* Uncompress to /tmp */
199 tmpfd = m_tempfd(tmppath, sizeof(tmppath), NONULL(Tempdir), NULL);
200 /* If we cannot open tempfile, that means the file already exists (!?)
201 * or we are following a symlink, which is bad and insecure.
208 ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
210 ctx->cinfo->size = get_size(ctx->realpath);
213 mutt_message (_("Decompressing %s..."), ctx->realpath);
215 cmd = get_compression_cmd (ci->open, ctx);
219 if ((fp = fopen (ctx->realpath, "r")) == NULL) {
220 mutt_perror (ctx->realpath);
224 mutt_block_signals ();
225 if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
227 mutt_unblock_signals ();
228 mutt_error _("Unable to lock mailbox!");
236 sprintf (echo_cmd, _("echo Decompressing %s..."), ctx->realpath);
237 mutt_system (echo_cmd);
238 rc = mutt_system (cmd);
239 mbox_unlock_compressed (ctx, fp);
240 mutt_unblock_signals ();
244 mutt_any_key_to_continue (NULL);
246 p_delete(&ctx->cinfo);
247 mutt_error(_("Error executing: %s : unable to open the mailbox!\n"), cmd);
253 if (mutt_check_mailbox_compressed (ctx))
256 ctx->magic = mx_get_magic (ctx->path);
261 static void restore_path (CONTEXT * ctx)
263 p_delete(&ctx->path);
264 ctx->path = ctx->realpath;
267 /* remove the temporary mailbox */
268 static void remove_file (CONTEXT * ctx)
270 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
274 int mutt_open_append_compressed (CONTEXT * ctx)
277 compress_info *ci = set_compress_info (ctx);
278 char tmppath[_POSIX_PATH_MAX];
280 if (!get_append_command (ctx->path, ctx)) {
281 if (ci->open && ci->close)
282 return mutt_open_read_compressed (ctx);
285 p_delete(&ctx->cinfo);
289 /* Setup the right paths */
290 ctx->realpath = ctx->path;
292 /* Uncompress to /tmp */
293 fh = m_tempfile(tmppath, sizeof(tmppath), NONULL(Tempdir), NULL);
296 ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
298 ctx->magic = DefaultMagic;
300 if (is_new (ctx->realpath) ||
301 (ctx->magic != M_MBOX &&
302 ctx->magic != M_MMDF))
305 /* No error checking - the parent function will catch it */
310 /* close a compressed mailbox */
311 void mutt_fast_close_compressed (CONTEXT * ctx)
316 /* if the folder was removed, remove the gzipped folder too */
317 if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
318 remove (ctx->realpath);
323 p_delete(&ctx->cinfo);
327 /* return 0 on success, -1 on failure */
328 int mutt_sync_compressed (CONTEXT * ctx)
335 mutt_message (_("Compressing %s..."), ctx->realpath);
337 cmd = get_compression_cmd (ctx->cinfo->close, ctx);
341 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
342 mutt_perror (ctx->realpath);
346 mutt_block_signals ();
347 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
349 mutt_unblock_signals ();
350 mutt_error _("Unable to lock mailbox!");
352 ctx->cinfo->size = get_size(ctx->realpath);
360 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
361 mutt_system (echo_cmd);
362 if (mutt_system (cmd)) {
363 mutt_any_key_to_continue (NULL);
365 ("%s: Error compressing mailbox! Original mailbox deleted, uncompressed one kept!\n"),
370 mbox_unlock_compressed (ctx, fp);
371 mutt_unblock_signals ();
376 ctx->cinfo->size = get_size(ctx->realpath);
381 int mutt_slow_close_compressed (CONTEXT * ctx)
386 compress_info *ci = ctx->cinfo;
388 if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
389 || (append = ci->close)))) {
390 /* if we can not or should not append, we only have to remove the
391 compressed info, because sync was already called */
392 mutt_fast_close_compressed (ctx);
399 if (append == ci->close)
400 mutt_message (_("Compressing %s..."), ctx->realpath);
402 mutt_message (_("Compressed-appending to %s..."), ctx->realpath);
405 cmd = get_compression_cmd (append, ctx);
409 if ((fp = fopen (ctx->realpath, "a")) == NULL) {
410 mutt_perror (ctx->realpath);
414 mutt_block_signals ();
415 if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
417 mutt_unblock_signals ();
418 mutt_error _("Unable to lock mailbox!");
427 if (append == ci->close)
428 sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
430 sprintf (echo_cmd, _("echo Compressed-appending to %s..."),
432 mutt_system (echo_cmd);
434 if (mutt_system (cmd)) {
435 mutt_any_key_to_continue (NULL);
436 mutt_error (_(" %s: Error compressing mailbox! Uncompressed one kept!\n"),
439 mbox_unlock_compressed (ctx, fp);
440 mutt_unblock_signals ();
445 mbox_unlock_compressed (ctx, fp);
446 mutt_unblock_signals ();
451 p_delete(&ctx->cinfo);
456 mx_t const compress_mx = {
462 mutt_open_read_compressed,