]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
fd: Revamp para_mkdir().
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 25 Sep 2022 20:35:16 +0000 (22:35 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 7 May 2023 18:45:22 +0000 (20:45 +0200)
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
fd.c
fd.h
play.c

diff --git a/afs.c b/afs.c
index 000aaa54670714a195a6d8bce3e12f3f723017dc..8bd52c9af282c59c31106bf667625bd586ca1c53 100644 (file)
--- 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 d858ae623ba6203719e70930625df97867166e67..917ed186a00182568c3aa6618ef54d771e59ea17 100644 (file)
--- 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 da480f0bc134e20f1f6d656d12eb635dbf31be85..8fb1fb651363bd816edfb680439b51496aed7a43 100644 (file)
--- 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 4024c8ea4fa5468ff81059f89e732df29dd5fbbf..bd183b6b8dd48906e08cf142d7ff8c1e05aad48a 100644 (file)
--- 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);