NeoMutt
2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Toggle main menu visibility
Loading...
Searching...
No Matches
shared.c
Go to the documentation of this file.
1
22
28
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
49
mode_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
73
bool
mh_mkstemp
(
struct
Mailbox
*m, FILE **fp,
char
**tgt)
74
{
75
int
fd;
76
char
path[
PATH_MAX
] = { 0 };
77
78
mode_t new_umask =
mh_umask
(m);
79
mode_t old_umask = umask(new_umask);
80
mutt_debug
(
LL_DEBUG3
,
"umask set to %03o\n"
, new_umask);
81
82
while
(
true
)
83
{
84
snprintf(path,
sizeof
(path),
"%s/.neomutt-%s-%d-%"
PRIu64,
mailbox_path
(m),
85
NONULL
(
ShortHostname
), (
int
) getpid(),
mutt_rand64
());
86
fd = open(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
87
if
(fd == -1)
88
{
89
if
(errno != EEXIST)
90
{
91
mutt_perror
(
"%s"
, path);
92
umask(old_umask);
93
mutt_debug
(
LL_DEBUG3
,
"umask set to %03o\n"
, old_umask);
94
return
false
;
95
}
96
}
97
else
98
{
99
*tgt =
mutt_str_dup
(path);
100
break
;
101
}
102
}
103
umask(old_umask);
104
mutt_debug
(
LL_DEBUG3
,
"umask set to %03o\n"
, old_umask);
105
106
*fp = fdopen(fd,
"w"
);
107
if
(!*fp)
108
{
109
FREE
(tgt);
110
close(fd);
111
unlink(path);
112
return
false
;
113
}
114
115
return
true
;
116
}
lib.h
Convenience wrapper for the core headers.
mailbox_path
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition
mailbox.h:216
ShortHostname
char * ShortHostname
Short version of the hostname.
Definition
globals.c:36
globals.h
Global variables.
mutt_debug
#define mutt_debug(LEVEL,...)
Definition
logging2.h:91
mutt_perror
#define mutt_perror(...)
Definition
logging2.h:95
LL_DEBUG3
@ LL_DEBUG3
Log at debug level 3.
Definition
logging2.h:47
LL_DEBUG1
@ LL_DEBUG1
Log at debug level 1.
Definition
logging2.h:45
FREE
#define FREE(x)
Free memory and set the pointer to NULL.
Definition
memory.h:68
mh_mdata_get
struct MhMboxData * mh_mdata_get(struct Mailbox *m)
Get the private data for this Mailbox.
Definition
mdata.c:59
mdata.h
Mh-specific Mailbox data.
mh_mkstemp
bool mh_mkstemp(struct Mailbox *m, FILE **fp, char **tgt)
Create a temporary file.
Definition
shared.c:73
mh_umask
mode_t mh_umask(struct Mailbox *m)
Create a umask from the mailbox directory.
Definition
shared.c:49
shared.h
MH shared functions.
lib.h
Convenience wrapper for the library headers.
mutt_str_dup
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition
string.c:257
PATH_MAX
#define PATH_MAX
Definition
mutt.h:49
mutt_rand64
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition
random.c:123
NONULL
#define NONULL(x)
Definition
string2.h:44
Mailbox
A mailbox.
Definition
mailbox.h:81
MhMboxData
Mh-specific Mailbox data -.
Definition
mdata.h:35
MhMboxData::umask
mode_t umask
umask to use when creating files
Definition
mdata.h:38