X-Git-Url: https://git.librecmc.org/?p=oweals%2Fopkg-lede.git;a=blobdiff_plain;f=libopkg%2Fpkg_parse.c;h=7017a06b6f10af45d562fcae68225f0bc21bb3c9;hp=44338f03d5bd84ffc3f096037db97c87b0fdb725;hb=271d485c10f2070573b25e740b93839945dbcd9d;hpb=3e11844f829eef5260ab13fe0d95594a77a1d065 diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c index 44338f0..7017a06 100644 --- a/libopkg/pkg_parse.c +++ b/libopkg/pkg_parse.c @@ -1,7 +1,8 @@ /* pkg_parse.c - the opkg package management system + Copyright (C) 2009 Ubiq Technologies + Steven M. Ayer - Copyright (C) 2002 Compaq Computer Corporation This program is free software; you can redistribute it and/or @@ -15,402 +16,302 @@ General Public License for more details. */ -#include "includes.h" -#include +#include #include - +#include + #include "pkg.h" #include "opkg_utils.h" #include "pkg_parse.h" +#include "pkg_depends.h" #include "libbb/libbb.h" -static int isGenericFieldType(char * type, const char * line) -{ - if(!strncmp(line, type, strlen(type))) - return 1; - return 0; -} +#include "file_util.h" +#include "parse_util.h" -static char * parseGenericFieldType(char * type, const char * raw) +static void parse_status(pkg_t * pkg, const char *sstr) { - const char * field_value = raw + (strlen(type) + 1); - return trim_alloc(field_value); -} + char sw_str[64], sf_str[64], ss_str[64]; -static void parseStatus(pkg_t *pkg, const char * raw) -{ - char sw_str[64], sf_str[64], ss_str[64]; + if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) { + opkg_msg(ERROR, "Failed to parse Status line for %s\n", + pkg->name); + return; + } - sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str); - pkg->state_want = pkg_state_want_from_str(sw_str); - pkg->state_flag = pkg_state_flag_from_str(sf_str); - pkg->state_status = pkg_state_status_from_str(ss_str); + pkg->state_want = pkg_state_want_from_str(sw_str); + pkg->state_flag |= pkg_state_flag_from_str(sf_str); + pkg->state_status = pkg_state_status_from_str(ss_str); } -static char ** parseDependsString(const char * raw, int * depends_count) +static void parse_conffiles(pkg_t * pkg, const char *cstr) { - char ** depends = NULL; - int line_count = 0; - char buff[2048], * dest; - - while(raw && *raw && !isspace(*raw)) { - raw++; - } - - if(line_is_blank(raw)){ - *depends_count = line_count; - return NULL; - } - while(raw && *raw){ - depends = xrealloc(depends, sizeof(char *) * (line_count + 1)); - - while(isspace(*raw)) raw++; - - dest = buff; - while((*raw != ',') && *raw) - *dest++ = *raw++; - - *dest = '\0'; - depends[line_count] = trim_alloc(buff); - if(depends[line_count] ==NULL) - return NULL; - line_count++; - if(*raw == ',') - raw++; - } - *depends_count = line_count; - return depends; + conffile_list_t *cl; + char file_name[1024], md5sum[85]; + + if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) { + opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n", + pkg->name); + return; + } + + cl = pkg_get_ptr(pkg, PKG_CONFFILES); + + if (cl) + conffile_list_append(cl, file_name, md5sum); } -static void parseConffiles(pkg_t * pkg, const char * raw) +int parse_version(pkg_t * pkg, const char *vstr) { - char file_name[1048], md5sum[1048]; /* please tell me there aren't any longer that 1k */ + char *colon, *dup, *rev; - if(!strncmp(raw, "Conffiles:", 10)) - raw += strlen("Conffiles:"); + if (strncmp(vstr, "Version:", 8) == 0) + vstr += 8; - while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){ - conffile_list_append(&pkg->conffiles, file_name, md5sum); - /* fprintf(stderr, "%s %s ", file_name, md5sum);*/ - while (*raw && isspace(*raw)) { - raw++; + while (*vstr && isspace(*vstr)) + vstr++; + + colon = strchr(vstr, ':'); + if (colon) { + errno = 0; + pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10)); + if (errno) { + opkg_perror(ERROR, "%s: invalid epoch", pkg->name); + } + vstr = ++colon; } - raw += strlen(file_name); - while (*raw && isspace(*raw)) { - raw++; + + + dup = xstrdup(vstr); + rev = strrchr(dup, '-'); + + if (rev) { + *rev++ = '\0'; + pkg_set_string(pkg, PKG_REVISION, rev); } - raw += strlen(md5sum); - } -} -int parseVersion(pkg_t *pkg, const char *raw) + pkg_set_string(pkg, PKG_VERSION, dup); + free(dup); + + return 0; +} + +static char *parse_architecture(pkg_t *pkg, const char *str) { - char *colon, *eepochcolon; - char *hyphen; - unsigned long epoch; - - if (!*raw) { - fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__); - return EINVAL; - } - - if (strncmp(raw, "Version:", 8) == 0) { - raw += 8; - } - while (*raw && isspace(*raw)) { - raw++; - } - - colon= strchr(raw,':'); - if (colon) { - epoch= strtoul(raw,&eepochcolon,10); - if (colon != eepochcolon) { - fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__); - return EINVAL; - } - if (!*++colon) { - fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__); - return EINVAL; - } - raw= colon; - pkg->epoch= epoch; - } else { - pkg->epoch= 0; - } - - pkg->revision = ""; - - if (!pkg->version) - { - pkg->version= xcalloc(1, strlen(raw)+1); - strcpy(pkg->version, raw); - } - - hyphen= strrchr(pkg->version,'-'); - - if (hyphen) { - *hyphen++= 0; - pkg->revision = hyphen; - } - - return 0; + const char *s = str; + const char *e; + + while (isspace(*s)) + s++; + + e = s + strlen(s); + + while (e > s && isspace(*e)) + e--; + + return pkg_set_architecture(pkg, s, e - s); } -static int -pkg_parse_line(pkg_t *pkg, const char *line, uint mask) +int pkg_parse_line(void *ptr, const char *line, uint mask) { + pkg_t *pkg = (pkg_t *) ptr; + abstract_pkg_t *ab_pkg = NULL; + conffile_list_t *cl; + /* these flags are a bit hackish... */ static int reading_conffiles = 0, reading_description = 0; + static char *description = NULL; + char *s; + int ret = 0; + + /* Exclude globally masked fields. */ + mask |= conf->pfm; + + /* Flip the semantics of the mask. */ + mask ^= PFM_ALL; switch (*line) { case 'A': - if((mask & PFM_ARCHITECTURE ) && isGenericFieldType("Architecture:", line)) - pkg->architecture = parseGenericFieldType("Architecture", line); - else if((mask & PFM_AUTO_INSTALLED) && isGenericFieldType("Auto-Installed:", line)) { - char *auto_installed_value; - auto_installed_value = parseGenericFieldType("Auto-Installed:", line); - if (strcmp(auto_installed_value, "yes") == 0) { - pkg->auto_installed = 1; + if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line)) + parse_architecture(pkg, line + strlen("Architecture") + 1); + else if ((mask & PFM_AUTO_INSTALLED) + && is_field("Auto-Installed", line)) { + char *tmp = parse_simple("Auto-Installed", line); + if (strcmp(tmp, "yes") == 0) + pkg->auto_installed = 1; + free(tmp); } - free(auto_installed_value); - } - break; + break; case 'C': - if((mask & PFM_CONFFILES) && isGenericFieldType("Conffiles", line)){ - parseConffiles(pkg, line); - reading_conffiles = 1; - reading_description = 0; - goto dont_reset_flags; - } - else if((mask & PFM_CONFLICTS) && isGenericFieldType("Conflicts", line)) - pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count); - break; + if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) { + reading_conffiles = 1; + reading_description = 0; + + cl = xcalloc(1, sizeof(*cl)); + conffile_list_init(cl); + pkg_set_ptr(pkg, PKG_CONFFILES, cl); + + goto dont_reset_flags; + } else if ((mask & PFM_CONFLICTS) + && is_field("Conflicts", line)) + parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1); + break; case 'D': - if((mask & PFM_DESCRIPTION) && isGenericFieldType("Description", line)) { - pkg->description = parseGenericFieldType("Description", line); - reading_conffiles = 0; - reading_description = 1; - goto dont_reset_flags; - } - else if((mask & PFM_DEPENDS) && isGenericFieldType("Depends", line)) - pkg->depends_str = parseDependsString(line, &pkg->depends_count); - break; + if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) { + description = parse_simple("Description", line); + reading_conffiles = 0; + reading_description = 1; + goto dont_reset_flags; + } else if ((mask & PFM_DEPENDS) && is_field("Depends", line)) + parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1); + break; case 'E': - if((mask & PFM_ESSENTIAL) && isGenericFieldType("Essential:", line)) { - char *essential_value; - essential_value = parseGenericFieldType("Essential", line); - if (strcmp(essential_value, "yes") == 0) { - pkg->essential = 1; + if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) { + char *tmp = parse_simple("Essential", line); + if (strcmp(tmp, "yes") == 0) + pkg->essential = 1; + free(tmp); } - free(essential_value); - } - break; + break; case 'F': - if((mask & PFM_FILENAME) && isGenericFieldType("Filename:", line)) - pkg->filename = parseGenericFieldType("Filename", line); - break; + if ((mask & PFM_FILENAME) && is_field("Filename", line)) + pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1); + break; case 'I': - if((mask && PFM_INSTALLED_SIZE) && isGenericFieldType("Installed-Size:", line)) - pkg->installed_size = parseGenericFieldType("Installed-Size", line); - else if((mask && PFM_INSTALLED_TIME) && isGenericFieldType("Installed-Time:", line)) { - char *time_str = parseGenericFieldType("Installed-Time", line); - pkg->installed_time = strtoul(time_str, NULL, 0); - free (time_str); - } - break; + if ((mask & PFM_INSTALLED_SIZE) + && is_field("Installed-Size", line)) { + pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0)); + } else if ((mask & PFM_INSTALLED_TIME) + && is_field("Installed-Time", line)) { + pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0)); + } + break; case 'M': - if(mask && PFM_MD5SUM) { - if (isGenericFieldType("MD5sum:", line)) - pkg->md5sum = parseGenericFieldType("MD5sum", line); - /* The old opkg wrote out status files with the wrong - * case for MD5sum, let's parse it either way */ - else if(isGenericFieldType("MD5Sum:", line)) - pkg->md5sum = parseGenericFieldType("MD5Sum", line); - } else if((mask & PFM_MAINTAINER) && isGenericFieldType("Maintainer", line)) - pkg->maintainer = parseGenericFieldType("Maintainer", line); - break; + if ((mask & PFM_MD5SUM) && (is_field("MD5sum:", line) || is_field("MD5Sum:", line))) + pkg_set_md5(pkg, line + strlen("MD5sum") + 1); + else if ((mask & PFM_MAINTAINER) + && is_field("Maintainer", line)) + pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1); + break; case 'P': - if((mask & PFM_PACKAGE) && isGenericFieldType("Package:", line)) - pkg->name = parseGenericFieldType("Package", line); - else if((mask & PFM_PRIORITY) && isGenericFieldType("Priority:", line)) - pkg->priority = parseGenericFieldType("Priority", line); - else if((mask & PFM_PROVIDES) && isGenericFieldType("Provides", line)){ - pkg->provides_str = parseDependsString(line, &pkg->provides_count); - } - else if((mask & PFM_PRE_DEPENDS) && isGenericFieldType("Pre-Depends", line)) - pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count); - break; + if ((mask & PFM_PACKAGE) && is_field("Package", line)) { + pkg->name = parse_simple("Package", line); + ab_pkg = abstract_pkg_fetch_by_name(pkg->name); + + if (ab_pkg && (ab_pkg->state_flag & SF_NEED_DETAIL)) { + if (!(pkg->state_flag & SF_NEED_DETAIL)) { + opkg_msg(DEPEND, "propagating abpkg flag to pkg %s\n", pkg->name); + pkg->state_flag |= SF_NEED_DETAIL; + } + } + } + else if ((mask & PFM_PRIORITY) && is_field("Priority", line)) + pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1); + else if ((mask & PFM_PROVIDES) && is_field("Provides", line)) + parse_providelist(pkg, line + strlen("Provides") + 1); + else if ((mask & PFM_PRE_DEPENDS) + && is_field("Pre-Depends", line)) + parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1); + break; case 'R': - if((mask & PFM_RECOMMENDS) && isGenericFieldType("Recommends", line)) - pkg->recommends_str = parseDependsString(line, &pkg->recommends_count); - else if((mask & PFM_REPLACES) && isGenericFieldType("Replaces", line)) - pkg->replaces_str = parseDependsString(line, &pkg->replaces_count); - - break; + if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line)) + parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1); + else if ((mask & PFM_REPLACES) && is_field("Replaces", line)) + parse_replacelist(pkg, line + strlen("Replaces") + 1); + break; case 'S': - if((mask & PFM_SECTION) && isGenericFieldType("Section:", line)) - pkg->section = parseGenericFieldType("Section", line); -#ifdef HAVE_SHA256 - else if((mask & PFM_SHA256SUM) && isGenericFieldType("SHA256sum:", line)) - pkg->sha256sum = parseGenericFieldType("SHA256sum", line); -#endif - else if((mask & PFM_SIZE) && isGenericFieldType("Size:", line)) - pkg->size = parseGenericFieldType("Size", line); - else if((mask & PFM_SOURCE) && isGenericFieldType("Source:", line)) - pkg->source = parseGenericFieldType("Source", line); - else if((mask & PFM_STATUS) && isGenericFieldType("Status", line)) - parseStatus(pkg, line); - else if((mask & PFM_SUGGESTS) && isGenericFieldType("Suggests", line)) - pkg->suggests_str = parseDependsString(line, &pkg->suggests_count); - break; + if ((mask & PFM_SECTION) && is_field("Section", line)) + pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1); + else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line)) + pkg_set_sha256(pkg, line + strlen("SHA256sum") + 1); + else if ((mask & PFM_SIZE) && is_field("Size", line)) { + pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0)); + } else if ((mask & PFM_SOURCE) && is_field("Source", line)) + pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1); + else if ((mask & PFM_STATUS) && is_field("Status", line)) + parse_status(pkg, line); + else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line)) + parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1); + break; case 'T': - if((mask & PFM_TAGS) && isGenericFieldType("Tags:", line)) - pkg->tags = parseGenericFieldType("Tags", line); - break; + if ((mask & PFM_TAGS) && is_field("Tags", line)) + pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1); + break; case 'V': - if((mask & PFM_VERSION) && isGenericFieldType("Version", line)) - parseVersion(pkg, line); - break; + if ((mask & PFM_VERSION) && is_field("Version", line)) + parse_version(pkg, line); + break; case ' ': - if((mask & PFM_DESCRIPTION) && reading_description) { - /* we already know it's not blank, so the rest of description */ - pkg->description = xrealloc(pkg->description, - strlen(pkg->description) - + 1 + strlen(line) + 1); - strcat(pkg->description, "\n"); - strcat(pkg->description, (line)); - goto dont_reset_flags; - } - else if((mask && PFM_CONFFILES) && reading_conffiles) { - parseConffiles(pkg, line); - goto dont_reset_flags; - } - break; - - default: - /* For package lists, signifies end of package. */ - if(line_is_blank(line)) { - return 1; - } - } - - reading_description = 0; - reading_conffiles = 0; - -dont_reset_flags: - - return 0; -} - -int -pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask, - char **buf0, size_t buf0len) -{ - int ret, lineno; - char *buf, *nl; - size_t buflen; - - lineno = 1; - ret = 0; - - buflen = buf0len; - buf = *buf0; - buf[0] = '\0'; - - while (1) { - if (fgets(buf, buflen, fp) == NULL) { - if (ferror(fp)) { - fprintf(stderr, "%s: fgets: %s\n", - __FUNCTION__, strerror(errno)); - ret = -1; - } else if (strlen(*buf0) == buflen-1) { - fprintf(stderr, "%s: missing new line character" - " at end of file!\n", - __FUNCTION__); - pkg_parse_line(pkg, *buf0, mask); + if ((mask & PFM_DESCRIPTION) && reading_description) { + if (isatty(1)) { + description = xrealloc(description, + strlen(description) + + 1 + strlen(line) + + 1); + strcat(description, "\n"); + } else { + description = xrealloc(description, + strlen(description) + + 1 + strlen(line)); } - break; + strcat(description, (line)); + goto dont_reset_flags; + } else if ((mask & PFM_CONFFILES) && reading_conffiles) { + parse_conffiles(pkg, line); + goto dont_reset_flags; } - nl = strchr(buf, '\n'); - if (nl == NULL) { - if (strlen(buf) < buflen-1) { - /* - * Line could be exactly buflen-1 long and - * missing a newline, but we won't know until - * fgets fails to read more data. - */ - fprintf(stderr, "%s: missing new line character" - " at end of file!\n", - __FUNCTION__); - pkg_parse_line(pkg, *buf0, mask); - break; - } - if (buf0len >= EXCESSIVE_LINE_LEN) { - fprintf(stderr, "%s: excessively long line at " - "%d. Corrupt file?\n", - __FUNCTION__, lineno); - ret = -1; - break; - } - - /* - * Realloc and move buf past the data already read. - * |<--------------- buf0len ----------------->| - * | |<------- buflen ---->| - * |---------------------|---------------------| - * buf0 buf - */ - buflen = buf0len; - buf0len *= 2; - *buf0 = xrealloc(*buf0, buf0len); - buf = *buf0 + buflen -1; - - continue; + /* FALLTHROUGH */ + default: + /* For package lists, signifies end of package. */ + if (line_is_blank(line)) { + ret = 1; + break; } + } - *nl = '\0'; - - lineno++; - - if (pkg_parse_line(pkg, *buf0, mask)) - break; + if (reading_description && description) { + pkg_set_string(pkg, PKG_DESCRIPTION, description); + free(description); + reading_description = 0; + description = NULL; + } - buf = *buf0; - buflen = buf0len; - buf[0] = '\0'; - }; + reading_conffiles = 0; - if (pkg->name == NULL) { - /* probably just a blank line */ - ret = EINVAL; - } +dont_reset_flags: return ret; } -int -pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask) +int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask) { int ret; char *buf; const size_t len = 4096; buf = xmalloc(len); - ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len); + ret = + parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf, + len); free(buf); + if (pkg->name == NULL) { + /* probably just a blank line */ + ret = 1; + } + return ret; }