NeoMutt  2024-12-12-14-g7b49f7
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 "lib.h"
#include "expando/lib.h"
#include "expando.h"
#include "globals.h"
#include "hook.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.
 
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? - Implements MxOps::path_probe() -.
 
static int comp_path_canon (struct Buffer *path)
 Canonicalise a Mailbox path - Implements MxOps::path_canon() -.
 

Variables

static 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

◆ mutt_comp_init()

void mutt_comp_init ( void  )

Setup feature commands.

Definition at line 90 of file compress.c.

91{
93}
static const struct Command CompCommands[]
Compression Commands.
Definition: compress.c:63
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:38
+ 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 106 of file compress.c.

107{
108 if (!m || !m->compress_info)
109 return false;
110
111 struct CompressInfo *ci = m->compress_info;
112
113 if (ci->locked)
114 return true;
115
116 if (excl)
117 ci->fp_lock = mutt_file_fopen(m->realpath, "a");
118 else
119 ci->fp_lock = mutt_file_fopen(m->realpath, "r");
120 if (!ci->fp_lock)
121 {
122 mutt_perror("%s", m->realpath);
123 return false;
124 }
125
126 int r = mutt_file_lock(fileno(ci->fp_lock), excl, true);
127 if (r == 0)
128 {
129 ci->locked = true;
130 }
131 else if (excl)
132 {
134 m->readonly = true;
135 return true;
136 }
137
138 return r == 0;
139}
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1202
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
#define mutt_perror(...)
Definition: logging2.h:93
Private data for compress.
Definition: lib.h:60
FILE * fp_lock
fp used for locking
Definition: lib.h:67
bool locked
if realpath is locked
Definition: lib.h:66
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:121
bool readonly
Don't allow changes to the mailbox.
Definition: mailbox.h:116
+ 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 147 of file compress.c.

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

174{
175 if (!m)
176 return -1;
177
178 /* Setup the right paths */
180
181 /* We will uncompress to TMPDIR */
182 struct Buffer *buf = buf_pool_get();
183 buf_mktemp(buf);
184 buf_copy(&m->pathbuf, buf);
185 buf_pool_release(&buf);
186
187 FILE *fp = mutt_file_fopen(mailbox_path(m), "w");
188 if (!fp)
189 return -1;
190
191 mutt_file_fclose(&fp);
192 return 0;
193}
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:223
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
String manipulation buffer.
Definition: buffer.h:36
struct Buffer pathbuf
Path of the Mailbox.
Definition: mailbox.h:80
#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 201 of file compress.c.

202{
203 if (!m || !m->compress_info)
204 return;
205
206 struct CompressInfo *ci = m->compress_info;
207
209}
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1519
long size
size of the compressed file
Definition: lib.h:64
+ 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 216 of file compress.c.

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

239{
240 if (!m)
241 return NULL;
242
243 if (m->compress_info)
244 return m->compress_info;
245
246 /* Open is compulsory */
247 const char *o = mutt_find_hook(MUTT_OPEN_HOOK, mailbox_path(m));
248 if (!o)
249 return NULL;
250
251 const char *c = mutt_find_hook(MUTT_CLOSE_HOOK, mailbox_path(m));
252 const char *a = mutt_find_hook(MUTT_APPEND_HOOK, mailbox_path(m));
253
254 struct CompressInfo *ci = MUTT_MEM_CALLOC(1, struct CompressInfo);
255 m->compress_info = ci;
256
260
261 return ci;
262}
static struct Expando * validate_compress_expando(const char *s)
Validate the Compress hooks.
Definition: compress.c:216
char * mutt_find_hook(HookFlags type, const char *pat)
Find a matching hook.
Definition: hook.c:678
#define MUTT_OPEN_HOOK
open-hook: to read a compressed mailbox
Definition: hook.h:49
#define MUTT_CLOSE_HOOK
close-hook: write to a compressed mailbox
Definition: hook.h:51
#define MUTT_APPEND_HOOK
append-hook: append to a compressed mailbox
Definition: hook.h:50
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
struct Expando * cmd_open
open-hook command
Definition: lib.h:63
struct Expando * cmd_append
append-hook command
Definition: lib.h:61
struct Expando * cmd_close
close-hook command
Definition: lib.h:62
+ 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 268 of file compress.c.

269{
270 if (!m || !m->compress_info)
271 return;
272
273 struct CompressInfo *ci = m->compress_info;
277
279
280 FREE(&m->compress_info);
281}
static void unlock_realpath(struct Mailbox *m)
Unlock the mailbox->realpath.
Definition: compress.c:147
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition: expando.c:61
#define FREE(x)
Definition: memory.h:55
+ 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 294 of file compress.c.

295{
296 if (!m || !exp || !progress)
297 return false;
298
299 if (m->verbose)
300 mutt_message(progress, m->realpath);
301
302 bool rc = true;
303 struct Buffer *sys_cmd = buf_pool_get();
304 buf_alloc(sys_cmd, STR_COMMAND);
305
307 mutt_endwin();
308 fflush(stdout);
309
311 sys_cmd->dsize, sys_cmd);
312
313 if (mutt_system(buf_string(sys_cmd)) != 0)
314 {
315 rc = false;
317 mutt_error(_("Error running \"%s\""), buf_string(sys_cmd));
318 }
319
321
322 buf_pool_release(&sys_cmd);
323 return rc;
324}
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:70
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:91
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:52
#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:212
void mutt_sig_unblock(void)
Restore previously blocked signals.
Definition: signal.c:230
#define STR_COMMAND
Enough space for a long command line.
Definition: string2.h:35
size_t dsize
Length of data.
Definition: buffer.h:39
bool verbose
Display status messages?
Definition: mailbox.h:117
+ 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 337 of file compress.c.

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

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

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

Compression Commands.

Definition at line 63 of file compress.c.

◆ 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:50
@ ED_CMP_TO
'to' path
Definition: lib.h:51
@ ED_COMPRESS
Compress ED_CMP_ ExpandoDataCompress.
Definition: domain.h:40

Expando definitions.

Config:

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

Definition at line 79 of file compress.c.