NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
compress.c File Reference

Compressed mbox local mailbox type. More...

#include "config.h"
#include <errno.h>
#include <stdbool.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 "mutt.h"
#include "lib.h"
#include "expando/lib.h"
#include "hooks/lib.h"
#include "expando.h"
#include "mx.h"
+ Include dependency graph for compress.c:

Go to the source code of this file.

Functions

static bool lock_realpath (struct Mailbox *m, bool excl)
 Try to lock the Mailbox.realpath.
 
static void unlock_realpath (struct Mailbox *m)
 Unlock the mailbox->realpath.
 
static int setup_paths (struct Mailbox *m)
 Set the mailbox paths.
 
static void store_size (const struct Mailbox *m)
 Save the size of the compressed file.
 
static struct Expandovalidate_compress_expando (const char *s)
 Validate the Compress hooks.
 
static struct CompressInfoset_compress_info (struct Mailbox *m)
 Find the compress hooks for a mailbox.
 
static void compress_info_free (struct Mailbox *m)
 Frees the compress info members and structure.
 
static bool execute_command (struct Mailbox *m, const struct Expando *exp, const char *progress)
 Run a system command.
 
bool mutt_comp_can_append (struct Mailbox *m)
 Can we append to this path?
 
bool mutt_comp_can_read (const char *path)
 Can we read from this file?
 
int mutt_comp_valid_command (const char *cmd)
 Is this command string allowed?
 
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() -.
 
static bool comp_ac_add (struct Account *a, struct Mailbox *m)
 Add a Mailbox to an Account - Implements MxOps::ac_add() -.
 
static enum MxOpenReturns comp_mbox_open (struct Mailbox *m)
 Open a Mailbox - Implements MxOps::mbox_open() -.
 
static bool comp_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
 
static enum MxStatus comp_mbox_check (struct Mailbox *m)
 Check for new mail - Implements MxOps::mbox_check() -.
 
static enum MxStatus comp_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
 
static enum MxStatus comp_mbox_close (struct Mailbox *m)
 Close a Mailbox - Implements MxOps::mbox_close() -.
 
static bool comp_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
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() -.
 
static int comp_msg_commit (struct Mailbox *m, struct Message *msg)
 Save changes to an email - Implements MxOps::msg_commit() -.
 
static int comp_msg_close (struct Mailbox *m, struct Message *msg)
 Close an email - Implements MxOps::msg_close() -.
 
static int comp_msg_padding_size (struct Mailbox *m)
 Bytes of padding between messages - Implements MxOps::msg_padding_size() -.
 
static int comp_msg_save_hcache (struct Mailbox *m, struct Email *e)
 Save message to the header cache - Implements MxOps::msg_save_hcache() -.
 
static int comp_tags_edit (struct Mailbox *m, const char *tags, struct Buffer *buf)
 Prompt and validate new messages tags - Implements MxOps::tags_edit() -.
 
static int comp_tags_commit (struct Mailbox *m, struct Email *e, const char *buf)
 Save the tags to a message - Implements MxOps::tags_commit() -.
 
static enum MailboxType comp_path_probe (const char *path, const struct stat *st)
 Is this a compressed Mailbox?
 
static int comp_path_canon (struct Buffer *path)
 Canonicalise a Mailbox path - Implements MxOps::path_canon() -.
 

Variables

const struct Command CompCommands []
 Compression Commands.
 
const struct ExpandoDefinition CompressFormatDef []
 Expando definitions.
 
const struct MxOps MxCompOps
 Compressed Mailbox - Implements MxOps -.
 

Detailed Description

Compressed mbox local mailbox type.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Reto Brunner
  • Tóth János

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

◆ 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 108 of file compress.c.

109{
110 if (!m || !m->compress_info)
111 return false;
112
113 struct CompressInfo *ci = m->compress_info;
114
115 if (ci->locked)
116 return true;
117
118 if (excl)
119 ci->fp_lock = mutt_file_fopen(m->realpath, "a");
120 else
121 ci->fp_lock = mutt_file_fopen(m->realpath, "r");
122 if (!ci->fp_lock)
123 {
124 mutt_perror("%s", m->realpath);
125 return false;
126 }
127
128 int r = mutt_file_lock(fileno(ci->fp_lock), excl, true);
129 if (r == 0)
130 {
131 ci->locked = true;
132 }
133 else if (excl)
134 {
136 m->readonly = true;
137 return true;
138 }
139
140 return r == 0;
141}
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition file.c:1092
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
#define mutt_perror(...)
Definition logging2.h:95
Private data for compress.
Definition lib.h:61
FILE * fp_lock
fp used for locking
Definition lib.h:68
bool locked
if realpath is locked
Definition lib.h:67
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition mailbox.h:80
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 149 of file compress.c.

