From 21788df2799eb8976c1c68cd84abf0ffe92a7a45 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Fri, 28 Jun 2024 18:42:56 +0200 Subject: Handle the XDG directories. --- plugins/pychrysalide/common/Makefile.am | 3 +- plugins/pychrysalide/common/module.c | 2 + plugins/pychrysalide/common/xdg.c | 385 ++++++++++++++++++++++++++++++++ plugins/pychrysalide/common/xdg.h | 39 ++++ src/common/xdg.c | 338 +++++++++++++++++++++++++--- src/common/xdg.h | 19 +- src/plugins/plugin.c | 2 +- tests/common/xdg.py | 71 ++++++ 8 files changed, 822 insertions(+), 37 deletions(-) create mode 100644 plugins/pychrysalide/common/xdg.c create mode 100644 plugins/pychrysalide/common/xdg.h create mode 100644 tests/common/xdg.py diff --git a/plugins/pychrysalide/common/Makefile.am b/plugins/pychrysalide/common/Makefile.am index 199ef43..cc87a82 100644 --- a/plugins/pychrysalide/common/Makefile.am +++ b/plugins/pychrysalide/common/Makefile.am @@ -14,7 +14,8 @@ noinst_LTLIBRARIES = libpychrysacommon.la libpychrysacommon_la_SOURCES = \ bits.h bits.c \ - module.h module.c + module.h module.c \ + xdg.h xdg.c libpychrysacommon_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT diff --git a/plugins/pychrysalide/common/module.c b/plugins/pychrysalide/common/module.c index 5fc1135..7af12ba 100644 --- a/plugins/pychrysalide/common/module.c +++ b/plugins/pychrysalide/common/module.c @@ -33,6 +33,7 @@ //#include "packed.h" //#include "pathname.h" //#include "pearson.h" +#include "xdg.h" #include "../helpers.h" @@ -106,6 +107,7 @@ bool populate_common_module(void) if (result) result = populate_common_module_with_pathname(); if (result) result = populate_common_module_with_pearson(); */ + if (result) result = populate_common_module_with_xdg(); if (result) result = ensure_python_bitfield_is_registered(); //if (result) result = ensure_python_packed_buffer_is_registered(); diff --git a/plugins/pychrysalide/common/xdg.c b/plugins/pychrysalide/common/xdg.c new file mode 100644 index 0000000..789a0a6 --- /dev/null +++ b/plugins/pychrysalide/common/xdg.c @@ -0,0 +1,385 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * xdg.c - équivalent Python du fichier "common/xdg.c" + * + * Copyright (C) 2024 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * Chrysalide is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Chrysalide is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "xdg.h" + + +#include +#include + + +#include + + +#include "../access.h" +#include "../helpers.h" + + + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_cache_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_config_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_data_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_state_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire éphémère XDG. */ +static PyObject *py_xdg_get_xdg_runtime_dir(PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_cache_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_CACHE_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_cache_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_CACHE_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to cache according to the XDG specifications, or" \ + " *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_cache_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_config_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_CONFIG_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_config_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_CONFIG_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to configuration according to the XDG specifications,"\ + " or *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_config_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_data_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_DATA_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_data_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_DATA_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to data according to the XDG specifications, or" \ + " *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_data_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_state_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_STATE_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_state_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_STATE_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to states according to the XDG specifications, or" \ + " *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_state_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire éphémère XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_runtime_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_RUNTIME_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_runtime_dir, "suffix", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_RUNTIME_DIR* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to runtime data according to the XDG specifications," \ + " or *None* in case of failure." \ +) + + ret = PyArg_ParseTuple(args, "s", &suffix); + if (!ret) return NULL; + + filename = get_xdg_runtime_dir(suffix); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Définit une extension du module 'common' à compléter. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool populate_common_module_with_xdg(void) +{ + bool result; /* Bilan à retourner */ + PyObject *module; /* Module à recompléter */ + + static PyMethodDef py_xdg_methods[] = { + GET_XDG_CACHE_DIR_METHOD, + GET_XDG_CONFIG_DIR_METHOD, + GET_XDG_DATA_DIR_METHOD, + GET_XDG_STATE_DIR_METHOD, + GET_XDG_RUNTIME_DIR_METHOD, + { NULL } + }; + + module = get_access_to_python_module("pychrysalide.common"); + + result = register_python_module_methods(module, py_xdg_methods); + + return result; + +} diff --git a/plugins/pychrysalide/common/xdg.h b/plugins/pychrysalide/common/xdg.h new file mode 100644 index 0000000..f1aa16a --- /dev/null +++ b/plugins/pychrysalide/common/xdg.h @@ -0,0 +1,39 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * xdg.h - prototypes pour l'équivalent Python du fichier "common/xdg.c" + * + * Copyright (C) 2024 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * Chrysalide is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Chrysalide is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_COMMON_XDG_H +#define _PLUGINS_PYCHRYSALIDE_COMMON_XDG_H + + +#include +#include + + + +/* Définit une extension du module 'common' à compléter. */ +bool populate_common_module_with_xdg(void); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_COMMON_XDG_H */ diff --git a/src/common/xdg.c b/src/common/xdg.c index cabff75..891b6ae 100644 --- a/src/common/xdg.c +++ b/src/common/xdg.c @@ -24,20 +24,41 @@ #include "xdg.h" -#include -#include +#include #include #include -#include #include +#include + + +#include "pathname.h" + + + +/** + * Cf. https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + */ + +/* $HOME/.cache */ +#define CACHE_HOME_SUFFIX ".cache" G_DIR_SEPARATOR_S + +/* $HOME/.config */ +#define CONFIG_HOME_SUFFIX ".config" G_DIR_SEPARATOR_S + +/* $HOME/.local/share */ +#define DATA_HOME_SUFFIX ".local" G_DIR_SEPARATOR_S "share" G_DIR_SEPARATOR_S + +/* $HOME/.local/state */ +#define STATE_HOME_SUFFIX ".local" G_DIR_SEPARATOR_S "state" G_DIR_SEPARATOR_S /****************************************************************************** * * * Paramètres : suffix = élément visé dans le répertoire de configuration. * +* create = assure la mise en place du répertoire final. * * * -* Description : Détermine le chemin d'un répertoire selon les specs. XDG. * +* Description : Détermine le chemin d'un répertoire de données XDG. * * * * Retour : Chemin d'accès aux configurations personnelles ou NULL. * * * @@ -45,70 +66,321 @@ * * ******************************************************************************/ -char *get_xdg_config_dir(const char *suffix) +char *get_xdg_cache_dir(const char *suffix, bool create) { char *result; /* Chemin d'accès à renvoyer */ const char *env; /* Valeur de l'environnement */ - DIR *directory; /* Répertoire avec contenu ? */ - struct dirent *entry; /* Elément de répertoire */ + int ret; /* Bilan d'une assurance */ + + assert(suffix[0] != G_DIR_SEPARATOR); result = NULL; - env = getenv("XDG_CONFIG_HOME"); + env = getenv("XDG_CACHE_HOME"); if (env != NULL && env[0] != '\0') { - directory = opendir(env); - if (directory == NULL) goto default_cfg_dir; + result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char)); + + strcpy(result, env); + + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); + + strcat(result, suffix); + + } + + else + { + env = getenv("HOME"); + if (env == NULL || env[0] == '\0') goto no_env; + + result = calloc(strlen(env) + 1 + strlen(CACHE_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char)); + + strcpy(result, env); + + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); + + strcat(result, CACHE_HOME_SUFFIX); + strcat(result, suffix); + + } + + if (create) + { + ret = ensure_path_exists(result); - while (1) + if (ret != 0) { - errno = 0; + free(result); + result = NULL; + } + + } + + no_env: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : suffix = élément visé dans le répertoire de configuration. * +* create = assure la mise en place du répertoire final. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou NULL. * +* * +* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. * +* * +******************************************************************************/ - entry = readdir(directory); +char *get_xdg_config_dir(const char *suffix, bool create) +{ + char *result; /* Chemin d'accès à renvoyer */ + const char *env; /* Valeur de l'environnement */ + int ret; /* Bilan d'une assurance */ + + assert(suffix[0] != G_DIR_SEPARATOR); - if (entry == NULL) - { - if (errno != 0) - perror("readdir"); + result = NULL; + + env = getenv("XDG_CONFIG_HOME"); - break; + if (env != NULL && env[0] != '\0') + { + result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char)); - } + strcpy(result, env); - if (strcmp(entry->d_name, ".") == 0) continue; - if (strcmp(entry->d_name, "..") == 0) continue; + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); - result = calloc(strlen(env) + 2 + strlen(suffix) + 1, sizeof(char)); - strcpy(result, env); + strcat(result, suffix); - if (env[strlen(env) - 1] != G_DIR_SEPARATOR) - strcat(result, G_DIR_SEPARATOR_S); + } + + else + { + env = getenv("HOME"); + if (env == NULL || env[0] == '\0') goto no_env; + + result = calloc(strlen(env) + 1 + strlen(CONFIG_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char)); + + strcpy(result, env); + + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); + + strcat(result, CONFIG_HOME_SUFFIX); + strcat(result, suffix); + + } - strcat(result, "."); - strcat(result, suffix); + if (create) + { + ret = ensure_path_exists(result); + if (ret != 0) + { + free(result); + result = NULL; } - closedir(directory); + } + + no_env: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : suffix = élément visé dans le répertoire de configuration. * +* create = assure la mise en place du répertoire final. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou NULL. * +* * +* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. * +* * +******************************************************************************/ + +char *get_xdg_data_dir(const char *suffix, bool create) +{ + char *result; /* Chemin d'accès à renvoyer */ + const char *env; /* Valeur de l'environnement */ + int ret; /* Bilan d'une assurance */ + + assert(suffix[0] != G_DIR_SEPARATOR); + + result = NULL; + + env = getenv("XDG_DATA_HOME"); + + if (env != NULL && env[0] != '\0') + { + result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char)); + + strcpy(result, env); + + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); + + strcat(result, suffix); + + } + + else + { + env = getenv("HOME"); + if (env == NULL || env[0] == '\0') goto no_env; + + result = calloc(strlen(env) + 1 + strlen(DATA_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char)); + + strcpy(result, env); + + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); + + strcat(result, DATA_HOME_SUFFIX); + strcat(result, suffix); + + } + + if (create) + { + ret = ensure_path_exists(result); + + if (ret != 0) + { + free(result); + result = NULL; + } } - default_cfg_dir: + no_env: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : suffix = élément visé dans le répertoire de configuration. * +* create = assure la mise en place du répertoire final. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou NULL. * +* * +* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. * +* * +******************************************************************************/ + +char *get_xdg_state_dir(const char *suffix, bool create) +{ + char *result; /* Chemin d'accès à renvoyer */ + const char *env; /* Valeur de l'environnement */ + int ret; /* Bilan d'une assurance */ + + assert(suffix[0] != G_DIR_SEPARATOR); + + result = NULL; + + env = getenv("XDG_STATE_HOME"); + + if (env != NULL && env[0] != '\0') + { + result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char)); + + strcpy(result, env); + + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); - if (result == NULL) + strcat(result, suffix); + + } + + else { env = getenv("HOME"); - if (env == NULL || env[0] == '\0') return NULL; + if (env == NULL || env[0] == '\0') goto no_env; + + result = calloc(strlen(env) + 1 + strlen(STATE_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char)); + + strcpy(result, env); + + if (env[strlen(env) - 1] != G_DIR_SEPARATOR) + strcat(result, G_DIR_SEPARATOR_S); + + strcat(result, STATE_HOME_SUFFIX); + strcat(result, suffix); + + } + + if (create) + { + ret = ensure_path_exists(result); + + if (ret != 0) + { + free(result); + result = NULL; + } + + } + + no_env: + + return result; + +} + - result = calloc(strlen(env) + 1 + strlen(".config" G_DIR_SEPARATOR_S) + strlen(suffix) + 1, sizeof(char)); +/****************************************************************************** +* * +* Paramètres : suffix = élément visé dans le répertoire de configuration. * +* * +* Description : Détermine le chemin d'un répertoire éphémère XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou NULL. * +* * +* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. * +* * +******************************************************************************/ + +char *get_xdg_runtime_dir(const char *suffix) +{ + char *result; /* Chemin d'accès à renvoyer */ + const char *env; /* Valeur de l'environnement */ + + assert(suffix[0] != G_DIR_SEPARATOR); + + result = NULL; + + env = getenv("XDG_RUNTIME_DIR"); + + if (env != NULL && env[0] != '\0') + { + result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char)); strcpy(result, env); if (env[strlen(env) - 1] != G_DIR_SEPARATOR) strcat(result, G_DIR_SEPARATOR_S); - strcat(result, ".config" G_DIR_SEPARATOR_S); strcat(result, suffix); } diff --git a/src/common/xdg.h b/src/common/xdg.h index c9c2327..a6cd91d 100644 --- a/src/common/xdg.h +++ b/src/common/xdg.h @@ -25,9 +25,24 @@ #define _COMMON_XDG_H +#include -/* Détermine le chemin d'un répertoire selon les specs. XDG. */ -char *get_xdg_config_dir(const char *); + + +/* Détermine le chemin d'un répertoire de données XDG. */ +char *get_xdg_cache_dir(const char *, bool); + +/* Détermine le chemin d'un répertoire de données XDG. */ +char *get_xdg_config_dir(const char *, bool); + +/* Détermine le chemin d'un répertoire de données XDG. */ +char *get_xdg_data_dir(const char *, bool); + +/* Détermine le chemin d'un répertoire de données XDG. */ +char *get_xdg_state_dir(const char *, bool); + +/* Détermine le chemin d'un répertoire éphémère XDG. */ +char *get_xdg_runtime_dir(const char *); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 1dc20e8..75cf4e0 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -1203,7 +1203,7 @@ char *g_plugin_module_build_config_filename(const GPluginModule *plugin, const c suffix = stradd(suffix, G_DIR_SEPARATOR_S); suffix = stradd(suffix, final); - result = get_xdg_config_dir(suffix); + result = get_xdg_config_dir(suffix, true); free(suffix); free(modname); diff --git a/tests/common/xdg.py b/tests/common/xdg.py new file mode 100644 index 0000000..df03c3c --- /dev/null +++ b/tests/common/xdg.py @@ -0,0 +1,71 @@ + +import os + +from chrysacase import ChrysalideTestCase +from pychrysalide.common import get_xdg_cache_dir, get_xdg_config_dir, get_xdg_data_dir, \ + get_xdg_state_dir, get_xdg_runtime_dir + + +class TestXDG(ChrysalideTestCase): + """TestCase for XDG directories.""" + + def testXDGCachePath(self): + """Retrieve the XDG cache directory.""" + + filename = get_xdg_cache_dir('test.txt', False) + + self.assertIsNotNone(filename) + self.assertTrue(filename.startswith(os.sep)) + self.assertTrue(filename.endswith('test.txt')) + + # Depends on current configuration + self.assertTrue('.cache' in filename) + + + def testXDGConfigPath(self): + """Retrieve the XDG config directory.""" + + filename = get_xdg_config_dir('test.txt', False) + + self.assertIsNotNone(filename) + self.assertTrue(filename.startswith(os.sep)) + self.assertTrue(filename.endswith('test.txt')) + + # Depends on current configuration + self.assertTrue('.config' in filename) + + + def testXDGDataPath(self): + """Retrieve the XDG data directory.""" + + filename = get_xdg_data_dir('test.txt', False) + + self.assertIsNotNone(filename) + self.assertTrue(filename.startswith(os.sep)) + self.assertTrue(filename.endswith('test.txt')) + + # Depends on current configuration + self.assertTrue(os.path.join('.local', 'share') in filename) + + + def testXDGStatePath(self): + """Retrieve the XDG state directory.""" + + filename = get_xdg_state_dir('test.txt', False) + + self.assertIsNotNone(filename) + self.assertTrue(filename.startswith(os.sep)) + self.assertTrue(filename.endswith('test.txt')) + + # Depends on current configuration + self.assertTrue(os.path.join('.local', 'state') in filename) + + + def testXDGRuntimePath(self): + """Retrieve the XDG runtime directory.""" + + filename = get_xdg_runtime_dir('test.txt') + + self.assertIsNotNone(filename) + self.assertTrue(filename.startswith(os.sep)) + self.assertTrue(filename.endswith('test.txt')) -- cgit v0.11.2-87-g4458