From 9ffcf336d7595db792d1cdde6ad2e02ea5aeb0c4 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sun, 3 May 2026 15:49:05 -0400 Subject: [PATCH] libremail+tools: Add mailbox_create() function It is best to prefer mailbox_create() rather than try_mkdir() directly as we'll be able to encapslate mailbox creation logic within it. Signed-off-by: Chloe M. --- libremail/common/mailbox.c | 29 +++++++++++++++++++++++++++++ libremail/inc/libremail/mailbox.h | 21 +++++++++++++++++++++ tools/mailutil/core/mailutil.c | 3 ++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 libremail/common/mailbox.c create mode 100644 libremail/inc/libremail/mailbox.h diff --git a/libremail/common/mailbox.c b/libremail/common/mailbox.c new file mode 100644 index 0000000..5b0fd20 --- /dev/null +++ b/libremail/common/mailbox.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2026, Chloe Moffett + * Provided under the BSD-3 clause + */ + +#include +#include +#include +#include +#include "libremail/mailbox.h" + +int +mailbox_create(const char *path, mode_t mode) +{ + int error; + + if (path == NULL) { + errno = EINVAL; + return -1; + } + + error = mkdir(path, mode); + if (error < 0) { + perror("mkdir"); + return -1; + } + + return 0; +} diff --git a/libremail/inc/libremail/mailbox.h b/libremail/inc/libremail/mailbox.h new file mode 100644 index 0000000..0311796 --- /dev/null +++ b/libremail/inc/libremail/mailbox.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2026, Chloe Moffett + * Provided under the BSD-3 clause + */ + +#ifndef LIBREMAIL_MAILBOX_H +#define LIBREMAIL_MAILBOX_H 1 + +#include + +/* + * Create a mailbox directory + * + * @path: Path to mailbox to create + * @mode: Mode to assign to directory + * + * Returns zero on success + */ +int mailbox_create(const char *path, mode_t mode); + +#endif /* !LIBREMAIL_MAILBOX_H */ diff --git a/tools/mailutil/core/mailutil.c b/tools/mailutil/core/mailutil.c index b1f3589..c5e57c7 100644 --- a/tools/mailutil/core/mailutil.c +++ b/tools/mailutil/core/mailutil.c @@ -10,6 +10,7 @@ #include #include "libremail/file.h" #include "libremail/common.h" +#include "libremail/mailbox.h" /* Maximum command components */ #define CMD_MAX_CNP 6 @@ -120,7 +121,7 @@ cmd_mailbox_create(const char *cmdlist[CMD_MAX_CNP], size_t count) MAILBOX_PREFIX, cmdlist[2]); /* Create the mailbox */ - if (try_mkdir(pathbuf, DEFAULT_MAILBOX_MODE) < 0) { + if (mailbox_create(pathbuf, DEFAULT_MAILBOX_MODE) < 0) { printf("fatal: failed to create mailbox\n"); perror("try_mkdir"); return -1;