libopkg: fix --force-checksum to cover sha256sum cases as well
[project/opkg-lede.git] / libopkg / pkg_parse.c
index fb479c36bd7a43840f84eeb673f0ce6423c2beed..edf6312fcc2dd0ba209a8e65522d175dbb8aad2f 100644 (file)
@@ -1,7 +1,8 @@
 /* pkg_parse.c - the opkg package management system
 
+   Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
+
    Steven M. Ayer
-   
    Copyright (C) 2002 Compaq Computer Corporation
 
    This program is free software; you can redistribute it and/or
    General Public License for more details.
 */
 
-#include "includes.h"
-#include <errno.h>
+#include "config.h"
+
+#include <stdio.h>
 #include <ctype.h>
-   
+#include <unistd.h>
+
 #include "pkg.h"
 #include "opkg_utils.h"
 #include "pkg_parse.h"
 #include "libbb/libbb.h"
 
-static int
-is_field(char *type, const char *line)
-{
-       if (!strncmp(line, type, strlen(type)))
-               return 1;
-       return 0;
-}
-
-static char *
-parse_simple(char *type, const char *line)
-{
-       return trim_xstrdup(line + strlen(type) + 1);
-}
-
-/*
- * Parse a comma separated string into an array.
- */
-static char **
-parse_comma_separated(const char *raw, int *count)
-{
-       char **depends = NULL;
-       const char *start, *end;
-       int line_count = 0;
-
-       /* skip past the "Field:" marker */
-       while (*raw && *raw != ':')
-               raw++;
-       raw++;
-
-       if (line_is_blank(raw)) {
-               *count = line_count;
-               return NULL;
-       }
-
-       while (*raw) {
-               depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
-       
-               while (isspace(*raw))
-                       raw++;
-
-               start = raw;
-               while (*raw != ',' && *raw)
-                       raw++;
-               end = raw;
-
-               while (end > start && isspace(*end))
-                       end--;
-
-               depends[line_count] = xstrndup(start, end-start);
-
-               line_count++;
-               if (*raw == ',')
-                   raw++;
-       }
-
-       *count = line_count;
-       return depends;
-}
+#include "parse_util.h"
 
 static void
 parse_status(pkg_t *pkg, const char *sstr)
@@ -90,8 +36,8 @@ parse_status(pkg_t *pkg, const char *sstr)
 
        if (sscanf(sstr, "Status: %63s %63s %63s",
                                sw_str, sf_str, ss_str) != 3) {
-               fprintf(stderr, "%s: failed to parse Conffiles line for %s\n",
-                               __FUNCTION__, pkg->name);
+               opkg_msg(ERROR, "Failed to parse Status line for %s\n",
+                               pkg->name);
                return;
        }
 
@@ -103,11 +49,11 @@ parse_status(pkg_t *pkg, const char *sstr)
 static void
 parse_conffiles(pkg_t *pkg, const char *cstr)
 {
-       char file_name[1024], md5sum[35];
+       char file_name[1024], md5sum[85];
 
-       if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) {
-               fprintf(stderr, "%s: failed to parse Conffiles line for %s\n",
-                               __FUNCTION__, pkg->name);
+       if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
+               opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
+                               pkg->name);
                return;
        }
 
@@ -130,15 +76,14 @@ parse_version(pkg_t *pkg, const char *vstr)
                errno = 0;
                pkg->epoch = strtoul(vstr, NULL, 10);
                if (errno) {
-                       fprintf(stderr, "%s: %s: invalid epoch: %s\n",
-                               __FUNCTION__, pkg->name, strerror(errno));
+                       opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
                }
                vstr = ++colon;
        } else {
                pkg->epoch= 0;
        }
 
-       pkg->version= xstrdup(vstr);
+       pkg->version = xstrdup(vstr);
        pkg->revision = strrchr(pkg->version,'-');
 
        if (pkg->revision)
@@ -148,16 +93,39 @@ parse_version(pkg_t *pkg, const char *vstr)
 }
 
 static int
-pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
+get_arch_priority(const char *arch)
+{
+       nv_pair_list_elt_t *l;
+
+       list_for_each_entry(l , &conf->arch_list.head, node) {
+               nv_pair_t *nv = (nv_pair_t *)l->data;
+               if (strcmp(nv->name, arch) == 0)
+                       return strtol(nv->value, NULL, 0);
+       }
+       return 0;
+}
+
+int
+pkg_parse_line(void *ptr, const char *line, uint mask)
 {
+       pkg_t *pkg = (pkg_t *) ptr;
+
        /* these flags are a bit hackish... */
        static int reading_conffiles = 0, reading_description = 0;
+       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 ) && is_field("Architecture", line))
+               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)) {
+                       pkg->arch_priority = get_arch_priority(pkg->architecture);
+               } 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;
@@ -172,7 +140,7 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                        goto dont_reset_flags;
                }
                else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line))
