diff options
Diffstat (limited to 'src/common/pathname.c')
-rw-r--r-- | src/common/pathname.c | 83 |
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); + +} |