]> git.ipfire.org Git - pakfire.git/commitdiff
Add convenience functions to create directories
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Nov 2017 17:12:51 +0000 (18:12 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Nov 2017 17:12:51 +0000 (18:12 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/util.c

index dbe001abc2ef2ac4059f77bf727eaff4050603a5..3574ff999f51ccfa4741b983b41b75daaa9ab5ba 100644 (file)
@@ -22,6 +22,7 @@
 #define PAKFIRE_UTIL_H
 
 #include <stddef.h>
+#include <sys/types.h>
 #include <time.h>
 
 void pakfire_oom(size_t num, size_t len);
@@ -41,6 +42,8 @@ char* pakfire_path_join(const char* first, const char* second);
 
 char* pakfire_basename(const char* path);
 char* pakfire_dirname(const char* path);
+int pakfire_access(const char* dir, const char* file, int mode);
+int pakfire_mkdir(const char* path, mode_t mode);
 
 char* pakfire_sgets(char* str, int num, char** input);
 char* pakfire_remove_trailing_newline(char* str);
index a4bf292cd5afd2e833e08325b448e0dea8ec2fa6..ed783e8251ed74daa86c6d66c43a9ed1b7667bd0 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
 #include <libgen.h>
 #include <math.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <time.h>
+#include <unistd.h>
 
 #include <gcrypt.h>
 
 #include <pakfire/constants.h>
+#include <pakfire/logging.h>
 
 void pakfire_oom(size_t num, size_t len) {
        if (num)
@@ -145,6 +150,59 @@ char* pakfire_dirname(const char* path) {
        return dirname(parent);
 }
 
+int pakfire_access(const char* dir, const char* file, int mode) {
+       char* path = pakfire_path_join(dir, file);
+
+       int r = access(path, mode);
+
+       if (r) {
+               if (mode & R_OK)
+                       DEBUG("%s is not readable\n", path);
+
+               if (mode & W_OK)
+                       DEBUG("%s is not writable\n", path);
+
+               if (mode & X_OK)
+                       DEBUG("%s is not executable\n", path);
+
+               if (mode & F_OK)
+                       DEBUG("%s does not exist\n", path);
+       }
+
+       return r;
+}
+
+int pakfire_mkdir(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(parent, NULL, F_OK);
+       if (r)
+               r = pakfire_mkdir(parent, 0);
+
+       pakfire_free(parent);
+
+       if (r)
+               return r;
+
+       // 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;