/* Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
* Copyright (C) 2016 Richard Russon <rich@flatcap.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt.h"
#include "mailbox.h"
#include "mutt_curses.h"
#include "mx.h"
#include "compress.h"
/* Notes:
* Any references to compressed files also apply to encrypted files.
* ctx->path == plaintext file
* ctx->realpath == compressed file
*/
/**
* struct COMPRESS_INFO - Private data for compress
*
* This object gets attached to the mailbox's CONTEXT.
*/
typedef struct
{
const char *append; /* append-hook command */
const char *close; /* close-hook command */
const char *open; /* open-hook command */
off_t size; /* size of the compressed file */
struct mx_ops *child_ops; /* callbacks of de-compressed file */
int locked; /* if realpath is locked */
FILE *lockfp; /* fp used for locking */
} COMPRESS_INFO;
/**
* lock_realpath - Try to lock the ctx->realpath
* @ctx: Mailbox to lock
* @excl: Lock exclusively?
*
* Try to (exclusively) lock the mailbox. If we succeed, then we mark the
* mailbox as locked. If we fail, but we didn't want exclusive rights, then
* the mailbox will be marked readonly.
*
* Returns:
* 1: Success (locked or readonly)
* 0: Error (can't lock the file)
*/
static int
lock_realpath (CONTEXT *ctx, int excl)
{
if (!ctx)
return 0;
COMPRESS_INFO *ci = ctx->compress_info;
if (!ci)
return 0;
if (ci->locked)
return 1;
if (excl)
ci->lockfp = fopen (ctx->realpath, "a");
else
ci->lockfp = fopen (ctx->realpath, "r");
if (!ci->lockfp)
{
mutt_perror (ctx->realpath);
return 0;
}
int r = mx_lock_file (ctx->realpath, fileno (ci->lockfp), excl, 1, 1);
if (r == 0)
ci->locked = 1;
else if (excl == 0)
{
safe_fclose (&ci->lockfp);
ctx->readonly = 1;
return 1;
}
return (r == 0);
}
/**
* unlock_realpath - Unlock the ctx->realpath
* @ctx: Mailbox to unlock
*
* Unlock a mailbox previously locked by lock_mailbox().
*/
static void
unlock_realpath (CONTEXT *ctx)
{
if (!ctx)
return;
COMPRESS_INFO *ci = ctx->compress_info;
if (!ci)
return;
if (!ci->locked)
return;
mx_unlock_file (ctx->realpath, fileno (ci->lockfp), 1);
ci->locked = 0;
safe_fclose (&ci->lockfp);
}
/**
* setup_paths - Set the mailbox paths
* @ctx: Mailbox to modify
*
* Save the compressed filename in ctx->realpath.
* Create a temporary filename and put its name in ctx->path.
* The temporary file is created to prevent symlink attacks.
*
* Returns:
* 0: Success
* -1: Error
*/
static int
setup_paths (CONTEXT *ctx)
{
if (!ctx)
return -1;
char tmppath[_POSIX_PATH_MAX];
FILE *tmpfp;
/* Setup the right paths */
FREE(&ctx->realpath);
ctx->realpath = ctx->path;
/* We will uncompress to /tmp */
mutt_mktemp (tmppath, sizeof (tmppath));
ctx->path = safe_strdup (tmppath);
if ((tmpfp = safe_fopen (ctx->path, "w")) == NULL)
return -1;
safe_fclose (&tmpfp);
return 0;
}
/**
* get_size - Get the size of a file
* @path: File to measure
*
* Returns:
* number: Size in bytes
* 0: On error
*/
static int
get_size (const char *path)
{
if (!path)
return 0;
struct stat sb;
if (stat (path, &sb) != 0)
return 0;
return sb.st_size;
}
/**
* store_size - Save the size of the compressed file
* @ctx: Mailbox
*
* Save the compressed file size in the compress_info struct.
*/
static void
store_size (const CONTEXT *ctx)
{
if (!ctx)
return;
COMPRESS_INFO *ci = ctx->compress_info;
if (!ci)
return;
ci->size = get_size (ctx->realpath);
}
/**
* find_hook - Find a hook to match a path
* @type: Type of hook, e.g. MUTT_CLOSEHOOK
* @path: Filename to test
*
* Each hook has a type and a pattern.
* Find a command that matches the type and path supplied. e.g.
*
* User config:
* open-hook '\.gz$' "gzip -cd '%f' > '%t'"
*
* Call:
* find_hook (MUTT_OPENHOOK, "myfile.gz");
*
* Returns:
* string: Matching hook command
* NULL: No matches
*/
static const char *
find_hook (int