NeoMutt  2025-12-11-694-ga89709
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 else
140 {
142 }
143
144 return r == 0;
145}
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition file.c:1088
#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 153 of file compress.c.

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

180{
181 if (!m)
182 return -1;
183
184 /* Setup the right paths */
186
187 /* We will uncompress to TMPDIR */
188 struct Buffer *buf = buf_pool_get();
189 buf_mktemp(buf);
190 buf_copy(&m->pathbuf, buf);
191 buf_pool_release(&buf);
192
193 return mutt_file_touch(mailbox_path(m)) ? 0 : -1;
194}
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:974
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 202 of file compress.c.

203{
204 if (!m || !m->compress_info)
205 return;
206
207 struct CompressInfo *ci = m->compress_info;
208
210}
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition file.c:1414
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 217 of file compress.c.

218{
219 struct Buffer *err = buf_pool_get();
220
221 struct Expando *exp = expando_parse(s, CompressFormatDef, err);
222 if (!exp)
223 {
224 mutt_error(_("Expando parse error: %s"), buf_string(err));
225 }
226
227 buf_pool_release(&err);
228 return exp;
229}
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 239 of file compress.c.

240{
241 if (!m)
242 return NULL;
243
244 if (m->compress_info)
245 return m->compress_info;
246
247 /* Open is compulsory */
248 const char *o = mutt_find_hook(CMD_OPEN_HOOK, mailbox_path(m));
249 if (!o)
250 return NULL;
251
252 const char *c = mutt_find_hook(CMD_CLOSE_HOOK, mailbox_path(m));
253 const char *a = mutt_find_hook(CMD_APPEND_HOOK, mailbox_path(m));
254
255 struct CompressInfo *ci = MUTT_MEM_CALLOC(1, struct CompressInfo);
256 m->compress_info = ci;
257
259 ci->cmd_close = c ? validate_compress_expando(c) : NULL;
260 ci->cmd_append = a ? validate_compress_expando(a) : NULL;
261
262 return ci;
263}
@ 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:217
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 269 of file compress.c.

270{
271 if (!m || !m->compress_info)
272 return;
273
274 struct CompressInfo *ci = m->compress_info;
278
280
281 FREE(&m->compress_info);
282}
static void unlock_realpath(struct Mailbox *m)
Unlock the mailbox->realpath.
Definition compress.c:153
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 295 of file compress.c.

296{
297 if (!m || !exp || !progress)
298 return false;
299
300 if (m->verbose)
301 mutt_message(progress, m->realpath);
302
303 bool rc = true;
304 struct Buffer *sys_cmd = buf_pool_get();
305 buf_alloc(sys_cmd, STR_COMMAND);
306
308 mutt_endwin();
309 fflush(stdout);
310
312 sys_cmd->dsize, sys_cmd);
313
314 if (mutt_system(buf_string(sys_cmd)) != 0)
315 {
316 rc = false;
318 mutt_error(_("Error running \"%s\""), buf_string(sys_cmd));
319 }
320
322
323 buf_pool_release(&sys_cmd);
324 return rc;
325}
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 338 of file compress.c.

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

368{
369 if (!path)
370 return false;
371
372 if (mutt_find_hook(CMD_OPEN_HOOK, path))
373 return true;
374
375 return false;
376}
+ 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 387 of file compress.c.

388{
389 if (!cmd)
390 return 0;
391
392 return strstr(cmd, "%f") && strstr(cmd, "%t");
393}
+ 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:914
#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};