]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Refactor pakfire_mkdir
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 Mar 2021 17:50:55 +0000 (17:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 Mar 2021 17:50:55 +0000 (17:50 +0000)
Passing the Pakfire instance was a bit excessive and this function
should remain private.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/key.c
src/libpakfire/pakfire.c
src/libpakfire/util.c

index 345b67fe5b280f0c7efcb3a5126438ff490cbe6f..27213e85722f0f8498a4b1b9f72da9e3ac8468a6 100644 (file)
@@ -42,7 +42,6 @@ int pakfire_path_isdir(const char* path);
 char* pakfire_basename(const char* path);
 char* pakfire_dirname(const char* path);
 int pakfire_access(Pakfire pakfire, const char* dir, const char* file, int mode);
-int pakfire_mkdir(Pakfire pakfire, const char* path, mode_t mode);
 
 char* pakfire_sgets(char* str, int num, char** input);
 char* pakfire_remove_trailing_newline(char* str);
@@ -68,6 +67,7 @@ time_t pakfire_path_age(const char* path);
 
 char* pakfire_hexlify(const char* digest, const size_t length);
 
+int pakfire_mkdir(const char* path, mode_t mode);
 FILE* pakfire_mktemp(char* path);
 char* pakfire_mkdtemp(char* path);
 int pakfire_rmtree(const char* path, int flags);
index c240b02501a00099296187e7f05b6c3fba97a83b..d8fd4d40cccda7bebd1e1018d7c35e483434cf62 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <assert.h>
+#include <errno.h>
 #include <gpgme.h>
 #include <stdlib.h>
 #include <string.h>
@@ -72,8 +73,8 @@ gpgme_ctx_t pakfire_get_gpgctx(Pakfire pakfire) {
        if (pakfire_access(pakfire, home, NULL, R_OK) != 0) {
                DEBUG(pakfire, "Creating GPG database at %s\n", home);
 
-               int r = pakfire_mkdir(pakfire, home, S_IRUSR|S_IWUSR|S_IXUSR);
-               if (r) {
+               int r = pakfire_mkdir(home, S_IRUSR|S_IWUSR|S_IXUSR);
+               if (r && errno != EEXIST) {
                        ERROR(pakfire, "Could not initialize the GPG database at %s\n", home);
                        goto FAIL;
                }
index 1daf0251c5ed907a6df6de0696dd8c120baa3c56..3598cb28a4b24dac437a311f565ccf3ed660bda1 100644 (file)
@@ -153,7 +153,7 @@ RETRY:
                if (r) {
                        // If the target directory does not exist, we will create it
                        if (errno == ENOENT) {
-                               r = pakfire_mkdir(pakfire, target, S_IRUSR|S_IWUSR|S_IXUSR);
+                               r = pakfire_mkdir(target, S_IRUSR|S_IWUSR|S_IXUSR);
                                if (r) {
                                        ERROR(pakfire, "Could not create %s\n", target);
                                        free(target);
@@ -467,8 +467,8 @@ PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char
 
        // Make sure that our private directory exists
        char* private_dir = pakfire_make_path(p, PAKFIRE_PRIVATE_DIR);
-       r = pakfire_mkdir(p, private_dir, 0);
-       if (r) {
+       r = pakfire_mkdir(private_dir, 0);
+       if (r && errno != EEXIST) {
                ERROR(p, "Could not create private directory %s: %s\n",
                        private_dir, strerror(errno));
                free(private_dir);
@@ -529,8 +529,8 @@ PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* ds
        DEBUG(pakfire, "Mounting %s to %s\n", src, mountpoint);
 
        // Make sure the directory exists
-       int r = pakfire_mkdir(pakfire, mountpoint, 0);
-       if (r)
+       int r = pakfire_mkdir(mountpoint, 0);
+       if (r && errno != EEXIST)
                goto ERROR;
 
        // Perform mount
index 8dcf620985673457feda191bbfa060d1de59cec3..6f9912dd478ad03d3a0b2328508de95fb5bf7070 100644 (file)
@@ -315,40 +315,6 @@ PAKFIRE_EXPORT int pakfire_access(Pakfire pakfire, const char* dir, const char*
        return r;
 }
 
-int pakfire_mkdir(Pakfire pakfire, const char* path, mode_t mode) {
-       int r = 0;
-
-       if ((strcmp(path, "/") == 0) || (strcmp(path, ".") == 0))
-               return 0;
-
-       // If parent does not exists, we try to create it.
-       char* parent = pakfire_dirname(path);
-       r = pakfire_access(pakfire, parent, NULL, F_OK);
-       if (r)
-               r = pakfire_mkdir(pakfire, parent, 0);
-
-       free(parent);
-
-       // Exit if parent directory could not be created
-       if (r)
-               return r;
-
-       DEBUG(pakfire, "Creating directory %s\n", path);
-
-       // Finally, create the directory we want.
-       r = mkdir(path, mode);
-       if (r) {
-               switch (errno) {
-                       // If the directory already exists, this is fine.
-                       case EEXIST:
-                               r = 0;
-                               break;
-               }
-       }
-
-       return r;
-}
-
 char* pakfire_sgets(char* str, int num, char** input) {
        char* next = *input;
        int numread = 0;
@@ -520,7 +486,7 @@ char* pakfire_hexlify(const char* digest, const size_t length) {
        return s;
 }
 
-static int pakfire_mkparentdir(const char* path) {
+static int pakfire_mkparentdir(const char* path, mode_t mode) {
        int r;
 
        char* dirname = pakfire_dirname(path);
@@ -532,12 +498,12 @@ static int pakfire_mkparentdir(const char* path) {
                return 0;
 
        // Ensure the parent directory exists
-       r = pakfire_mkparentdir(dirname);
+       r = pakfire_mkparentdir(dirname, mode);
        if (r)
                goto END;
 
        // Create this directory
-       r = mkdir(dirname, 0);
+       r = mkdir(dirname, mode);
 
        // Ignore when the directory already exists
        if (r && errno == EEXIST)
@@ -549,8 +515,17 @@ END:
        return r;
 }
 
+int pakfire_mkdir(const char* path, mode_t mode) {
+       int r = pakfire_mkparentdir(path, mode);
+       if (r)
+               return r;
+
+       // Finally, create the directory we want
+       return mkdir(path, mode);
+}
+
 FILE* pakfire_mktemp(char* path) {
-       int r = pakfire_mkparentdir(path);
+       int r = pakfire_mkparentdir(path, 0);
        if (r)
                return NULL;
 
@@ -564,7 +539,7 @@ FILE* pakfire_mktemp(char* path) {
 }
 
 char* pakfire_mkdtemp(char* path) {
-       int r = pakfire_mkparentdir(path);
+       int r = pakfire_mkparentdir(path, 0);
        if (r)
                return NULL;