summaryrefslogtreecommitdiff
path: root/src/common/pathname.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2015-12-01 21:15:16 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2015-12-01 21:15:16 (GMT)
commit9b0389d37851ddfe36bb872063218aeaaa4ff383 (patch)
treeda29330a70a9c6ea97a34d356fd710c705a8b8fa /src/common/pathname.c
parent106e06d33196ca124d6d27cc00a5898d6f96121d (diff)
Moved the mkpath() function to a proper location.
Diffstat (limited to 'src/common/pathname.c')
-rw-r--r--src/common/pathname.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/common/pathname.c b/src/common/pathname.c
index 181fd1f..3a68599 100644
--- a/src/common/pathname.c
+++ b/src/common/pathname.c
@@ -28,6 +28,7 @@
#include <glib.h>
#include <malloc.h>
#include <string.h>
+#include <sys/stat.h>
#include "extstr.h"
@@ -171,3 +172,85 @@ char *build_absolute_filename(const char *ref, const char *target)
return result;
}
+
+
+/******************************************************************************
+* *
+* Paramètres : path = chemin d'accès avec répertoires. *
+* *
+* Description : S'assure que le chemin fourni est bien en place. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool mkpath(const char *path)
+{
+ char tmp[PATH_MAX]; /* Recopie de travail */
+ size_t len; /* Taille du chemin fourni */
+ char *iter; /* Boucle de parcours */
+ struct stat info; /* Information sur l'existant */
+ int ret; /* Bilan d'un appel système */
+
+ snprintf(tmp, PATH_MAX, "%s", path);
+ len = strlen(tmp);
+
+ /* Le chemin fournit ne contient que des répertoires ? */
+ if (tmp[len - 1] == G_DIR_SEPARATOR)
+ tmp[len - 1] = '\0';
+
+ /* Sinon, on supprime le dernier élément, qui est un fichier */
+ else
+ {
+ iter = strrchr(tmp, G_DIR_SEPARATOR);
+ if (iter == NULL) return true;
+
+ *iter = '\0';
+
+ }
+
+ for(iter = tmp + 1; *iter; iter++)
+ if(*iter == G_DIR_SEPARATOR)
+ {
+ *iter = '\0';
+
+ /* Analyse de l'existant */
+ if (stat(tmp, &info) == 0)
+ {
+ if (S_ISDIR(info.st_mode) == 0)
+ return false;
+ else
+ {
+ *iter = G_DIR_SEPARATOR;
+ continue;
+ }
+ }
+
+ ret = mkdir(tmp, S_IRWXU);
+ if (ret != 0)
+ {
+ perror("mkdir");
+ return false;
+ }
+
+ *iter = G_DIR_SEPARATOR;
+
+ }
+
+ /* Analyse de l'existant */
+ if (stat(tmp, &info) == 0)
+ {
+ if (S_ISDIR(info.st_mode) == 0)
+ return false;
+ else
+ return true;
+ }
+
+ ret = mkdir(tmp, S_IRWXU);
+ if (ret != 0) perror("mkdir");
+
+ return (ret == 0);
+
+}