]> git.tuebingen.mpg.de Git - adu.git/blobdiff - fd.c
Use ?:= as the assignement operator for PREFIX.
[adu.git] / fd.c
diff --git a/fd.c b/fd.c
index 40eb2fa1e6c3d1562d444a35f682a580c3327626..e661e71fa32d2c20d92f558f9f40f62f576b9ba1 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -4,14 +4,16 @@
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file fd.c Helper functions for file descriptor handling. */
+/** \file fd.c \brief Helper functions for file descriptor handling. */
 
 #include <sys/types.h>
 #include <dirent.h>
 #include <sys/mman.h>
+#include <string.h>
 
 #include "adu.h"
 #include "error.h"
+#include "string.h"
 
 /**
  * Wrapper for the write system call.
@@ -98,7 +100,7 @@ static int __open(const char *path, int flags, mode_t mode)
  *
  * \return Standard.
  */
-int para_write_file(const char *filename, const void *buf, size_t size)
+int adu_write_file(const char *filename, const void *buf, size_t size)
 {
        int ret, fd;
 
@@ -155,7 +157,7 @@ int __chdir(const char *path)
  * \sa getcwd(3).
  *
  */
-int para_opendir(const char *dirname, DIR **dir, int *cwd)
+int adu_opendir(const char *dirname, DIR **dir, int *cwd)
 {
        int ret;
 
@@ -188,7 +190,7 @@ close_cwd:
  *
  * \return Standard.
  */
-int para_fchdir(int fd)
+int adu_fchdir(int fd)
 {
        if (fchdir(fd) < 0)
                return -ERRNO_TO_ERROR(errno);
@@ -263,7 +265,7 @@ out:
  *
  * \sa munmap(2), mmap_full_file().
  */
-int para_munmap(void *start, size_t length)
+int adu_munmap(void *start, size_t length)
 {
        int err;
        if (munmap(start, length) >= 0)
@@ -273,3 +275,54 @@ int para_munmap(void *start, size_t length)
                strerror(err));
        return -ERRNO_TO_ERROR(err);
 }
+
+__must_check __malloc static char *adu_dirname(const char *name)
+{
+       char *p, *ret;
+
+       if (!name || !*name)
+               return NULL;
+       ret = adu_strdup(name);
+       p = strrchr(ret, '/');
+       if (!p)
+               *ret = '\0';
+       else
+               *p = '\0';
+       return ret;
+}
+
+/**
+ * Recursive mkdir
+ *
+ * \param p Full path that should be created.
+ *
+ * \param mode Use this mode when creating directories.
+ *
+ * \return 0 if successful, -E_MKDIR on errors.
+ */
+int mkpath(const char *p, mode_t mode)
+{
+       char *parent, *path;
+       int ret = -E_MKDIR;
+
+       DEBUG_LOG("%s\n", p);
+       if (strcmp(p, ".") == 0 || strcmp(p, "/") == 0 || strcmp(p, "") == 0) {
+               DEBUG_LOG("reached beginning of path\n");
+               return 0;
+       }
+       path = adu_strdup(p);
+       parent = adu_dirname(p);
+       if (!parent)
+               goto out;
+       ret = mkpath(parent, mode);
+       if (ret < 0)
+               goto out;
+       INFO_LOG("making dir %s\n", path);
+       ret = 0;
+       if ((mkdir(path, mode) == -1) && (errno != EEXIST))
+               ret = -E_MKDIR;
+out:
+       free(parent);
+       free(path);
+       return ret;
+}