NeoMutt  2023-11-03-107-g582dc1
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
shared.c File Reference

MH shared functions. More...

#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "shared.h"
#include "globals.h"
#include "mdata.h"
+ Include dependency graph for shared.c:

Go to the source code of this file.

Functions

mode_t mh_umask (struct Mailbox *m)
 Create a umask from the mailbox directory.
 
bool mh_mkstemp (struct Mailbox *m, FILE **fp, char **tgt)
 Create a temporary file.
 

Detailed Description

MH shared functions.

Authors
  • Michael R. Elkins
  • Thomas Roessler
  • Michael R. Elkins
  • 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 shared.c.

Function Documentation

◆ mh_umask()

mode_t mh_umask ( struct Mailbox m)

Create a umask from the mailbox directory.

Parameters
mMailbox
Return values
numUmask

Definition at line 52 of file shared.c.

53{
54 struct MhMboxData *mhata = mh_mdata_get(m);
55 if (mhata && mhata->umask)
56 return mhata->umask;
57
58 struct stat st = { 0 };
59 if (stat(mailbox_path(m), &st) != 0)
60 {
61 mutt_debug(LL_DEBUG1, "stat failed on %s\n", mailbox_path(m));
62 return 077;
63 }
64
65 return 0777 & ~st.st_mode;
66}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:210
struct MhMboxData * mh_mdata_get(struct Mailbox *m)
Get the private data for this Mailbox.
Definition: mdata.c:60
Mh-specific Mailbox data -.
Definition: mdata.h:35
mode_t umask
umask to use when creating files
Definition: mdata.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mh_mkstemp()

bool mh_mkstemp ( struct Mailbox m,
FILE **  fp,
char **  tgt 
)

Create a temporary file.

Parameters
[in]mMailbox to create the file in
[out]fpFile handle
[out]tgtFile name
Return values
trueSuccess
falseFailure

Definition at line 76 of file shared.c.

77{
78 int fd;
79 char path[PATH_MAX] = { 0 };
80
81 mode_t omask = umask(mh_umask(m));
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(omask);
93 return false;
94 }
95 }
96 else
97 {
98 *tgt = mutt_str_dup(path);
99 break;
100 }
101 }
102 umask(omask);
103
104 *fp = fdopen(fd, "w");
105 if (!*fp)
106 {
107 FREE(tgt);
108 close(fd);
109 unlink(path);
110 return false;
111 }
112
113 return true;
114}
char * ShortHostname
Short version of the hostname.
Definition: globals.c:40
#define mutt_perror(...)
Definition: logging2.h:93
#define FREE(x)
Definition: memory.h:45
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
#define PATH_MAX
Definition: mutt.h:41
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition: random.c:121
mode_t mh_umask(struct Mailbox *m)
Create a umask from the mailbox directory.
Definition: shared.c:52
#define NONULL(x)
Definition: string2.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function: