From ef427fa2c16139b2d6790b8867d5bdb6307fd14d Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 25 May 2020 23:57:24 -0400 Subject: Add strlist_append_file() * Add reader_strlist_append_file() as default reader function --- lib/strlist.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 10 deletions(-) (limited to 'lib/strlist.c') diff --git a/lib/strlist.c b/lib/strlist.c index aac2755..c2cff09 100644 --- a/lib/strlist.c +++ b/lib/strlist.c @@ -4,6 +4,7 @@ */ #include "spm.h" #include "strlist.h" +#include "url.h" /** * @@ -46,21 +47,87 @@ void strlist_append(StrList *pStrList, char *str) { pStrList->num_alloc++; } +static int reader_strlist_append_file(size_t lineno, char **line) { + (void)(lineno); // unused parameter + (void)(line); // unused parameter + return 0; +} + /** - * Produce a new copy of a `StrList` - * @param pStrList `StrList` - * @return `StrList` copy + * Append lines from a local file or remote URL (HTTP/s only) + * @param pStrList + * @param path file path or HTTP/s address + * @param readerFn pointer to a reader function (use NULL to retrieve all data) + * @return 0=success -1=error (spmerrno set) */ -StrList *strlist_copy(StrList *pStrList) { - StrList *result = strlist_init(); - if (pStrList == NULL || result == NULL) { - return NULL; +int strlist_append_file(StrList *pStrList, char *path, ReaderFn *readerFn) { + int is_remote = 0; + char *filename = NULL; + char *from_file_tmpdir = NULL; + char **from_file = NULL; + char *fetched = NULL; + + if (readerFn == NULL) { + readerFn = reader_strlist_append_file; } - for (size_t i = 0; i < strlist_count(pStrList); i++) { - strlist_append(result, strlist_item(pStrList, i)); + filename = calloc(PATH_MAX, sizeof(char)); + if (filename == NULL) { + spmerrno = errno; + return -1; } - return result; + + filename = expandpath(path); + + is_remote = (startswith(path, "http") || startswith(path, "https")); + if (is_remote) { + long response = 0; + from_file_tmpdir = spm_mkdtemp(TMP_DIR, __FUNCTION__, NULL); + + if (from_file_tmpdir == NULL) { + spmerrno = errno; + spmerrno_cause("file_from temp directory"); + return -1; + } + + fetched = join((char *[]){from_file_tmpdir, basename(path), NULL}, DIRSEPS); + if ((response = fetch(path, fetched)) >= 400) { + spmerrno = SPM_ERR_FETCH; + char cause[PATH_MAX]; + sprintf(cause, "HTTP(%ld): %s: %s", response, http_response_str(response), path); + spmerrno_cause(cause); + return -1; + } + + strcpy(filename, fetched); + } + + if (exists(filename) != 0) { + spmerrno = errno; + spmerrno_cause(filename); + return -1; + } + + from_file = file_readlines(filename, 0, 0, readerFn); + if (from_file == NULL) { + spmerrno = errno; + spmerrno_cause(filename); + return -1; + } + + for (size_t record = 0; from_file[record] != NULL; record++) { + strlist_append(pStrList, from_file[record]); + free(from_file[record]); + } + free(from_file); + + if (from_file_tmpdir) { + rmdirs(from_file_tmpdir); + } + free(from_file_tmpdir); + free(filename); + + return 0; } /** @@ -82,6 +149,23 @@ void strlist_append_strlist(StrList *pStrList1, StrList *pStrList2) { } } +/** + * Produce a new copy of a `StrList` + * @param pStrList `StrList` + * @return `StrList` copy + */ +StrList *strlist_copy(StrList *pStrList) { + StrList *result = strlist_init(); + if (pStrList == NULL || result == NULL) { + return NULL; + } + + for (size_t i = 0; i < strlist_count(pStrList); i++) { + strlist_append(result, strlist_item(pStrList, i)); + } + return result; +} + /** * Remove a record by index from a `StrList` * @param pStrList -- cgit From 629ab111362a361729a8c2281c7168eb13729a77 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 26 May 2020 00:19:06 -0400 Subject: Make a copy of the path --- lib/strlist.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib/strlist.c') diff --git a/lib/strlist.c b/lib/strlist.c index c2cff09..0de0d2a 100644 --- a/lib/strlist.c +++ b/lib/strlist.c @@ -60,8 +60,9 @@ static int reader_strlist_append_file(size_t lineno, char **line) { * @param readerFn pointer to a reader function (use NULL to retrieve all data) * @return 0=success -1=error (spmerrno set) */ -int strlist_append_file(StrList *pStrList, char *path, ReaderFn *readerFn) { +int strlist_append_file(StrList *pStrList, char *_path, ReaderFn *readerFn) { int is_remote = 0; + char *path = NULL; char *filename = NULL; char *from_file_tmpdir = NULL; char **from_file = NULL; @@ -71,6 +72,12 @@ int strlist_append_file(StrList *pStrList, char *path, ReaderFn *readerFn) { readerFn = reader_strlist_append_file; } + path = strdup(_path); + if (path == NULL) { + spmerrno = errno; + return -1; + } + filename = calloc(PATH_MAX, sizeof(char)); if (filename == NULL) { spmerrno = errno; @@ -99,7 +106,7 @@ int strlist_append_file(StrList *pStrList, char *path, ReaderFn *readerFn) { return -1; } - strcpy(filename, fetched); + strncpy(filename, fetched, PATH_MAX - 1); } if (exists(filename) != 0) { -- cgit From 9bc167d23a3f738057185dc8a6412132c7c47c3e Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 26 May 2020 13:53:44 -0400 Subject: Errors set return value and jump to end of function --- lib/strlist.c | 66 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) (limited to 'lib/strlist.c') diff --git a/lib/strlist.c b/lib/strlist.c index 0de0d2a..f90cce4 100644 --- a/lib/strlist.c +++ b/lib/strlist.c @@ -58,15 +58,15 @@ static int reader_strlist_append_file(size_t lineno, char **line) { * @param pStrList * @param path file path or HTTP/s address * @param readerFn pointer to a reader function (use NULL to retrieve all data) - * @return 0=success -1=error (spmerrno set) + * @return 0=success 1=no data, -1=error (spmerrno set) */ int strlist_append_file(StrList *pStrList, char *_path, ReaderFn *readerFn) { + int retval = 0; int is_remote = 0; char *path = NULL; char *filename = NULL; char *from_file_tmpdir = NULL; - char **from_file = NULL; - char *fetched = NULL; + char **data = NULL; if (readerFn == NULL) { readerFn = reader_strlist_append_file; @@ -75,26 +75,21 @@ int strlist_append_file(StrList *pStrList, char *_path, ReaderFn *readerFn) { path = strdup(_path); if (path == NULL) { spmerrno = errno; - return -1; - } - - filename = calloc(PATH_MAX, sizeof(char)); - if (filename == NULL) { - spmerrno = errno; - return -1; + retval = -1; + goto fatal; } - filename = expandpath(path); - is_remote = (startswith(path, "http") || startswith(path, "https")); if (is_remote) { long response = 0; + char *fetched = NULL; from_file_tmpdir = spm_mkdtemp(TMP_DIR, __FUNCTION__, NULL); if (from_file_tmpdir == NULL) { spmerrno = errno; spmerrno_cause("file_from temp directory"); - return -1; + retval = -1; + goto fatal; } fetched = join((char *[]){from_file_tmpdir, basename(path), NULL}, DIRSEPS); @@ -103,38 +98,47 @@ int strlist_append_file(StrList *pStrList, char *_path, ReaderFn *readerFn) { char cause[PATH_MAX]; sprintf(cause, "HTTP(%ld): %s: %s", response, http_response_str(response), path); spmerrno_cause(cause); - return -1; + retval = -1; + goto fatal; } - strncpy(filename, fetched, PATH_MAX - 1); + filename = strdup(fetched); + free(fetched); + } else { + filename = expandpath(path); } - if (exists(filename) != 0) { + if (filename == NULL) { spmerrno = errno; - spmerrno_cause(filename); - return -1; + retval = -1; + goto fatal; } - from_file = file_readlines(filename, 0, 0, readerFn); - if (from_file == NULL) { - spmerrno = errno; - spmerrno_cause(filename); - return -1; + data = file_readlines(filename, 0, 0, readerFn); + if (data == NULL) { + retval = 1; + goto fatal; } - for (size_t record = 0; from_file[record] != NULL; record++) { - strlist_append(pStrList, from_file[record]); - free(from_file[record]); + for (size_t record = 0; data[record] != NULL; record++) { + strlist_append(pStrList, data[record]); + free(data[record]); } - free(from_file); + free(data); - if (from_file_tmpdir) { +fatal: + if (from_file_tmpdir != NULL) { rmdirs(from_file_tmpdir); + free(from_file_tmpdir); + } + if (filename != NULL) { + free(filename); + } + if (path != NULL) { + free(path); } - free(from_file_tmpdir); - free(filename); - return 0; + return retval; } /** -- cgit