X-Git-Url: https://git.librecmc.org/?p=oweals%2Fopkg-lede.git;a=blobdiff_plain;f=libopkg%2Fpkg_parse.c;h=246705e8f2549ae2d2270489dcd7ff880ccaee31;hp=98d7a9b209b706af780c42cea8480f8352fca205;hb=1e92bab925599c6162c7824d20ec70fca6f9fe59;hpb=480538737a8a9be074a1848f2e52cf2d1ff4709f diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c index 98d7a9b..246705e 100644 --- a/libopkg/pkg_parse.c +++ b/libopkg/pkg_parse.c @@ -24,382 +24,374 @@ #include "pkg_parse.h" #include "libbb/libbb.h" -int isGenericFieldType(char * type, char * line) +static int +is_field(char *type, const char *line) { - if(!strncmp(line, type, strlen(type))) - return 1; - return 0; + if (!strncmp(line, type, strlen(type))) + return 1; + return 0; } -char * parseGenericFieldType(char * type, char * raw) +static char * +parse_simple(char *type, const char *line) { - char * field_value = raw + (strlen(type) + 1); - return trim_alloc(field_value); + return trim_xstrdup(line + strlen(type) + 1); } -void parseStatus(pkg_t *pkg, char * raw) +/* + * Parse a comma separated string into an array. + */ +static char ** +parse_comma_separated(const char *raw, int *count) { - char sw_str[64], sf_str[64], ss_str[64]; + char **depends = NULL; + const char *start, *end; + int line_count = 0; - 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); -} + /* skip past the "Field:" marker */ + while (*raw && *raw != ':') + raw++; + raw++; -char ** parseDependsString(char * raw, int * depends_count) -{ - char ** depends = NULL; - int line_count = 0; - char buff[2048], * dest; + if (line_is_blank(raw)) { + *count = line_count; + return NULL; + } - 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 (*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; -} + while (isspace(*raw)) + raw++; -void parseConffiles(pkg_t * pkg, char * raw) -{ - char file_name[1048], md5sum[1048]; /* please tell me there aren't any longer that 1k */ + start = raw; + while (*raw != ',' && *raw) + raw++; + end = raw; - if(!strncmp(raw, "Conffiles:", 10)) - raw += strlen("Conffiles:"); + while (end > start && isspace(*end)) + end--; - 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++; - } - raw += strlen(file_name); - while (*raw && isspace(*raw)) { - raw++; + depends[line_count] = xstrndup(start, end-start); + + line_count++; + if (*raw == ',') + raw++; } - raw += strlen(md5sum); - } -} -int parseVersion(pkg_t *pkg, char *raw) -{ - 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; + *count = line_count; + return depends; } +static void +parse_status(pkg_t *pkg, const char *sstr) +{ + char sw_str[64], sf_str[64], ss_str[64]; -/* This code is needed to insert in first position the keyword for the aligning bug */ + if (sscanf(sstr, "Status: %63s %63s %63s", + sw_str, sf_str, ss_str) != 3) { + fprintf(stderr, "%s: failed to parse Status line for %s\n", + __FUNCTION__, pkg->name); + return; + } -int alterProvidesLine(char *raw, char *temp) + 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 void +parse_conffiles(pkg_t *pkg, const char *cstr) { + char file_name[1024], md5sum[35]; + if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) { + fprintf(stderr, "%s: failed to parse Conffiles line for %s\n", + __FUNCTION__, pkg->name); + return; + } - if (!*raw) { - fprintf(stderr, "%s: ERROR: Provides string is empty", __FUNCTION__); - return -EINVAL; - } - - if ( temp == NULL ) { - fprintf(stderr, "%s: out of memory \n", __FUNCTION__); - return -ENOMEM; - } - - if (strncmp(raw, "Provides:", 9) == 0) { - raw += 9; - } - while (*raw && isspace(*raw)) { - raw++; - } - - snprintf ( temp, 35, "Provides: opkg_internal_use_only, "); /* First part of the line */ - while (*raw) { - strncat( temp, raw++, 1); - } - return 0; - + conffile_list_append(&pkg->conffiles, file_name, md5sum); } -/* Some random thoughts from Carl: +int +parse_version(pkg_t *pkg, const char *vstr) +{ + char *colon; - This function could be considerably simplified if we just kept - an array of all the generic string-valued field names, and looped - through those looking for a match. Also, these fields could perhaps - be stored in the package as an array as well, (or, probably better, - as an nv_pair_list_t). + if (strncmp(vstr, "Version:", 8) == 0) + vstr += 8; - Fields which require special parsing or storage, (such as Depends: - and Status:) could be handled as they are now. -*/ -/* XXX: FEATURE: The Suggests: field needs to be changed from a string - to a dependency list. And, since we already have - Depends/Pre-Depends and need to add Conflicts, Recommends, and - Enhances, perhaps we could generalize all of these and save some - code duplication. -*/ -int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest) -{ - int reading_conffiles, reading_description; - int pkg_false_provides=1; - char ** lines; - char * provide=NULL; + while (*vstr && isspace(*vstr)) + vstr++; - pkg->src = src; - pkg->dest = dest; + colon = strchr(vstr, ':'); + if (colon) { + errno = 0; + pkg->epoch = strtoul(vstr, NULL, 10); + if (errno) { + fprintf(stderr, "%s: %s: invalid epoch: %s\n", + __FUNCTION__, pkg->name, strerror(errno)); + } + vstr = ++colon; + } else { + pkg->epoch= 0; + } - reading_conffiles = reading_description = 0; + pkg->version= xstrdup(vstr); + pkg->revision = strrchr(pkg->version,'-'); - for (lines = *raw; *lines; lines++) { - /* fprintf(stderr, "PARSING %s\n", *lines);*/ - switch (**lines) { - case 'P': - if(isGenericFieldType("Package:", *lines)) - pkg->name = parseGenericFieldType("Package", *lines); - else if(isGenericFieldType("Priority:", *lines)) - pkg->priority = parseGenericFieldType("Priority", *lines); - else if(isGenericFieldType("Provides", *lines)){ -/* Here we add the internal_use to align the off by one problem between provides_str and provides */ - provide = xcalloc(1, strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */ - if ( alterProvidesLine(*lines,provide) ){ - return EINVAL; - } - pkg->provides_str = parseDependsString( provide, &pkg->provides_count); -/* Let's try to hack a bit here. - The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies - in alot of other places. We will remove it before writing down the status database */ - pkg_false_provides=0; - free(provide); - } - else if(isGenericFieldType("Pre-Depends", *lines)) - pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count); - break; + if (pkg->revision) + *pkg->revision++ = '\0'; + + return 0; +} + +static int +pkg_parse_line(pkg_t *pkg, const char *line, uint mask) +{ + /* these flags are a bit hackish... */ + static int reading_conffiles = 0, reading_description = 0; + switch (*line) { case 'A': - if(isGenericFieldType("Architecture:", *lines)) - pkg->architecture = parseGenericFieldType("Architecture", *lines); - else if(isGenericFieldType("Auto-Installed:", *lines)) { - char *auto_installed_value; - auto_installed_value = parseGenericFieldType("Auto-Installed:", *lines); - if (strcmp(auto_installed_value, "yes") == 0) { - pkg->auto_installed = 1; + if ((mask & PFM_ARCHITECTURE ) && is_field("Architecture", line)) + pkg->architecture = parse_simple("Architecture", line); + 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); + } + break; + + case 'C': + if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) { + reading_conffiles = 1; + reading_description = 0; + goto dont_reset_flags; + } + else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line)) + pkg->conflicts_str = parse_comma_separated(line, &pkg->conflicts_count); + break; + + case 'D': + if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) { + pkg->description = parse_simple("Description", line); + reading_conffiles = 0; + reading_description = 1; + goto dont_reset_flags; + } else if ((mask & PFM_DEPENDS) && is_field("Depends", line)) + pkg->depends_str = parse_comma_separated(line, &pkg->depends_count); + break; + + case 'E': + 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(auto_installed_value); - } - break; + break; case 'F': - if(isGenericFieldType("Filename:", *lines)) - pkg->filename = parseGenericFieldType("Filename", *lines); - break; + if((mask & PFM_FILENAME) && is_field("Filename", line)) + pkg->filename = parse_simple("Filename", line); + break; + + case 'I': + if ((mask && PFM_INSTALLED_SIZE) && is_field("Installed-Size", line)) + pkg->installed_size = parse_simple("Installed-Size", line); + else if ((mask && PFM_INSTALLED_TIME) && is_field("Installed-Time", line)) { + char *tmp = parse_simple("Installed-Time", line); + pkg->installed_time = strtoul(tmp, NULL, 0); + free (tmp); + } + break; + + case 'M': + if (mask && PFM_MD5SUM) { + if (is_field("MD5sum:", line)) + pkg->md5sum = parse_simple("MD5sum", line); + /* The old opkg wrote out status files with the wrong + * case for MD5sum, let's parse it either way */ + else if (is_field("MD5Sum:", line)) + pkg->md5sum = parse_simple("MD5Sum", line); + } else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line)) + pkg->maintainer = parse_simple("Maintainer", line); + break; + + case 'P': + if ((mask & PFM_PACKAGE) && is_field("Package", line)) + pkg->name = parse_simple("Package", line); + else if ((mask & PFM_PRIORITY) && is_field("Priority", line)) + pkg->priority = parse_simple("Priority", line); + else if ((mask & PFM_PROVIDES) && is_field("Provides", line)) + pkg->provides_str = parse_comma_separated(line, &pkg->provides_count); + else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line)) + pkg->pre_depends_str = parse_comma_separated(line, &pkg->pre_depends_count); + break; + + case 'R': + if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line)) + pkg->recommends_str = parse_comma_separated(line, &pkg->recommends_count); + else if ((mask & PFM_REPLACES) && is_field("Replaces", line)) + pkg->replaces_str = parse_comma_separated(line, &pkg->replaces_count); + + break; case 'S': - if(isGenericFieldType("Section:", *lines)) - pkg->section = parseGenericFieldType("Section", *lines); + if ((mask & PFM_SECTION) && is_field("Section", line)) + pkg->section = parse_simple("Section", line); #ifdef HAVE_SHA256 - else if(isGenericFieldType("SHA256sum:", *lines)) - pkg->sha256sum = parseGenericFieldType("SHA256sum", *lines); + else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line)) + pkg->sha256sum = parse_simple("SHA256sum", line); #endif - else if(isGenericFieldType("Size:", *lines)) - pkg->size = parseGenericFieldType("Size", *lines); - else if(isGenericFieldType("Source:", *lines)) - pkg->source = parseGenericFieldType("Source", *lines); - else if(isGenericFieldType("Status", *lines)) - parseStatus(pkg, *lines); - else if(isGenericFieldType("Suggests", *lines)) - pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count); - break; + else if ((mask & PFM_SIZE) && is_field("Size", line)) + pkg->size = parse_simple("Size", line); + else if ((mask & PFM_SOURCE) && is_field("Source", line)) + pkg->source = parse_simple("Source", line); + else if ((mask & PFM_STATUS) && is_field("Status", line)) + parse_status(pkg, line); + else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line)) + pkg->suggests_str = parse_comma_separated(line, &pkg->suggests_count); + break; case 'T': - if(isGenericFieldType("Tags:", *lines)) - pkg->tags = parseGenericFieldType("Tags", *lines); - break; + if ((mask & PFM_TAGS) && is_field("Tags", line)) + pkg->tags = parse_simple("Tags", line); + break; - case 'M': - if(isGenericFieldType("MD5sum:", *lines)) - pkg->md5sum = parseGenericFieldType("MD5sum", *lines); - /* The old opkg wrote out status files with the wrong case for MD5sum, - let's parse it either way */ - else if(isGenericFieldType("MD5Sum:", *lines)) - pkg->md5sum = parseGenericFieldType("MD5Sum", *lines); - else if(isGenericFieldType("Maintainer", *lines)) - pkg->maintainer = parseGenericFieldType("Maintainer", *lines); - break; + case 'V': + if ((mask & PFM_VERSION) && is_field("Version", line)) + parse_version(pkg, line); + break; - case 'I': - if(isGenericFieldType("Installed-Size:", *lines)) - pkg->installed_size = parseGenericFieldType("Installed-Size", *lines); - else if(isGenericFieldType("Installed-Time:", *lines)) { - char *time_str = parseGenericFieldType("Installed-Time", *lines); - pkg->installed_time = strtoul(time_str, NULL, 0); - free (time_str); - } - break; + case ' ': + if ((mask & PFM_DESCRIPTION) && reading_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) { + parse_conffiles(pkg, line); + goto dont_reset_flags; + } + break; - case 'E': - if(isGenericFieldType("Essential:", *lines)) { - char *essential_value; - essential_value = parseGenericFieldType("Essential", *lines); - if (strcmp(essential_value, "yes") == 0) { - pkg->essential = 1; + 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); + } + break; } - free(essential_value); - } - break; - case 'V': - if(isGenericFieldType("Version", *lines)) - parseVersion(pkg, *lines); - break; + 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; + } - case 'C': - if(isGenericFieldType("Conffiles", *lines)){ - parseConffiles(pkg, *lines); - reading_conffiles = 1; - } - else if(isGenericFieldType("Conflicts", *lines)) - pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count); - break; + *nl = '\0'; - case 'D': - if(isGenericFieldType("Description", *lines)) { - pkg->description = parseGenericFieldType("Description", *lines); - reading_conffiles = 0; - reading_description = 1; - } - else if(isGenericFieldType("Depends", *lines)) - pkg->depends_str = parseDependsString(*lines, &pkg->depends_count); - break; + lineno++; - case 'R': - if(isGenericFieldType("Recommends", *lines)) - pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count); - else if(isGenericFieldType("Replaces", *lines)) - pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count); - - break; + if (pkg_parse_line(pkg, *buf0, mask)) + break; - case ' ': - if(reading_description) { - /* we already know it's not blank, so the rest of description */ - pkg->description = xrealloc(pkg->description, - strlen(pkg->description) - + 1 + strlen(*lines) + 1); - strcat(pkg->description, "\n"); - strcat(pkg->description, (*lines)); - } - else if(reading_conffiles) - parseConffiles(pkg, *lines); - - break; + buf = *buf0; + buflen = buf0len; + buf[0] = '\0'; + }; - default: - if(line_is_blank(*lines)) { - lines++; - goto out; - } + if (pkg->name == NULL) { + /* probably just a blank line */ + ret = EINVAL; } - } -out:; - - *raw = lines; -/* If the opk has not a Provides line, we insert our false line */ - if ( pkg_false_provides==1) - { - pkg->provides_count = 1; - pkg->provides_str = xcalloc(1, sizeof (char*)); - pkg->provides_str[0] = xstrdup("opkg_internal_use_only"); - } - - if (pkg->name) { - return 0; - } else { - return EINVAL; - } + + return ret; } -int pkg_valorize_other_field(pkg_t *pkg, char ***raw) +int +pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask) { - char ** lines; - - for (lines = *raw; *lines; lines++) { - if(isGenericFieldType("Essential:", *lines)) { - char *essential_value; - essential_value = parseGenericFieldType("Essential", *lines); - if (strcmp(essential_value, "yes") == 0) { - pkg->essential = 1; - } - free(essential_value); - } - } - *raw = lines; + int ret; + char *buf; + const size_t len = 4096; + + buf = xmalloc(len); + ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len); + free(buf); - return 0; + return ret; }