From 1fd565cba706d6a9e809d14f79ceb92633b62ead Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Mon, 28 Apr 2014 16:37:00 +0100 Subject: make GTK configuration handling conform to XDG specification. --- utils/errors.h | 6 +++- utils/filepath.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- utils/filepath.h | 55 ++++++++++++++++++++++++++++-------- 3 files changed, 132 insertions(+), 14 deletions(-) (limited to 'utils') diff --git a/utils/errors.h b/utils/errors.h index c38e4b9e2..4c02adb2d 100644 --- a/utils/errors.h +++ b/utils/errors.h @@ -37,6 +37,8 @@ typedef enum { NSERROR_NOT_FOUND, /**< Requested item not found */ + NSERROR_NOT_DIRECTORY, /**< Missing directory */ + NSERROR_SAVE_FAILED, /**< Failed to save data */ NSERROR_CLONE_FAILED, /**< Failed to clone handle */ @@ -69,7 +71,9 @@ typedef enum { NSERROR_BAD_CONTENT, /**< Bad Content */ - NSERROR_FRAME_DEPTH /**< Exceeded frame depth */ + NSERROR_FRAME_DEPTH, /**< Exceeded frame depth */ + + NSERROR_PERMISSION /**< Permission error */ } nserror; #endif diff --git a/utils/filepath.c b/utils/filepath.c index d088777e5..d82dfc627 100644 --- a/utils/filepath.c +++ b/utils/filepath.c @@ -202,7 +202,13 @@ filepath_generate(char * const *pathv, const char * const *langv) return respath; } -/* expand ${} in a string into environment variables */ +/** + * expand ${} in a string into environment variables. + * + * @param path The pathname to expand. + * @param pathlen The length of the path element. + * @return A string with the expanded path or NULL on empty expansion or error. + */ static char * expand_path(const char *path, int pathlen) { @@ -317,3 +323,80 @@ void filepath_free_strvec(char **pathv) } free(pathv); } + +/* exported interface documented in filepath.h */ +char *filepath_append(const char *path, const char *leaf) +{ + int dirname_len; + char *dirname; + + if ((path == NULL) || (leaf == NULL)) { + return NULL; + } + + dirname_len = strlen(path) + strlen(leaf) + 2; + dirname = malloc(dirname_len); + if (dirname != NULL) { + snprintf(dirname, dirname_len, "%s/%s", path, leaf); + } + + return dirname; +} + +/* exported interface documented in filepath.h */ +nserror filepath_mkdir_all(const char *fname) +{ + char *dname; + char *sep; + struct stat sb; + + dname = strdup(fname); + + sep = strrchr(dname, '/'); + if (sep == NULL) { + /* no directory separator path is just filename so its ok */ + free(dname); + return NSERROR_OK; + } + + *sep = 0; /* null terminate directory path */ + + if (stat(dname, &sb) == 0) { + free(dname); + if (S_ISDIR(sb.st_mode)) { + /* path to file exists and is a directory */ + return NSERROR_OK; + } + return NSERROR_NOT_DIRECTORY; + } + *sep = '/'; /* restore separator */ + + sep = dname; + while (*sep == '/') { + sep++; + } + while ((sep = strchr(sep, '/')) != NULL) { + *sep = 0; + if (stat(dname, &sb) != 0) { + if (mkdir(dname, S_IRWXU) != 0) { + /* could not create path element */ + free(dname); + return NSERROR_NOT_FOUND; + } + } else { + if (! S_ISDIR(sb.st_mode)) { + /* path element not a directory */ + free(dname); + return NSERROR_NOT_DIRECTORY; + } + } + *sep = '/'; /* restore separator */ + /* skip directory separators */ + while (*sep == '/') { + sep++; + } + } + + free(dname); + return NSERROR_OK; +} diff --git a/utils/filepath.h b/utils/filepath.h index ff3ebe90e..56e9eff74 100644 --- a/utils/filepath.h +++ b/utils/filepath.h @@ -16,9 +16,9 @@ * along with this program. If not, see . */ -/** - * \file utils/filepath.h - * \brief Utility routines to obtain paths to file resources. +/** + * @file utils/filepath.h + * @brief Utility routines to obtain paths to file resources. */ #ifndef _NETSURF_UTILS_FILEPATH_H_ @@ -26,8 +26,10 @@ #include +#include "utils/errors.h" -/** Create a normalised file name. +/** + * Create a normalised file name. * * If the file described by the format exists and is accessible the * normalised path is placed in str and a pointer to str returned @@ -43,14 +45,16 @@ char *filepath_vsfindfile(char *str, const char *format, va_list ap); -/** Create a normalised file name. +/** + * Create a normalised file name. * * Similar to vsfindfile but takes variadic (printf like) parameters */ char *filepath_sfindfile(char *str, const char *format, ...); -/** Create a normalised file name. +/** + * Create a normalised file name. * * Similar to sfindfile but allocates its own storage for the * returned string. The caller must free this sorage. @@ -58,7 +62,8 @@ char *filepath_sfindfile(char *str, const char *format, ...); char *filepath_findfile(const char *format, ...); -/** Searches an array of resource paths for a file. +/** + * Searches an array of resource paths for a file. * * Iterates through a vector of resource paths and returns the * normalised file name of the first acessible file or NULL if no file @@ -72,7 +77,8 @@ char *filepath_findfile(const char *format, ...); char *filepath_sfind(char **respathv, char *filepath, const char *filename); -/** Searches an array of resource paths for a file. +/** + * Searches an array of resource paths for a file. * * Similar to filepath_sfind except it allocates its own storage for * the returned string. The caller must free this sorage. @@ -80,7 +86,8 @@ char *filepath_sfind(char **respathv, char *filepath, const char *filename); char *filepath_find(char **respathv, const char *filename); -/** Searches an array of resource paths for a file optionally forcing a default. +/** + * Searches an array of resource paths for a file optionally forcing a default. * * Similar to filepath_sfind except if no resource is found the default * is used as an additional path element to search, if that still @@ -91,7 +98,8 @@ char *filepath_sfinddef(char **respathv, char *filepath, const char *filename, const char *def); -/** Merge two string vectors into a resource search path vector. +/** + * Merge two string vectors into a resource search path vector. * * @param pathv A string vector containing path elemets to scan. * @param langv A string vector containing language names to enumerate. @@ -101,7 +109,8 @@ char *filepath_sfinddef(char **respathv, char *filepath, const char *filename, char **filepath_generate(char * const *pathv, const char * const *langv); -/** Convert a colon separated list of path elements into a string vector. +/** + * Convert a colon separated list of path elements into a string vector. * * @param path A colon separated path. * @return A pointer to a NULL terminated string vector of valid @@ -110,10 +119,32 @@ char **filepath_generate(char * const *pathv, const char * const *langv); char **filepath_path_to_strvec(const char *path); -/** Free a string vector +/** + * Free a string vector. * * Free a string vector allocated by filepath_path_to_strvec */ void filepath_free_strvec(char **pathv); + +/** + * generate a new filename from a path and leaf. + * + * @param path The base path. + * @param leaf The leaf to add. + * @return The combined path in a new string must be freed by caller + * or NULL on failiure to allocte memory. + */ +char *filepath_append(const char *path, const char *leaf); + + +/** + * Ensure that all directory elements needed to store a filename exist. + * + * @param fname The filename to ensure the path to exists. + * @return NSERROR_OK on success or error code on failure. + */ +nserror filepath_mkdir_all(const char *fname); + + #endif /* _NETSURF_UTILS_FILEPATH_H_ */ -- cgit v1.2.3