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);
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);
#############################################################################*/
#include <assert.h>
+#include <errno.h>
#include <gpgme.h>
#include <stdlib.h>
#include <string.h>
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;
}
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);
// 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);
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
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;
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);
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)
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;
}
char* pakfire_mkdtemp(char* path) {
- int r = pakfire_mkparentdir(path);
+ int r = pakfire_mkparentdir(path, 0);
if (r)
return NULL;