-                       pkg->conflicts_str = parse_comma_separated(line, &pkg->conflicts_count);
+                       pkg->conflicts_str = parse_list(line, &pkg->conflicts_count, ',', 0);
                break;
 
        case 'D':
@@ -182,7 +150,7 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                        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);
+                       pkg->depends_str = parse_list(line, &pkg->depends_count, ',', 0);
                break;
 
        case 'E':
@@ -200,43 +168,44 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                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)) {
+               if ((mask & PFM_INSTALLED_SIZE) && is_field("Installed-Size", line)) {
+                       char *tmp = parse_simple("Installed-Size", line);
+                       pkg->installed_size = strtoul(tmp, NULL, 0);
+                       free (tmp);
+               } 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);
+               if ((mask & PFM_MD5SUM) && 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))
+               else if ((mask & PFM_MD5SUM) && 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)) 
+               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);
+                       pkg->provides_str = parse_list(line, &pkg->provides_count, ',', 0);
                else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line))
-                       pkg->pre_depends_str = parse_comma_separated(line, &pkg->pre_depends_count);
+                       pkg->pre_depends_str = parse_list(line, &pkg->pre_depends_count, ',', 0);
                break;
 
        case 'R':
                if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
-                       pkg->recommends_str = parse_comma_separated(line, &pkg->recommends_count);
+                       pkg->recommends_str = parse_list(line, &pkg->recommends_count, ',', 0);
                else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
-                       pkg->replaces_str = parse_comma_separated(line, &pkg->replaces_count);
+                       pkg->replaces_str = parse_list(line, &pkg->replaces_count, ',', 0);
 
                break;
 
@@ -247,14 +216,16 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
                        pkg->sha256sum = parse_simple("SHA256sum", line);
 #endif
-               else if ((mask & PFM_SIZE) && is_field("Size", line))
-                       pkg->size = parse_simple("Size", line);
-               else if ((mask & PFM_SOURCE) && is_field("Source", line))
+               else if ((mask & PFM_SIZE) && is_field("Size", line)) {
+                       char *tmp = parse_simple("Size", line);
+                       pkg->size = strtoul(tmp, NULL, 0);
+                       free (tmp);
+               } 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);
+                       pkg->suggests_str = parse_list(line, &pkg->suggests_count, ',', 0);
                break;
 
        case 'T':
@@ -269,22 +240,30 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
 
        case ' ':
                if ((mask & PFM_DESCRIPTION) && reading_description) {
-                       pkg->description = xrealloc(pkg->description,
-                                               strlen(pkg->description)
-                                               + 1 + strlen(line) + 1);
-                       strcat(pkg->description, "\n");
+                       if (isatty(1)) {
+                               pkg->description = xrealloc(pkg->description,
+                                                       strlen(pkg->description)
+                                                       + 1 + strlen(line) + 1);
+                               strcat(pkg->description, "\n");
+                       } else {
+                               pkg->description = xrealloc(pkg->description,
+                                                       strlen(pkg->description)
+                                                       + 1 + strlen(line));
+                       }
                        strcat(pkg->description, (line));
                        goto dont_reset_flags;
-               } else if ((mask && PFM_CONFFILES) && reading_conffiles) {
+               } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
                        parse_conffiles(pkg, line);
                        goto dont_reset_flags;
                }
-               break;
 
+               /* FALLTHROUGH */
        default:
                /* For package lists, signifies end of package. */
-               if(line_is_blank(line))
-                       return 1;
+               if(line_is_blank(line)) {
+                       ret = 1;
+                       break;
+               }
        }
 
        reading_description = 0;
@@ -292,93 +271,6 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
 
 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;
-               }
-
-               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;
-               }
-
-               *nl = '\0';
-
-               lineno++;
-
-               if (pkg_parse_line(pkg, *buf0, mask))
-                       break;
-
-               buf = *buf0;
-               buflen = buf0len;
-               buf[0] = '\0';
-       };
-
-       if (pkg->name == NULL) {
-               /* probably just a blank line */
-               ret = EINVAL;
-       }
-
        return ret;
 }
 
@@ -390,8 +282,13 @@ pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
        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;
 }