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. <chloe@mirocom.org>
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Chloe Moffett
|
||||||
|
* Provided under the BSD-3 clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -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 <sys/stat.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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 */
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include "libremail/file.h"
|
#include "libremail/file.h"
|
||||||
#include "libremail/common.h"
|
#include "libremail/common.h"
|
||||||
|
#include "libremail/mailbox.h"
|
||||||
|
|
||||||
/* Maximum command components */
|
/* Maximum command components */
|
||||||
#define CMD_MAX_CNP 6
|
#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]);
|
MAILBOX_PREFIX, cmdlist[2]);
|
||||||
|
|
||||||
/* Create the mailbox */
|
/* 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");
|
printf("fatal: failed to create mailbox\n");
|
||||||
perror("try_mkdir");
|
perror("try_mkdir");
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user