150{
151 if (!m || !m->compress_info)
152 return;
153
154 struct CompressInfo *ci = m->compress_info;
155
156 if (!ci->locked)
157 return;
158
159 mutt_file_unlock(fileno(ci->fp_lock));
160
161 ci->locked = false;
163}
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition file.c:1139
+ 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 175 of file compress.c.

176{
177 if (!m)
178 return -1;
179
180 /* Setup the right paths */
182
183 /* We will uncompress to TMPDIR */
184 struct Buffer *buf = buf_pool_get();
185 buf_mktemp(buf);
186 buf_copy(&m->pathbuf, buf);
187 buf_pool_release(&buf);
188
189 return mutt_file_touch(mailbox_path(m)) ? 0 : -1;
190}
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition buffer.c:601
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:213
bool mutt_file_touch(const char *path)
Make sure a file exists.
Definition file.c:978
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:36
struct Buffer pathbuf
Path of the Mailbox.
Definition mailbox.h:79
#define buf_mktemp(buf)
Definition tmp.h:33
+ 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 198 of file compress.c.

199{
200 if (!m || !m->compress_info)
201 return;
202
203 struct CompressInfo *ci = m->compress_info;
204
206}
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition file.c:1409
long size
size of the compressed file
Definition lib.h:65
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validate_compress_expando()

static struct Expando * validate_compress_expando ( const char * s)
static

Validate the Compress hooks.

Parameters
sCommand string
Return values
ptrExpando

Definition at line 213 of file compress.c.

214{
215 struct Buffer *err = buf_pool_get();
216
217 struct Expando *exp = expando_parse(s, CompressFormatDef, err);
218 if (!exp)
219 {
220 mutt_error(_("Expando parse error: %s"), buf_string(err));
221 }
222
223 buf_pool_release(&err);
224 return exp;
225}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const struct ExpandoDefinition CompressFormatDef[]
Expando definitions.
Definition compress.c:89
struct Expando * expando_parse(const char *str, const struct ExpandoDefinition *defs, struct Buffer *err)
Parse an Expando string.
Definition expando.c:81
#define mutt_error(...)
Definition logging2.h:94
#define _(a)
Definition message.h:28
Parsed Expando trees.
Definition expando.h:41
+ 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 235 of file compress.c.

236{
237 if (!m)
238 return NULL;
239
240 if (m->compress_info)
241 return m->compress_info;
242
243 /* Open is compulsory */
244 const char *o = mutt_find_hook(CMD_OPEN_HOOK, mailbox_path(m));
245 if (!o)
246 return NULL;
247
248 const char *c = mutt_find_hook(CMD_CLOSE_HOOK, mailbox_path(m));
249 const char *a = mutt_find_hook(CMD_APPEND_HOOK, mailbox_path(m));
250
251 struct CompressInfo *ci = MUTT_MEM_CALLOC(1, struct CompressInfo);
252 m->compress_info = ci;
253
257
258 return ci;
259}
@ CMD_CLOSE_HOOK
:close-hook
Definition command.h:70
@ CMD_OPEN_HOOK
:open-hook
Definition command.h:100
@ CMD_APPEND_HOOK
:append-hook
Definition command.h:64
static struct Expando * validate_compress_expando(const char *s)
Validate the Compress hooks.
Definition compress.c:213
char * mutt_find_hook(enum CommandId id, const char *pat)
Find a matching hook.
Definition exec.c:114
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
struct Expando * cmd_open
open-hook command
Definition lib.h:64
struct Expando * cmd_append
append-hook command
Definition lib.h:62
struct Expando * cmd_close
close-hook command
Definition lib.h:63
+ 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 265 of file compress.c.

266{
267 if (!m || !m->compress_info)
268 return;
269
270 struct CompressInfo *ci = m->compress_info;
274
276
277 FREE(&m->compress_info);
278}
static void unlock_realpath(struct Mailbox *m)
Unlock the mailbox->realpath.
Definition compress.c:149
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition expando.c:61
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ execute_command()

static bool execute_command ( struct Mailbox * m,
const struct Expando * exp,
const char * progress )
static

Run a system command.

Parameters
mMailbox to work with
expCommand expando to execute
progressMessage to show the user
Return values
trueSuccess
falseFailure

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

Definition at line 291 of file compress.c.

