diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2011-12-09 01:21:30 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2011-12-09 01:21:30 (GMT) |
commit | a6f7f152b62dd79ec492c0b3f51a2b5d19732d27 (patch) | |
tree | 13014b3f4eb6ca69bfdd8c9bdb5bae529f65bb69 | |
parent | bc2abb81b39dc4d80cdec75477a0adad8ed23a63 (diff) |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | src/configuration.c | 60 |
2 files changed, 57 insertions, 8 deletions
@@ -1,3 +1,8 @@ +11-12-09 Cyrille Bagard <nocbos@gmail.com> + + * src/configuration.c: + Fix an old bug: ensure the path used for configuration does exist before saving it. + 11-11-30 Cyrille Bagard <nocbos@gmail.com> * 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 <libgen.h> #include <malloc.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <sys/stat.h> #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); } |