From a6f7f152b62dd79ec492c0b3f51a2b5d19732d27 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Fri, 9 Dec 2011 01:21:30 +0000 Subject: Fixed an old bug: ensure the path used for configuration does exist before saving it. git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@215 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a --- ChangeLog | 5 +++++ src/configuration.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0d4062b..8ca6ee2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +11-12-09 Cyrille Bagard + + * src/configuration.c: + Fix an old bug: ensure the path used for configuration does exist before saving it. + 11-11-30 Cyrille Bagard * po/fr.po: diff --git a/src/configuration.c b/src/configuration.c index 5d17072..1ff3353 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -24,10 +24,12 @@ #include "configuration.h" +#include #include #include #include #include +#include #include "xdg.h" @@ -50,6 +52,10 @@ struct _configuration }; +/* S'assure qu'un chemin donné existe dans le système. */ +static int ensure_path_exists(const char *); + + /****************************************************************************** * * @@ -74,16 +80,12 @@ configuration *load_configuration(const char *name, config_param *params, unsign result = (configuration *)calloc(1, sizeof(configuration)); - printf("reset...\n"); - for (i = 0; i < count; i++) params[i].cur = params[i].def; result->params = params; result->count = count; - printf("init done\n"); - suffix = strdup("openida/"); suffix = stradd(suffix, name); suffix = stradd(suffix, ".xml"); @@ -118,7 +120,45 @@ configuration *load_configuration(const char *name, config_param *params, unsign break; } - printf("loaded\n"); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : path = chemin d'accès à valider. * +* * +* Description : S'assure qu'un chemin donné existe dans le système. * +* * +* Retour : 0 si le chemin est actuellement présent, -1 sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int ensure_path_exists(const char *path) +{ + int result; /* Bilan de l'assurance */ + char *tmp; /* Chemin altérable */ + + tmp = strdup(path); + tmp = dirname(tmp); + + result = access(tmp, W_OK | X_OK); + + if (result != 0) + { + result = ensure_path_exists(tmp); + + if (result == 0) + result = mkdir(tmp, 0700); + + } + + free(tmp); + return result; } @@ -138,14 +178,16 @@ configuration *load_configuration(const char *name, config_param *params, unsign void unload_configuration(configuration *config) { + int ret; /* Bilan de l'assurance */ unsigned int i; /* Boucle de parcours */ config_param *params; /* Confort d'utilisation */ - printf("Writing '%s'\n", config->filename); - close_xml_file(config->xdoc, config->context); - create_new_xml_file(&config->xdoc, &config->context); + ret = ensure_path_exists(config->filename); + if (ret != 0) goto uc_exit; + + create_new_xml_file(&config->xdoc, &config->context); for (i = 0; i < config->count; i++) { @@ -187,6 +229,8 @@ void unload_configuration(configuration *config) save_xml_file(config->xdoc, config->filename); + uc_exit: + free(config); } -- cgit v0.11.2-87-g4458