From: Michael Tremer Date: Fri, 26 Mar 2021 17:50:55 +0000 (+0000) Subject: libpakfire: Refactor pakfire_mkdir X-Git-Tag: 0.9.28~1285^2~461 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d47c16346d6a2775dfff560f31d58b36ce6f918;p=pakfire.git libpakfire: Refactor pakfire_mkdir Passing the Pakfire instance was a bit excessive and this function should remain private. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 345b67fe5..27213e857 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -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); diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index c240b0250..d8fd4d40c 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -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; } diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 1daf0251c..3598cb28a 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -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 diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 8dcf62098..6f9912dd4 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -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;