NeoMutt  2023-03-22-27-g3cb248
Teaching an old dog new tricks
DOXYGEN
compress.c File Reference

Compressed mbox local mailbox type. More...

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "format_flags.h"
#include "globals.h"
#include "hook.h"
#include "muttlib.h"
#include "mx.h"
#include "protos.h"
+ Include dependency graph for compress.c:

Go to the source code of this file.

Functions

void mutt_comp_init (void)
 Setup feature commands. More...
 
static bool lock_realpath (struct Mailbox *m, bool excl)
 Try to lock the Mailbox.realpath. More...
 
static void unlock_realpath (struct Mailbox *m)
 Unlock the mailbox->realpath. More...
 
static int setup_paths (struct Mailbox *m)
 Set the mailbox paths. More...
 
static void store_size (const struct Mailbox *m)
 Save the size of the compressed file. More...
 
static struct CompressInfoset_compress_info (struct Mailbox *m)
 Find the compress hooks for a mailbox. More...
 
static void compress_info_free (struct Mailbox *m)
 Frees the compress info members and structure. More...
 
static const char * compress_format_str (char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
 Expand the filenames in a command string - Implements format_t -. More...
 
static void expand_command_str (const struct Mailbox *m, const char *cmd, char *buf, int buflen)
 Expand placeholders in command string. More...
 
static int execute_command (struct Mailbox *m, const char *command, const char *progress)
 Run a system command. More...
 
bool mutt_comp_can_append (struct Mailbox *m)
 Can we append to this path? More...
 
bool mutt_comp_can_read (const char *path)
 Can we read from this file? More...
 
int mutt_comp_valid_command (const char *cmd)
 Is this command string allowed? More...
 
static bool comp_ac_owns_path (struct Account *a, const char *path)
 Check whether an Account owns a Mailbox path - Implements MxOps::ac_owns_path() -. More...
 
static bool comp_ac_add (struct Account *a, struct Mailbox *m)
 Add a Mailbox to an Account - Implements MxOps::ac_add() -. More...
 
static enum MxOpenReturns comp_mbox_open (struct Mailbox *m)
 Open a Mailbox - Implements MxOps::mbox_open() -. More...
 
static bool comp_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static enum MxStatus comp_mbox_check (struct Mailbox *m)
 Check for new mail - Implements MxOps::mbox_check() -. More...
 
static enum MxStatus comp_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
static enum MxStatus comp_mbox_close (struct Mailbox *m)
 Close a Mailbox - Implements MxOps::mbox_close() -. More...
 
static bool comp_msg_open (struct Mailbox *m, struct Message *msg, int msgno)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -. More...
 
static bool comp_msg_open_new (struct Mailbox *m, struct Message *msg, const struct Email *e)
 Open a new message in a Mailbox - Implements MxOps::msg_open_new() -. More...
 
static int comp_msg_commit (struct Mailbox *m, struct Message *msg)
 Save changes to an email - Implements MxOps::msg_commit() -. More...
 
static int comp_msg_close (struct Mailbox *m, struct Message *msg)
 Close an email - Implements MxOps::msg_close() -. More...
 
static int comp_msg_padding_size (struct Mailbox *m)
 Bytes of padding between messages - Implements MxOps::msg_padding_size() -. More...
 
static int comp_msg_save_hcache (struct Mailbox *m, struct Email *e)
 Save message to the header cache - Implements MxOps::msg_save_hcache() -. More...
 
static int comp_tags_edit (struct Mailbox *m, const char *tags, struct Buffer *buf)
 Prompt and validate new messages tags - Implements MxOps::tags_edit() -. More...
 
static int comp_tags_commit (struct Mailbox *m, struct Email *e, const char *buf)
 Save the tags to a message - Implements MxOps::tags_commit() -. More...
 
static enum MailboxType comp_path_probe (const char *path, const struct stat *st)
 Is this a compressed Mailbox? - Implements MxOps::path_probe() -. More...
 
static int comp_path_canon (char *buf, size_t buflen)
 Canonicalise a Mailbox path - Implements MxOps::path_canon() -. More...
 
static int comp_path_pretty (char *buf, size_t buflen, const char *folder)
 Abbreviate a Mailbox path - Implements MxOps::path_pretty() -. More...
 
static int comp_path_parent (char *buf, size_t buflen)
 Find the parent of a Mailbox path - Implements MxOps::path_parent() -. More...
 

Variables

static const struct Command CompCommands []
 Compression Commands. More...
 
struct MxOps MxCompOps
 Compressed Mailbox - Implements MxOps -. More...
 

Detailed Description

Compressed mbox local mailbox type.

Authors
  • Alain Penders
  • Richard Russon

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, see http://www.gnu.org/licenses/.

Definition in file compress.c.

Function Documentation

◆ mutt_comp_init()

void mutt_comp_init ( void  )

Setup feature commands.

Definition at line 72 of file compress.c.

73{
75}
static const struct Command CompCommands[]
Compression Commands.
Definition: compress.c:61
void commands_register(const struct Command *cmds, const size_t num_cmds)
Add commands to Commands array.
Definition: command.c:53
#define mutt_array_size(x)
Definition: memory.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lock_realpath()

static bool lock_realpath ( struct Mailbox m,
bool  excl 
)
static

Try to lock the Mailbox.realpath.

Parameters
mMailbox to lock
exclLock exclusively?
Return values
trueSuccess (locked or readonly)
falseError (can't lock the file)

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.

Definition at line 88 of file compress.c.

89{
90 if (!m || !m->compress_info)
91 return false;
92
93 struct CompressInfo *ci = m->compress_info;
94
95 if (ci->locked)
96 return true;
97
98 if (excl)
99 ci->fp_lock = fopen(m->realpath, "a");
100 else
101 ci->fp_lock = fopen(m->realpath, "r");
102 if (!ci->fp_lock)
103 {
105 return false;
106 }
107
108 int r = mutt_file_lock(fileno(ci->fp_lock), excl, true);
109 if (r == 0)
110 {
111 ci->locked = true;
112 }
113 else if (excl)
114 {
116 m->readonly = true;
117 return true;
118 }
119
120 return r == 0;
121}
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:149
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1218
#define mutt_perror(...)
Definition: logging.h:88
Private data for compress.
Definition: lib.h:47
FILE * fp_lock
fp used for locking
Definition: lib.h:54
bool locked
if realpath is locked
Definition: lib.h:53
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:81
void * compress_info
Compressed mbox module private data.
Definition: mailbox.h:120
bool readonly
Don't allow changes to the mailbox.
Definition: mailbox.h:115
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unlock_realpath()

static void unlock_realpath ( struct Mailbox m)
static

Unlock the mailbox->realpath.

Parameters
mMailbox to unlock

Unlock a mailbox previously locked by lock_mailbox().

Definition at line 129 of file compress.c.

130{
131 if (!m || !m->compress_info)
132 return;
133
134 struct CompressInfo *ci = m->compress_info;
135
136 if (!ci->locked)
137 return;
138
139 mutt_file_unlock(fileno(ci->fp_lock));
140
141 ci->locked = false;
143}
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1266
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setup_paths()

static int setup_paths ( struct Mailbox m)
static

Set the mailbox paths.

Parameters
mMailbox to modify
Return values
0Success
-1Error

Save the compressed filename in mailbox->realpath. Create a temporary filename and put its name in mailbox->path. The temporary file is created to prevent symlink attacks.

Definition at line 155 of file compress.c.

156{
157 if (!m)
158 return -1;
159
160 /* Setup the right paths */
162
163 /* We will uncompress to TMPDIR */
164 struct Buffer *buf = mutt_buffer_pool_get();
166 mutt_buffer_copy(&m->pathbuf, buf);
168
169 FILE *fp = mutt_file_fopen(mailbox_path(m), "w");
170 if (!fp)
171 return -1;
172
173 mutt_file_fclose(&fp);
174 return 0;
175}
size_t mutt_buffer_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:500
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:209
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
String manipulation buffer.
Definition: buffer.h:34
struct Buffer pathbuf
Path of the Mailbox.
Definition: mailbox.h:80
#define mutt_buffer_mktemp(buf)
Definition: tmp.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ store_size()

static void store_size ( const struct Mailbox m)
static

Save the size of the compressed file.

Parameters
mMailbox

Save the compressed file size in the compress_info struct.

Definition at line 183 of file compress.c.

184{
185 if (!m || !m->compress_info)
186 return;
187
188 struct CompressInfo *ci = m->compress_info;
189
191}
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1538
long size
size of the compressed file
Definition: lib.h:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_compress_info()

static struct CompressInfo * set_compress_info ( struct Mailbox m)
static

Find the compress hooks for a mailbox.

Parameters
mMailbox to examine
Return values
ptrCompressInfo Hook info for the mailbox's path
NULLError

When a mailbox is opened, we check if there are any matching hooks.

Definition at line 201 of file compress.c.

202{
203 if (!m)
204 return NULL;
205
206 if (m->compress_info)
207 return m->compress_info;
208
209 /* Open is compulsory */
210 const char *o = mutt_find_hook(MUTT_OPEN_HOOK, mailbox_path(m));
211 if (!o)
212 return NULL;
213
214 const char *c = mutt_find_hook(MUTT_CLOSE_HOOK, mailbox_path(m));
215 const char *a = mutt_find_hook(MUTT_APPEND_HOOK, mailbox_path(m));
216
217 struct CompressInfo *ci = mutt_mem_calloc(1, sizeof(struct CompressInfo));
218 m->compress_info = ci;
219
220 ci->cmd_open = mutt_str_dup(o);
221 ci->cmd_close = mutt_str_dup(c);
222 ci->cmd_append = mutt_str_dup(a);
223
224 return ci;
225}
char * mutt_find_hook(HookFlags type, const char *pat)
Find a matching hook.
Definition: hook.c:635
#define MUTT_OPEN_HOOK
open-hook: to read a compressed mailbox
Definition: hook.h:51
#define MUTT_CLOSE_HOOK
close-hook: write to a compressed mailbox
Definition: hook.h:53
#define MUTT_APPEND_HOOK
append-hook: append to a compressed mailbox
Definition: hook.h:52
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
const char * cmd_append
append-hook command
Definition: lib.h:48
const char * cmd_open
open-hook command
Definition: lib.h:50
const char * cmd_close
close-hook command
Definition: lib.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compress_info_free()

static void compress_info_free ( struct Mailbox m)
static

Frees the compress info members and structure.

Parameters
mMailbox to free compress_info for

Definition at line 231 of file compress.c.

232{
233 if (!m || !m->compress_info)
234 return;
235
236 struct CompressInfo *ci = m->compress_info;
237 FREE(&ci->cmd_open);
238 FREE(&ci->cmd_close);
239 FREE(&ci->cmd_append);
240
242
243 FREE(&m->compress_info);
244}
static void unlock_realpath(struct Mailbox *m)
Unlock the mailbox->realpath.
Definition: compress.c:129
#define FREE(x)
Definition: memory.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ expand_command_str()

static void expand_command_str ( const struct Mailbox m,
const char *  cmd,
char *  buf,
int  buflen 
)
static

Expand placeholders in command string.

Parameters
mMailbox for paths
cmdTemplate command to be expanded
bufBuffer to store the command
buflenSize of the buffer

This function takes a hook command and expands the filename placeholders within it. The function calls mutt_expando_format() to do the replacement which calls our callback function compress_format_str(). e.g.

Template command: gzip -cd 'f' > 't'

Result: gzip -dc '~/mail/abc.gz' > '/tmp/xyz'

See also
compress_format_str()

Definition at line 306 of file compress.c.

307{
308 if (!m || !cmd || !buf)
309 return;
310
311 mutt_expando_format(buf, buflen, 0, buflen, cmd, compress_format_str,
312 (intptr_t) m, MUTT_FORMAT_NO_FLAGS);
313}
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
static const char * compress_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Expand the filenames in a command string - Implements format_t -.
Definition: compress.c:254
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:726
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ execute_command()

static int execute_command ( struct Mailbox m,
const char *  command,
const char *  progress 
)
static

Run a system command.

Parameters
mMailbox to work with
commandCommand string to execute
progressMessage to show the user
Return values
1Success
0Failure

Run the supplied command, taking care of all the NeoMutt requirements, such as locking files and blocking signals.

Definition at line 326 of file compress.c.

327{
328 if (!m || !command || !progress)
329 return 0;
330
331 if (m->verbose)
332 mutt_message(progress, m->realpath);
333
334 int rc = 1;
335 char sys_cmd[STR_COMMAND] = { 0 };
336
338 endwin();
339 fflush(stdout);
340
341 expand_command_str(m, command, sys_cmd, sizeof(sys_cmd));
342
343 if (mutt_system(sys_cmd) != 0)
344 {
345 rc = 0;
347 mutt_error(_("Error running \"%s\""), sys_cmd);
348 }
349
351
352 return rc;
353}
static void expand_command_str(const struct Mailbox *m, const char *cmd, char *buf, int buflen)
Expand placeholders in command string.
Definition: compress.c:306
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:386
#define mutt_error(...)
Definition: logging.h:87
#define mutt_message(...)
Definition: logging.h:86
#define _(a)
Definition: message.h:28
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:51
void mutt_sig_block(void)
Block signals during critical operations.
Definition: signal.c:150
void mutt_sig_unblock(void)
Restore previously blocked signals.
Definition: signal.c:168
#define STR_COMMAND
Enough space for a long command line.
Definition: string2.h:35
bool verbose
Display status messages?
Definition: mailbox.h:114
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_comp_can_append()

bool mutt_comp_can_append ( struct Mailbox m)

Can we append to this path?

Parameters
mMailbox
Return values
trueYes, we can append to the file
falseNo, appending isn't possible

To append to a file we can either use an 'append-hook' or a combination of 'open-hook' and 'close-hook'.

A match means it's our responsibility to append to the file.

Definition at line 366 of file compress.c.

367{
368 if (!m)
369 return false;
370
371 /* If this succeeds, we know there's an open-hook */
372 struct CompressInfo *ci = set_compress_info(m);
373 if (!ci)
374 return false;
375
376 /* We have an open-hook, so to append we need an append-hook,
377 * or a close-hook. */
378 if (ci->cmd_append || ci->cmd_close)
379 return true;
380
381 mutt_error(_("Can't append without an append-hook or close-hook : %s"), mailbox_path(m));
382 return false;
383}
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition: compress.c:201
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_comp_can_read()

bool mutt_comp_can_read ( const char *  path)

Can we read from this file?

Parameters
pathPathname of file to be tested
Return values
trueYes, we can read the file
falseNo, we can't read the file

Search for an 'open-hook' with a regex that matches the path.

A match means it's our responsibility to open the file.

Definition at line 395 of file compress.c.

396{
397 if (!path)
398 return false;
399
401 return true;
402
403 return false;
404}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_comp_valid_command()

int mutt_comp_valid_command ( const char *  cmd)

Is this command string allowed?

Parameters
cmdCommand string
Return values
1Valid command
0"%f" and/or "%t" is missing

A valid command string must have both "%f" (from file) and "%t" (to file). We don't check if we can actually run the command.

Definition at line 415 of file compress.c.

416{
417 if (!cmd)
418 return 0;
419
420 return strstr(cmd, "%f") && strstr(cmd, "%t");
421}
+ Here is the caller graph for this function:

Variable Documentation

◆ CompCommands

const struct Command CompCommands[]
static
Initial value:
= {
{ "append-hook", mutt_parse_hook, MUTT_APPEND_HOOK },
{ "close-hook", mutt_parse_hook, MUTT_CLOSE_HOOK },
{ "open-hook", mutt_parse_hook, MUTT_OPEN_HOOK },
}
enum CommandResult mutt_parse_hook(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'hook' family of commands - Implements Command::parse() -.
Definition: hook.c:126

Compression Commands.

Definition at line 61 of file compress.c.