292{
293 if (!m || !exp || !progress)
294 return false;
295
296 if (m->verbose)
297 mutt_message(progress, m->realpath);
298
299 bool rc = true;
300 struct Buffer *sys_cmd = buf_pool_get();
301 buf_alloc(sys_cmd, STR_COMMAND);
302
304 mutt_endwin();
305 fflush(stdout);
306
308 sys_cmd->dsize, sys_cmd);
309
310 if (mutt_system(buf_string(sys_cmd)) != 0)
311 {
312 rc = false;
314 mutt_error(_("Error running \"%s\""), buf_string(sys_cmd));
315 }
316
318
319 buf_pool_release(&sys_cmd);
320 return rc;
321}
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition buffer.c:337
const struct ExpandoRenderCallback CompressRenderCallbacks[]
Callbacks for Compression Hook Expandos.
Definition expando.c:64
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition curs_lib.c:173
void mutt_endwin(void)
Shutdown curses.
Definition curs_lib.c:151
int expando_render(const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando + data into a string.
Definition expando.c:118
#define mutt_message(...)
Definition logging2.h:93
int mutt_system(const char *cmd)
Run an external command.
Definition system.c:51
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition render.h:33
void mutt_sig_block(void)
Block signals during critical operations.
Definition signal.c:227
void mutt_sig_unblock(void)
Restore previously blocked signals.
Definition signal.c:245
#define STR_COMMAND
Enough space for a long command line.
Definition string2.h:42
size_t dsize
Length of data.
Definition buffer.h:39
bool verbose
Display status messages?
Definition mailbox.h:116
+ 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 334 of file compress.c.

335{
336 if (!m)
337 return false;
338
339 /* If this succeeds, we know there's an open-hook */
340 struct CompressInfo *ci = set_compress_info(m);
341 if (!ci)
342 return false;
343
344 /* We have an open-hook, so to append we need an append-hook,
345 * or a close-hook. */
346 if (ci->cmd_append || ci->cmd_close)
347 return true;
348
349 mutt_error(_("Can't append without an append-hook or close-hook : %s"), mailbox_path(m));
350 return false;
351}
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition compress.c:235
+ 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 363 of file compress.c.

364{
365 if (!path)
366 return false;
367
368 if (mutt_find_hook(CMD_OPEN_HOOK, path))
369 return true;
370
371 return false;
372}
+ 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 383 of file compress.c.

384{
385 if (!cmd)
386 return 0;
387
388 return strstr(cmd, "%f") && strstr(cmd, "%t");
389}
+ Here is the caller graph for this function:

Variable Documentation

◆ CompCommands

const struct Command CompCommands[]
Initial value:
= {
N_("Define command to append to a compressed mailbox"),
N_("append-hook <regex> <shell-command>"),
"optionalfeatures.html#append-hook" },
N_("Define command to close a compressed mailbox"),
N_("close-hook <regex> <shell-command>"),
"optionalfeatures.html#close-hook" },
N_("Define command to open a compressed mailbox"),
N_("open-hook <regex> <shell-command>"),
"optionalfeatures.html#open-hook" },
{ NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NO_FLAGS },
}
#define CF_NO_FLAGS
No flags are set.
Definition command.h:48
@ CMD_NONE
No Command.
Definition command.h:59
enum CommandResult parse_compress_hook(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse compress hook commands - Implements Command::parse() -.
Definition parse.c:910
#define N_(a)
Definition message.h:32

Compression Commands.

Definition at line 62 of file compress.c.

62 {
63 // clang-format off
64 { "append-hook", CMD_APPEND_HOOK, parse_compress_hook,
65 N_("Define command to append to a compressed mailbox"),
66 N_("append-hook <regex> <shell-command>"),
67 "optionalfeatures.html#append-hook" },
68 { "close-hook", CMD_CLOSE_HOOK, parse_compress_hook,
69 N_("Define command to close a compressed mailbox"),
70 N_("close-hook <regex> <shell-command>"),
71 "optionalfeatures.html#close-hook" },
72 { "open-hook", CMD_OPEN_HOOK, parse_compress_hook,
73 N_("Define command to open a compressed mailbox"),
74 N_("open-hook <regex> <shell-command>"),
75 "optionalfeatures.html#open-hook" },
76
77 { NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NO_FLAGS },
78 // clang-format on
79};

◆ CompressFormatDef

const struct ExpandoDefinition CompressFormatDef[]
Initial value:
= {
{ "f", "from", ED_COMPRESS, ED_CMP_FROM, NULL },
{ "t", "to", ED_COMPRESS, ED_CMP_TO, NULL },
{ NULL, NULL, 0, -1, NULL }
}
@ ED_CMP_FROM
'from' path
Definition lib.h:51
@ ED_CMP_TO
'to' path
Definition lib.h:52
@ ED_COMPRESS
Compress ED_CMP_ ExpandoDataCompress.
Definition domain.h:40

Expando definitions.

Config:

  • append-hook
  • close-hook
  • open-hook

Definition at line 89 of file compress.c.

89 {
90 // clang-format off
91 { "f", "from", ED_COMPRESS, ED_CMP_FROM, NULL },
92 { "t", "to", ED_COMPRESS, ED_CMP_TO, NULL },
93 { NULL, NULL, 0, -1, NULL }
94 // clang-format on
95};