From f1c2dde13bf1ca59a466cfed2f2d2076c06b235f Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Thu, 5 Jun 2014 12:06:47 +0100 Subject: extend file table with mkdir all and make fs backing store use it. enable fs backing store for RISC OS. --- utils/file.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/file.h | 16 +++++++++++++ utils/filepath.c | 57 ------------------------------------------- utils/filepath.h | 8 ------- 4 files changed, 89 insertions(+), 65 deletions(-) (limited to 'utils') diff --git a/utils/file.c b/utils/file.c index a2f1e94d0..14441e00e 100644 --- a/utils/file.c +++ b/utils/file.c @@ -23,6 +23,9 @@ #include #include #include +#include +#include +#include #include "desktop/gui_factory.h" @@ -194,6 +197,69 @@ static nserror posix_path_to_nsurl(const char *path, struct nsurl **url_out) return ret; } +/** + * 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. + */ +static nserror posix_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 (nsmkdir(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; +} + /** * default to using the posix file handling */ @@ -202,6 +268,7 @@ static struct gui_file_table file_table = { .basename = posix_basename, .nsurl_to_path = posix_nsurl_to_path, .path_to_nsurl = posix_path_to_nsurl, + .mkdir_all = posix_mkdir_all, }; struct gui_file_table *default_file_table = &file_table; @@ -230,3 +297,9 @@ nserror netsurf_path_to_nsurl(const char *path, struct nsurl **url) { return guit->file->path_to_nsurl(path, url); } + +/* exported interface documented in utils/file.h */ +nserror netsurf_mkdir_all(const char *fname) +{ + return guit->file->mkdir_all(fname); +} diff --git a/utils/file.h b/utils/file.h index 2e47e1fa1..7baf2f019 100644 --- a/utils/file.h +++ b/utils/file.h @@ -106,6 +106,14 @@ struct gui_file_table { * code on faliure. */ nserror (*path_to_nsurl)(const char *path, struct nsurl **url); + + /** + * Ensure that all directory elements needed to store a filename exist. + * + * @param[in] fname The filename to ensure the path to exists. + * @return NSERROR_OK on success or error code on failure. + */ + nserror (*mkdir_all)(const char *fname); }; /** Default (posix) file operation table. */ @@ -156,4 +164,12 @@ nserror netsurf_nsurl_to_path(struct nsurl *url, char **path_out); */ nserror netsurf_path_to_nsurl(const char *path, struct nsurl **url); +/** + * 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 netsurf_mkdir_all(const char *fname); + #endif diff --git a/utils/filepath.c b/utils/filepath.c index c28a821d7..c07cc4874 100644 --- a/utils/filepath.c +++ b/utils/filepath.c @@ -325,60 +325,3 @@ void filepath_free_strvec(char **pathv) free(pathv); } -/* 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 (nsmkdir(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 61b7209cc..ad077e2ce 100644 --- a/utils/filepath.h +++ b/utils/filepath.h @@ -127,13 +127,5 @@ char **filepath_path_to_strvec(const char *path); void filepath_free_strvec(char **pathv); -/** - * 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