NeoMutt
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 <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.
 
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 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 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 -.
 
static void expand_command_str (const struct Mailbox *m, const char *cmd, char *buf, int buflen)
 Expand placeholders in command string.
 
static bool execute_command (struct Mailbox *m, const char *command, 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() -.
 
static int comp_path_pretty (struct Buffer *path, const char *folder)
 Abbreviate a Mailbox path - Implements MxOps::path_pretty() -.
 
static int comp_path_parent (struct Buffer *path)
 Find the parent of a Mailbox path - Implements MxOps::path_parent() -.
 

Variables

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

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

74{
76}
static const struct Command CompCommands[]
Compression Commands.
Definition: compress.c:62
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 89 of file compress.c.

90{
91 if (!m || !m->compress_info)
92 return false;
93
94 struct CompressInfo *ci = m->compress_info;
95
96 if (ci->locked)
97 return true;
98
99 if (excl)
100 ci->fp_lock = fopen(m->realpath, "a");
101 else
102 ci->fp_lock = fopen(m->realpath, "r");
103 if (!ci->fp_lock)
104 {
105 mutt_perror("%s", m->realpath);
106 return false;
107 }
108
109 int r = mutt_file_lock(fileno(ci->fp_lock), excl, true);
110 if (r == 0)
111 {
112 ci->locked = true;
113 }
114 else if (excl)
115 {
117 m->readonly = true;
118 return true;
119 }
120
121 return r == 0;
122}
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1243
#define mutt_perror(...)
Definition: logging2.h:93
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:121
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 130 of file compress.c.

131{
132 if (!m || !m->compress_info)
133 return;
134
135 struct CompressInfo *ci = m->compress_info;
136
137 if (!ci->locked)
138 return;
139
140 mutt_file_unlock(fileno(ci->fp_lock));
141
142 ci->locked = false;
144}
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1290
+ 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 156 of file compress.c.

157{
158 if (!m)
159 return -1;
160
161 /* Setup the right paths */
163
164 /* We will uncompress to TMPDIR */
165 struct Buffer *buf = buf_pool_get();
166 buf_mktemp(buf);
167 buf_copy(&m->pathbuf, buf);
168 buf_pool_release(&buf);
169
170 FILE *fp = mutt_file_fopen(mailbox_path(m), "w");
171 if (!fp)
172 return -1;
173
174 mutt_file_fclose(&fp);
175 return 0;
176}
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:572
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:636
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:210
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
String manipulation buffer.
Definition: buffer.h:34
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 184 of file compress.c.

185{
186 if (!m || !m->compress_info)
187 return;
188
189 struct CompressInfo *ci = m->compress_info;
190
192}
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1560
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 202 of file compress.c.

203{
204 if (!m)
205 return NULL;
206
207 if (m->compress_info)
208 return m->compress_info;
209
210 /* Open is compulsory */
211 const char *o = mutt_find_hook(MUTT_OPEN_HOOK, mailbox_path(m));
212 if (!o)
213 return NULL;
214
215 const char *c = mutt_find_hook(MUTT_CLOSE_HOOK, mailbox_path(m));
216 const char *a = mutt_find_hook(MUTT_APPEND_HOOK, mailbox_path(m));
217
218 struct CompressInfo *ci = mutt_mem_calloc(1, sizeof(struct CompressInfo));
219 m->compress_info = ci;
220
221 ci->cmd_open = mutt_str_dup(o);
222 ci->cmd_close = mutt_str_dup(c);
223 ci->cmd_append = mutt_str_dup(a);
224
225 return ci;
226}
char * mutt_find_hook(HookFlags type, const char *pat)
Find a matching hook.
Definition: hook.c:636
#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:251
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 232 of file compress.c.

233{
234 if (!m || !m->compress_info)
235 return;
236
237 struct CompressInfo *ci = m->compress_info;
238 FREE(&ci->cmd_open);
239 FREE(&ci->cmd_close);
240 FREE(&ci->cmd_append);
241
243
244 FREE(&m->compress_info);
245}
static void unlock_realpath(struct Mailbox *m)
Unlock the mailbox->realpath.
Definition: compress.c:130
#define FREE(x)
Definition: memory.h:45
+ 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 307 of file compress.c.

308{
309 if (!m || !cmd || !buf)
310 return;
311
312 mutt_expando_format(buf, buflen, 0, buflen, cmd, compress_format_str,
313 (intptr_t) m, MUTT_FORMAT_NO_FLAGS);
314}
#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:255
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:745
+ 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 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
trueSuccess
falseFailure

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

Definition at line 327 of file compress.c.

328{
329 if (!m || !command || !progress)
330 return false;
331
332 if (m->verbose)
333 mutt_message(progress, m->realpath);
334
335 bool rc = true;
336 char sys_cmd[STR_COMMAND] = { 0 };
337
339 endwin();
340 fflush(stdout);
341
342 expand_command_str(m, command, sys_cmd, sizeof(sys_cmd));
343
344 if (mutt_system(sys_cmd) != 0)
345 {
346 rc = false;
348 mutt_error(_("Error running \"%s\""), sys_cmd);
349 }
350
352
353 return rc;
354}
static void expand_command_str(const struct Mailbox *m, const char *cmd, char *buf, int buflen)
Expand placeholders in command string.
Definition: compress.c:307
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:188
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define _(a)
Definition: message.h:28
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:52
void mutt_sig_block(void)
Block signals during critical operations.
Definition: signal.c:163
void mutt_sig_unblock(void)
Restore previously blocked signals.
Definition: signal.c:181
#define STR_COMMAND
Enough space for a long command line.
Definition: string2.h:35
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 367 of file compress.c.

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

397{
398 if (!path)
399 return false;
400
402 return true;
403
404 return false;
405}
+ 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 416 of file compress.c.

417{
418 if (!cmd)
419 return 0;
420
421 return strstr(cmd, "%f") && strstr(cmd, "%t");
422}
+ 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:128

Compression Commands.

Definition at line 62 of file compress.c.