NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
shared.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <errno.h>
31#include <fcntl.h>
32#include <inttypes.h>
33#include <limits.h>
34#include <stdbool.h>
35#include <stdio.h>
36#include <sys/stat.h>
37#include <unistd.h>
38#include "mutt/lib.h"
39#include "core/lib.h"
40#include "shared.h"
41#include "globals.h"
42#include "mdata.h"
43
49mode_t mh_umask(struct Mailbox *m)
50{
51 struct MhMboxData *mhata = mh_mdata_get(m);
52 if (mhata && mhata->umask)
53 return mhata->umask;
54
55 struct stat st = { 0 };
56 if (stat(mailbox_path(m), &st) != 0)
57 {
58 mutt_debug(LL_DEBUG1, "stat failed on %s\n", mailbox_path(m));
59 return 077;
60 }
61
62 return 0777 & ~st.st_mode;
63}
64
73bool mh_mkstemp(struct Mailbox *m, FILE **fp, char **tgt)
74{
75 int fd;
76 char path[PATH_MAX] = { 0 };
77
78 mode_t omask = umask(mh_umask(m));
79 while (true)
80 {
81 snprintf(path, sizeof(path), "%s/.neomutt-%s-%d-%" PRIu64, mailbox_path(m),
82 NONULL(ShortHostname), (int) getpid(), mutt_rand64());
83 fd = open(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
84 if (fd == -1)
85 {
86 if (errno != EEXIST)
87 {
88 mutt_perror("%s", path);
89 umask(omask);
90 return false;
91 }
92 }
93 else
94 {
95 *tgt = mutt_str_dup(path);
96 break;
97 }
98 }
99 umask(omask);
100
101 *fp = fdopen(fd, "w");
102 if (!*fp)
103 {
104 FREE(tgt);
105 close(fd);
106 unlink(path);
107 return false;
108 }
109
110 return true;
111}
Convenience wrapper for the core headers.
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:223
char * ShortHostname
Short version of the hostname.
Definition: globals.c:39
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define FREE(x)
Definition: memory.h:45
struct MhMboxData * mh_mdata_get(struct Mailbox *m)
Get the private data for this Mailbox.
Definition: mdata.c:60
bool mh_mkstemp(struct Mailbox *m, FILE **fp, char **tgt)
Create a temporary file.
Definition: shared.c:73
mode_t mh_umask(struct Mailbox *m)
Create a umask from the mailbox directory.
Definition: shared.c:49
MH shared functions.
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
#define PATH_MAX
Definition: mutt.h:42
Notmuch-specific Mailbox data.
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition: random.c:122
#define NONULL(x)
Definition: string2.h:37
A mailbox.
Definition: mailbox.h:79
Mh-specific Mailbox data -.
Definition: mdata.h:35
mode_t umask
umask to use when creating files
Definition: mdata.h:38