From da171f28894c6a3fc29b58bf8ee783bc7d28598c Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 25 Sep 2022 22:35:16 +0200 Subject: [PATCH] fd: Revamp para_mkdir(). It has two callers which both pass the mode value 0777 and contain extra code to regard the EEXIST error case as a success. Move the common bits into the wrapper and improve the documentation. --- afs.c | 14 ++------------ fd.c | 16 +++++++++++----- fd.h | 2 +- play.c | 4 ++-- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/afs.c b/afs.c index 000aaa54..8bd52c9a 100644 --- a/afs.c +++ b/afs.c @@ -580,17 +580,6 @@ static void get_database_dir(void) PARA_INFO_LOG("afs_database dir %s\n", database_dir); } -static int make_database_dir(void) -{ - int ret; - - get_database_dir(); - ret = para_mkdir(database_dir, 0777); - if (ret >= 0 || ret == -ERRNO_TO_PARA_ERROR(EEXIST)) - return 1; - return ret; -} - static int open_afs_tables(void) { int i, ret; @@ -1061,7 +1050,8 @@ static int com_init(struct command_context *cc, struct lls_parse_result *lpr) .size = sizeof(table_mask)}; unsigned num_inputs = lls_num_inputs(lpr); - ret = make_database_dir(); + get_database_dir(); + ret = para_mkdir(database_dir); if (ret < 0) return ret; if (num_inputs > 0) { diff --git a/fd.c b/fd.c index d858ae62..917ed186 100644 --- a/fd.c +++ b/fd.c @@ -440,17 +440,23 @@ close_cwd: } /** - * A wrapper for mkdir(2). + * Create a directory, don't fail if it already exists. * * \param path Name of the directory to create. - * \param mode The permissions to use. * - * \return Standard. + * This function passes the fixed mode value 0777 to mkdir(3) (which consults + * the file creation mask and restricts this value). + * + * \return Zero if the directory already existed, one if the directory has been + * created, negative error code if the mkdir(3) call failed for any reason + * other than EEXIST. */ -int para_mkdir(const char *path, mode_t mode) +int para_mkdir(const char *path) { - if (!mkdir(path, mode)) + if (mkdir(path, 0777) == 0) return 1; + if (errno == EEXIST) + return 0; return -ERRNO_TO_PARA_ERROR(errno); } diff --git a/fd.h b/fd.h index da480f0b..8fb1fb65 100644 --- a/fd.h +++ b/fd.h @@ -10,7 +10,7 @@ __must_check int mark_fd_nonblocking(int fd); __must_check int mark_fd_blocking(int fd); int para_mmap(size_t length, int prot, int flags, int fd, void *map); int para_open(const char *path, int flags, mode_t mode); -int para_mkdir(const char *path, mode_t mode); +int para_mkdir(const char *path); int mmap_full_file(const char *filename, int open_mode, void **map, size_t *size, int *fd_ptr); int para_munmap(void *start, size_t length); diff --git a/play.c b/play.c index 4024c8ea..bd183b6b 100644 --- a/play.c +++ b/play.c @@ -1048,9 +1048,9 @@ static void session_open(void) char *dot_para = make_message("%s/.paraslash", home); free(home); - ret = para_mkdir(dot_para, 0777); + ret = para_mkdir(dot_para); /* warn, but otherwise ignore mkdir error */ - if (ret < 0 && ret != -ERRNO_TO_PARA_ERROR(EEXIST)) + if (ret < 0) PARA_WARNING_LOG("Can not create %s: %s\n", dot_para, para_strerror(-ret)); history_file = make_message("%s/play.history", dot_para); -- 2.39.2