From 405f7d351ea3ec3d09253241bd43298d34efdea7 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sat, 2 May 2026 19:51:56 -0400 Subject: [PATCH] libremail: file: Add try_touch() function The try_touch() function is responsible for attempting to create new files while optionally filling it with initial data Signed-off-by: Chloe M. --- libremail/common/file_mkdir.c | 24 ------------- libremail/common/file_subr.c | 58 ++++++++++++++++++++++++++++++++ libremail/inc/libremail/common.h | 12 +++++++ libremail/inc/libremail/file.h | 12 +++++++ 4 files changed, 82 insertions(+), 24 deletions(-) delete mode 100644 libremail/common/file_mkdir.c create mode 100644 libremail/common/file_subr.c create mode 100644 libremail/inc/libremail/common.h diff --git a/libremail/common/file_mkdir.c b/libremail/common/file_mkdir.c deleted file mode 100644 index 8bc4e0e..0000000 --- a/libremail/common/file_mkdir.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2026, Chloe Moffett - * Provided under the BSD-3 clause - */ - -#include -#include -#include -#include "libremail/file.h" - -int -try_mkdir(const char *path, mode_t mode) -{ - if (path == NULL) { - errno = EINVAL; - return -1; - } - - if (access(path, F_OK) != 0) { - return mkdir(path, mode); - } - - return 0; -} diff --git a/libremail/common/file_subr.c b/libremail/common/file_subr.c new file mode 100644 index 0000000..2d6765a --- /dev/null +++ b/libremail/common/file_subr.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2026, Chloe Moffett + * Provided under the BSD-3 clause + */ + +#include +#include +#include +#include +#include +#include +#include "libremail/file.h" + +int +try_mkdir(const char *path, mode_t mode) +{ + if (path == NULL) { + errno = EINVAL; + return -1; + } + + if (access(path, F_OK) != 0) { + return mkdir(path, mode); + } + + return 0; +} + +int +try_touch(const char *path, mode_t mode, void *buf, size_t len) +{ + ssize_t retlen; + int fd; + + if (path == NULL) { + errno = EINVAL; + return -1; + } + + fd = open(path, O_RDONLY | O_CREAT, mode); + if (fd < 0) { + perror("open"); + return -1; + } + + /* Write the initial contents if we can */ + if (buf != NULL) { + retlen = write(fd, buf, len); + if (retlen <= 0) { + close(fd); + perror("write"); + return -1; + } + } + + close(fd); + return 0; +} diff --git a/libremail/inc/libremail/common.h b/libremail/inc/libremail/common.h new file mode 100644 index 0000000..8c7b873 --- /dev/null +++ b/libremail/inc/libremail/common.h @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2026, Chloe Moffett + * Provided under the BSD-3 clause + */ + +#ifndef LIBREMAIL_COMMON_H +#define LIBREMAIL_COMMON_H 1 + +/* Length of SHA256 hash in bytes */ +#define SHA256_N_BYTES 32 + +#endif /* !LIBREMAIL_COMMON_H */ diff --git a/libremail/inc/libremail/file.h b/libremail/inc/libremail/file.h index 5e1c341..4b9f054 100644 --- a/libremail/inc/libremail/file.h +++ b/libremail/inc/libremail/file.h @@ -18,4 +18,16 @@ */ int try_mkdir(const char *path, mode_t mode); +/* + * Attempt to create a new file if it doesn't exist + * + * @path: Path to file to create + * @mode: Desired mode to assign + * @buf: Buffer of initial data (NULLable) + * @len: Length of initial data + * + * Returns zero on success + */ +int try_touch(const char *path, mode_t mode, void *buf, size_t len); + #endif /* !LIBREMAIL_FILE_H */