From b9e9e8e2196e0081d4814c50cdd02c18911bcf29 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 8 Jul 2013 13:26:40 +0200 Subject: [PATCH] xml_include fails if file is not found YAZ-668 Unless glob pattern is used. --- src/file_glob.c | 24 +++++++++---- src/xml_include.c | 3 +- test/test_xml_include.c | 89 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 106 insertions(+), 10 deletions(-) diff --git a/src/file_glob.c b/src/file_glob.c index fd6ae0b..3a5def2 100644 --- a/src/file_glob.c +++ b/src/file_glob.c @@ -36,6 +36,17 @@ struct glob_res { struct res_entry *entries; }; +static void add_entry(yaz_glob_res_t res, const char *str) +{ + struct res_entry *ent = + nmem_malloc(res->nmem, sizeof(*ent)); + ent->file = nmem_strdup(res->nmem, str); + ent->next = 0; + *res->last_entry = ent; + res->last_entry = &ent->next; + res->number_of_entries++; +} + static void glob_r(yaz_glob_res_t res, const char *pattern, size_t off, char *prefix) { @@ -57,6 +68,11 @@ static void glob_r(yaz_glob_res_t res, const char *pattern, size_t off, glob_r(res, pattern, i, prefix); prefix[prefix_len] = '\0'; } + else if (!is_pattern && !pattern[i]) + { + strcpy(prefix + prefix_len, pattern + off); + add_entry(res, prefix); + } else { DIR * dir = opendir(*prefix ? prefix : "." ); @@ -82,13 +98,7 @@ static void glob_r(yaz_glob_res_t res, const char *pattern, size_t off, } else { - struct res_entry *ent = - nmem_malloc(res->nmem, sizeof(*ent)); - ent->file = nmem_strdup(res->nmem, prefix); - ent->next = 0; - *res->last_entry = ent; - res->last_entry = &ent->next; - res->number_of_entries++; + add_entry(res, prefix); } prefix[prefix_len] = '\0'; } diff --git a/src/xml_include.c b/src/xml_include.c index 41005dc..00357a8 100644 --- a/src/xml_include.c +++ b/src/xml_include.c @@ -102,7 +102,8 @@ static int config_include_src(yaz_xml_include_t config, xmlNode **np, yaz_glob_res_t glob_res; glob_ret = yaz_file_glob(wrbuf_cstr(w), &glob_res); - + yaz_log(YLOG_LOG, "yaz_file_glob returned w=%s %d", wrbuf_cstr(w), + glob_ret); if (glob_ret == 0) { size_t i; diff --git a/test/test_xml_include.c b/test/test_xml_include.c index 3e9b34d..7d45f45 100644 --- a/test/test_xml_include.c +++ b/test/test_xml_include.c @@ -13,7 +13,7 @@ #include #include -static void tst_xml_include(void) +static void tst_xml_include1(void) { #if YAZ_HAVE_XML2 const char *srcdir = getenv("srcdir"); @@ -63,12 +63,97 @@ static void tst_xml_include(void) #endif } +static void tst_xml_include2(void) +{ +#if YAZ_HAVE_XML2 + const char *srcdir = getenv("srcdir"); + xmlDocPtr doc; + xmlNodePtr node; + const char *xml_in = ""; + + if (srcdir == 0) + srcdir = "."; + + doc = xmlParseMemory(xml_in, strlen(xml_in)); + YAZ_CHECK(doc); + if (!doc) + return; + node = xmlDocGetRootElement(doc); + YAZ_CHECK(node); + if (node) + { + const char *expect = + "\n" + "" + "\n"; + + xmlChar *xml_out; + int len_out; + int ret = yaz_xml_include_simple(node, srcdir); + YAZ_CHECK(ret == -1); + } + xmlFreeDoc(doc); +#endif +} + +static void tst_xml_include3(void) +{ +#if YAZ_HAVE_XML2 + const char *srcdir = getenv("srcdir"); + xmlDocPtr doc; + xmlNodePtr node; + const char *xml_in = ""; + + if (srcdir == 0) + srcdir = "."; + + doc = xmlParseMemory(xml_in, strlen(xml_in)); + YAZ_CHECK(doc); + if (!doc) + return; + node = xmlDocGetRootElement(doc); + YAZ_CHECK(node); + if (node) + { + const char *expect = + "\n" + "" + "\n"; + + xmlChar *xml_out; + int len_out; + int ret = yaz_xml_include_simple(node, srcdir); + YAZ_CHECK(ret == 0); + xmlDocDumpMemory(doc, &xml_out, &len_out); + YAZ_CHECK(xml_out && len_out > 0); + if (xml_out && len_out > 0) + { + YAZ_CHECK(strlen(expect) == len_out); + if (strlen(expect) == len_out) + { + YAZ_CHECK(memcmp(expect, xml_out, len_out) == 0); + } + else + { + fwrite(xml_out, 1, len_out, stdout); + fflush(stdout); + } + xmlFree(xml_out); + } + } + xmlFreeDoc(doc); +#endif +} + + int main (int argc, char **argv) { YAZ_CHECK_INIT(argc, argv); YAZ_CHECK_LOG(); - tst_xml_include(); + tst_xml_include1(); + tst_xml_include2(); + tst_xml_include3(); YAZ_CHECK_TERM; } -- 1.7.10.4