From: Michael Schroeder Date: Mon, 6 Feb 2012 11:41:18 +0000 (+0100) Subject: - clean up repo parsing code X-Git-Tag: BASE-SuSE-Code-12_2-Branch~179 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;ds=sidebyside;h=8ba3467c8afc6d895939dae11f428de18ebbab6c;p=platform%2Fupstream%2Flibsolv.git - clean up repo parsing code --- diff --git a/ext/repo_comps.c b/ext/repo_comps.c index 7a100a7..536eb44 100644 --- a/ext/repo_comps.c +++ b/ext/repo_comps.c @@ -116,8 +116,6 @@ struct parsedata { Solvable *solvable; Id handle; - - Id langcache[ID_NUM_INTERNAL]; }; @@ -144,23 +142,6 @@ find_attr(const char *txt, const char **atts, int dup) /* - * create localized tag - */ - -static Id -langtag(struct parsedata *pd, Id tag, const char *language) -{ - if (language && !language[0]) - language = 0; - if (!language || tag >= ID_NUM_INTERNAL || 1) - return pool_id2langid(pd->repo->pool, tag, language, 1); - if (!pd->langcache[tag]) - pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1); - return pd->langcache[tag]; -} - - -/* * XML callback: startElement */ @@ -287,13 +268,13 @@ endElement(void *userData, const char *name) case STATE_NAME: case STATE_CNAME: - repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content); + repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content); pd->tmplang = solv_free((void *)pd->tmplang); break; case STATE_DESCRIPTION: case STATE_CDESCRIPTION: - repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content); + repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content); pd->tmplang = solv_free((void *)pd->tmplang); break; diff --git a/ext/repo_deltainfoxml.c b/ext/repo_deltainfoxml.c index 775cb01..5ca9382 100644 --- a/ext/repo_deltainfoxml.c +++ b/ext/repo_deltainfoxml.c @@ -18,6 +18,7 @@ #include "pool.h" #include "repo.h" +#include "chksum.h" #include "repo_updateinfoxml.h" #define DISABLE_SPLIT @@ -319,14 +320,10 @@ startElement(void *userData, const char *name, const char **atts) break; case STATE_NEWPACKAGE: if ((str = find_attr("name", atts)) != 0) - { - pd->newpkgname = pool_str2id(pool, str, 1); - } + pd->newpkgname = pool_str2id(pool, str, 1); pd->newpkgevr = makeevr_atts(pool, pd, atts); if ((str = find_attr("arch", atts)) != 0) - { - pd->newpkgarch = pool_str2id(pool, str, 1); - } + pd->newpkgarch = pool_str2id(pool, str, 1); break; case STATE_DELTA: @@ -348,13 +345,8 @@ startElement(void *userData, const char *name, const char **atts) pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1; if ((str = find_attr("type", atts)) != 0) { - if (!strcasecmp(str, "sha")) - pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1; - else if (!strcasecmp(str, "sha256")) - pd->delta.filechecksumtype = REPOKEY_TYPE_SHA256; - else if (!strcasecmp(str, "md5")) - pd->delta.filechecksumtype = REPOKEY_TYPE_MD5; - else + pd->delta.filechecksumtype = solv_chksum_str2type(str); + if (!pd->delta.filechecksumtype) pool_debug(pool, SOLV_ERROR, "unknown checksum type: '%s'\n", str); } case STATE_SEQUENCE: diff --git a/ext/repo_products.c b/ext/repo_products.c index cf76050..065c8a8 100644 --- a/ext/repo_products.c +++ b/ext/repo_products.c @@ -124,8 +124,6 @@ struct parsedata { ino_t baseproduct; ino_t currentproduct; int productscheme; - - Id langcache[ID_NUM_INTERNAL]; }; @@ -152,23 +150,6 @@ find_attr(const char *txt, const char **atts, int dup) /* - * create localized tag - */ - -static Id -langtag(struct parsedata *pd, Id tag, const char *language) -{ - if (language && !language[0]) - language = 0; - if (!language || tag >= ID_NUM_INTERNAL) - return pool_id2langid(pd->repo->pool, tag, language, 1); - if (!pd->langcache[tag]) - pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1); - return pd->langcache[tag]; -} - - -/* * XML callback: startElement */ @@ -323,14 +304,14 @@ endElement(void *userData, const char *name) /** obsolete **/ break; case STATE_SUMMARY: - repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content); + repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content); pd->tmplang = solv_free((void *)pd->tmplang); break; case STATE_SHORTSUMMARY: repodata_set_str(pd->data, pd->handle, PRODUCT_SHORTLABEL, pd->content); break; case STATE_DESCRIPTION: - repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content ); + repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content); pd->tmplang = solv_free((void *)pd->tmplang); break; case STATE_URL: diff --git a/ext/repo_repomdxml.c b/ext/repo_repomdxml.c index 7bff1ec..5a2bb47 100644 --- a/ext/repo_repomdxml.c +++ b/ext/repo_repomdxml.c @@ -176,7 +176,7 @@ struct parsedata { /* repo data handle */ Id rdhandle; - const char *tmpattr; + Id chksumtype; }; /* @@ -303,7 +303,15 @@ startElement(void *userData, const char *name, const char **atts) } case STATE_CHECKSUM: case STATE_OPENCHECKSUM: - pd->tmpattr= find_attr("type", atts); + { + const char *type= find_attr("type", atts); + pd->chksumtype = type && *type ? solv_chksum_str2type(type) : 0; + if (!pd->chksumtype) + { + fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), type ? type : "NULL"); + exit(1); + } + } break; default: break; @@ -347,21 +355,13 @@ endElement(void *userData, const char *name) case STATE_CHECKSUM: case STATE_OPENCHECKSUM: - { - Id type = solv_chksum_str2type(pd->tmpattr); - if (!type) - { - fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); - exit(1); - } - if (strlen(pd->content) != 2 * solv_chksum_len(type)) - { - fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); - exit(1); - } - repodata_set_checksum(pd->data, pd->rdhandle, pd->state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, type, pd->content); - break; - } + if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype)) + { + fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype)); + exit(1); + } + repodata_set_checksum(pd->data, pd->rdhandle, pd->state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, pd->chksumtype, pd->content); + break; case STATE_TIMESTAMP: { diff --git a/ext/repo_rpmmd.c b/ext/repo_rpmmd.c index 856f7c8..2dafb81 100644 --- a/ext/repo_rpmmd.c +++ b/ext/repo_rpmmd.c @@ -232,7 +232,9 @@ static struct stateswitch stateswitches[] = { #define CSREALLOC_STEP 1024 struct parsedata { - struct parsedata_common common; + Pool *pool; + Repo *repo; + Repodata *data; char *kind; int depth; enum state state; @@ -247,18 +249,13 @@ struct parsedata { enum state sbtab[NUMSTATES]; /* temporal to store attribute tag language */ const char *tmplang; - const char *capkind; - // used to store tmp attributes - // while the tag ends - const char *tmpattr; - Repodata *data; + Id chksumtype; Id handle; XML_Parser *parser; Id (*dirs)[3]; // dirid, size, nfiles int ndirs; - Id langcache[ID_NUM_INTERNAL]; - /** system language */ - const char *language; + const char *language; /* default language */ + Id langcache[ID_NUM_INTERNAL]; /* cache for the default language */ Id lastdir; char *lastdirstr; @@ -275,15 +272,18 @@ struct parsedata { static Id langtag(struct parsedata *pd, Id tag, const char *language) { - if (!language) /* fall back to default if not specified */ - language = pd->language; - if (language && (!language[0] || !strcmp(language, "en"))) - language = 0; - if (!language || tag >= ID_NUM_INTERNAL) - return pool_id2langid(pd->common.repo->pool, tag, language, 1); - return pool_id2langid(pd->common.repo->pool, tag, language, 1); + if (language) + { + if (!language[0] || !strcmp(language, "en")) + return tag; + return pool_id2langid(pd->pool, tag, language, 1); + } + if (!pd->language) + return tag; + if (tag >= ID_NUM_INTERNAL) + return pool_id2langid(pd->pool, tag, pd->language, 1); if (!pd->langcache[tag]) - pd->langcache[tag] = pool_id2langid(pd->common.repo->pool, tag, language, 1); + pd->langcache[tag] = pool_id2langid(pd->pool, tag, pd->language, 1); return pd->langcache[tag]; } @@ -520,7 +520,7 @@ adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts #if 0 fprintf(stderr, "new dep %s%s%s\n", pool_id2str(pool, d), id2rel(pool, d), id2evr(pool, d)); #endif - return repo_addid_dep(pd->common.repo, olddeps, id, marker); + return repo_addid_dep(pd->repo, olddeps, id, marker); } @@ -636,7 +636,7 @@ startElement(void *userData, const char *name, const char **atts) { //fprintf(stderr,"+tag: %s\n", name); struct parsedata *pd = userData; - Pool *pool = pd->common.pool; + Pool *pool = pd->pool; Solvable *s = pd->solvable; struct stateswitch *sw; const char *str; @@ -719,7 +719,7 @@ startElement(void *userData, const char *name, const char **atts) else { /* this is a new package */ - pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo)); + pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo)); pd->freshens = 0; } pd->handle = pd->solvable - pool->solvables; @@ -789,10 +789,10 @@ startElement(void *userData, const char *name, const char **atts) case STATE_SUMMARY: case STATE_CATEGORY: case STATE_DESCRIPTION: - pd->tmplang = find_attr("lang", atts); + pd->tmplang = solv_strdup(find_attr("lang", atts)); break; case STATE_USERVISIBLE: - repodata_set_void(pd->data, handle, SOLVABLE_ISVISIBLE ); + repodata_set_void(pd->data, handle, SOLVABLE_ISVISIBLE); break; case STATE_INCLUDESENTRY: { @@ -814,7 +814,15 @@ startElement(void *userData, const char *name, const char **atts) repodata_set_location(pd->data, handle, 0, 0, str); break; case STATE_CHECKSUM: - pd->tmpattr = find_attr("type", atts); + { + const char *tmp = find_attr("type", atts); + pd->chksumtype = tmp && *tmp ? solv_chksum_str2type(tmp) : 0; + if (!pd->chksumtype) + { + fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), tmp ? tmp: "NULL"); + exit(1); + } + } break; case STATE_TIME: { @@ -902,9 +910,9 @@ endElement(void *userData, const char *name) { //fprintf(stderr,"-tag: %s\n", name); struct parsedata *pd = userData; - Pool *pool = pd->common.pool; + Pool *pool = pd->pool; Solvable *s = pd->solvable; - Repo *repo = pd->common.repo; + Repo *repo = pd->repo; Id handle = pd->handle; Id id; char *p; @@ -964,19 +972,14 @@ endElement(void *userData, const char *name) break; case STATE_CHECKSUM: { - Id type, index; - type = solv_chksum_str2type(pd->tmpattr); - if (!type) - { - fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); - exit(1); - } - if (strlen(pd->content) != 2 * solv_chksum_len(type)) + Id index; + + if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype)) { - fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr); + fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype)); exit(1); } - repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, type, pd->content); + repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, pd->chksumtype, pd->content); /* we save the checksum to solvable id relationship for extended metadata */ index = stringpool_str2id(&pd->cspool, pd->content, 1 /* create it */); @@ -1026,12 +1029,15 @@ endElement(void *userData, const char *name) break; case STATE_SUMMARY: repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content); + pd->tmplang = solv_free((char *)pd->tmplang); break; case STATE_DESCRIPTION: set_description_author(pd->data, handle, pd->content, pd); + pd->tmplang = solv_free((char *)pd->tmplang); break; case STATE_CATEGORY: repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_CATEGORY, pd->tmplang), pd->content); + pd->tmplang = solv_free((char *)pd->tmplang); break; case STATE_DISTRIBUTION: repodata_set_poolstr(pd->data, handle, SOLVABLE_DISTRIBUTION, pd->content); @@ -1075,6 +1081,7 @@ endElement(void *userData, const char *name) case STATE_EULA: if (pd->content[0]) repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_EULA, pd->tmplang), pd->content); + pd->tmplang = solv_free((char *)pd->tmplang); break; case STATE_KEYWORD: if (pd->content[0]) @@ -1158,18 +1165,15 @@ repo_add_rpmmd(Repo *repo, FILE *fp, const char *language, int flags) pd.swtab[sw->from] = sw; pd.sbtab[sw->to] = sw->from; } - pd.common.pool = pool; - pd.common.repo = repo; - + pd.pool = pool; + pd.repo = repo; pd.data = data; pd.content = solv_malloc(256); pd.acontent = 256; pd.lcontent = 0; - pd.common.tmp = 0; - pd.common.tmpl = 0; pd.kind = 0; - pd.language = language; + pd.language = language && *language && strcmp(language, "en") != 0 ? language : 0; /* initialize the string pool where we will store the package checksums we know about, to get an Id diff --git a/ext/repo_susetags.c b/ext/repo_susetags.c index 0e69228..52b2e1f 100644 --- a/ext/repo_susetags.c +++ b/ext/repo_susetags.c @@ -24,18 +24,21 @@ struct datashare { Id evr; Id arch; }; + struct parsedata { - char *kind; + Pool *pool; Repo *repo; Repodata *data; + char *kind; int flags; - struct parsedata_common common; int last_found_source; struct datashare *share_with; int nshare; - Id (*dirs)[3]; // dirid, size, nfiles + Id (*dirs)[3]; /* dirid, size, nfiles */ int ndirs; - Id langcache[ID_NUM_INTERNAL]; + + char *language; /* the default language */ + Id langcache[ID_NUM_INTERNAL]; /* cache for the default language */ int lineno; }; @@ -52,13 +55,14 @@ static char *flagtab[] = { static Id langtag(struct parsedata *pd, Id tag, const char *language) { - if (language && !language[0]) - language = 0; - if (!language || tag >= ID_NUM_INTERNAL) + if (language && *language) return pool_id2langid(pd->repo->pool, tag, language, 1); - return pool_id2langid(pd->repo->pool, tag, language, 1); + if (!pd->language) + return tag; + if (tag >= ID_NUM_INTERNAL) + return pool_id2langid(pd->repo->pool, tag, pd->language, 1); if (!pd->langcache[tag]) - pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1); + pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, pd->language, 1); return pd->langcache[tag]; } @@ -578,10 +582,11 @@ repo_add_susetags(Repo *repo, FILE *fp, Id defvendor, const char *language, int line = malloc(1024); aline = 1024; - pd.repo = pd.common.repo = repo; + pd.pool = pool; + pd.repo = repo; pd.data = data; - pd.common.pool = pool; pd.flags = flags; + pd.language = language && *language ? solv_strdup(language) : 0; linep = line; s = 0; @@ -723,19 +728,21 @@ repo_add_susetags(Repo *repo, FILE *fp, Id defvendor, const char *language, int continue; if (! (line[0] && line[1] && line[2] && line[3] && (line[4] == ':' || line[4] == '.'))) continue; + line_lang[0] = 0; if (line[4] == '.') { char *endlang; endlang = strchr(line + 5, ':'); if (endlang) { + int langsize = endlang - (line + 5); keylen = endlang - line - 1; - strncpy(line_lang, line + 5, 5); - line_lang[endlang - line - 5] = 0; + if (langsize > 5) + langsize = 5; + strncpy(line_lang, line + 5, langsize); + line_lang[langsize] = 0; } } - else - line_lang[0] = 0; tag = tag_from_string(line); if (indelta) @@ -1015,19 +1022,19 @@ repo_add_susetags(Repo *repo, FILE *fp, Id defvendor, const char *language, int repodata_set_str(data, handle, SOLVABLE_AUTHORS, line + 6); continue; case CTAG('=', 'S', 'u', 'm'): - repodata_set_str(data, handle, langtag(&pd, SOLVABLE_SUMMARY, language ? language : line_lang), line + 3 + keylen ); + repodata_set_str(data, handle, langtag(&pd, SOLVABLE_SUMMARY, line_lang), line + 3 + keylen); continue; case CTAG('=', 'D', 'e', 's'): - repodata_set_str(data, handle, langtag(&pd, SOLVABLE_DESCRIPTION, language ? language : line_lang), line + 3 + keylen ); + repodata_set_str(data, handle, langtag(&pd, SOLVABLE_DESCRIPTION, line_lang), line + 3 + keylen); continue; case CTAG('=', 'E', 'u', 'l'): - repodata_set_str(data, handle, langtag(&pd, SOLVABLE_EULA, language), line + 6); + repodata_set_str(data, handle, langtag(&pd, SOLVABLE_EULA, line_lang), line + 6); continue; case CTAG('=', 'I', 'n', 's'): - repodata_set_str(data, handle, langtag(&pd, SOLVABLE_MESSAGEINS, language), line + 6); + repodata_set_str(data, handle, langtag(&pd, SOLVABLE_MESSAGEINS, line_lang), line + 6); continue; case CTAG('=', 'D', 'e', 'l'): - repodata_set_str(data, handle, langtag(&pd, SOLVABLE_MESSAGEDEL, language), line + 6); + repodata_set_str(data, handle, langtag(&pd, SOLVABLE_MESSAGEDEL, line_lang), line + 6); continue; case CTAG('=', 'V', 'i', 's'): { @@ -1035,7 +1042,7 @@ repo_add_susetags(Repo *repo, FILE *fp, Id defvendor, const char *language, int unsigned k; k = atoi(line + 6); if (k || !strcasecmp(line + 6, "true")) - repodata_set_void(data, handle, SOLVABLE_ISVISIBLE ); + repodata_set_void(data, handle, SOLVABLE_ISVISIBLE); } continue; case CTAG('=', 'S', 'h', 'r'): @@ -1099,7 +1106,9 @@ repo_add_susetags(Repo *repo, FILE *fp, Id defvendor, const char *language, int set_checksum(&pd, data, handle, SOLVABLE_CHECKSUM, line + 6); break; case CTAG('=', 'L', 'a', 'n'): - language = solv_strdup(line + 6); + pd.language = solv_free(pd.language); + if (line[6]) + pd.language = solv_strdup(line + 6); break; case CTAG('=', 'F', 'l', 's'): @@ -1209,8 +1218,7 @@ repo_add_susetags(Repo *repo, FILE *fp, Id defvendor, const char *language, int if (!(flags & REPO_NO_INTERNALIZE)) repodata_internalize(data); - if (pd.common.tmp) - free(pd.common.tmp); + solv_free(pd.language); free(line); join_freemem(); return 0; diff --git a/ext/repo_zyppdb.c b/ext/repo_zyppdb.c index e31ecc7..8882fce 100644 --- a/ext/repo_zyppdb.c +++ b/ext/repo_zyppdb.c @@ -84,8 +84,6 @@ struct parsedata { Solvable *solvable; Id handle; - - Id langcache[ID_NUM_INTERNAL]; }; @@ -112,23 +110,6 @@ find_attr(const char *txt, const char **atts, int dup) /* - * create localized tag - */ - -static Id -langtag(struct parsedata *pd, Id tag, const char *language) -{ - if (language && !language[0]) - language = 0; - if (!language || tag >= ID_NUM_INTERNAL) - return pool_id2langid(pd->repo->pool, tag, language, 1); - if (!pd->langcache[tag]) - pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1); - return pd->langcache[tag]; -} - - -/* * XML callback: startElement */ @@ -184,9 +165,7 @@ startElement(void *userData, const char *name, const char **atts) repodata_extend(pd->data, s - pool->solvables); pd->handle = s - pool->solvables; if (type) - { - repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type); - } + repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type); } break; case STATE_VERSION: @@ -246,7 +225,7 @@ endElement(void *userData, const char *name) s->arch = pool_str2id(pd->pool, pd->content, 1); break; case STATE_SUMMARY: - repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content); + repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content); pd->tmplang = solv_free((void *)pd->tmplang); break; case STATE_VENDOR: diff --git a/ext/tools_util.h b/ext/tools_util.h index 20bfcfe..309a433 100644 --- a/ext/tools_util.h +++ b/ext/tools_util.h @@ -16,13 +16,6 @@ static char *_join_tmp; static int _join_tmpl; -struct parsedata_common { - char *tmp; - int tmpl; - Pool *pool; - Repo *repo; -}; - static inline Id makeevr(Pool *pool, const char *s) { diff --git a/src/pool.c b/src/pool.c index 8724011..01b3dd4 100644 --- a/src/pool.c +++ b/src/pool.c @@ -1226,7 +1226,7 @@ pool_id2langid(Pool *pool, Id id, const char *lang, int create) char buf[256], *p; int l; - if (!lang) + if (!lang || !*lang) return id; n = pool_id2str(pool, id); l = strlen(n) + strlen(lang) + 2;