1.1 --- a/src/import-yum.c Thu Oct 09 17:27:41 2014 +0100
1.2 +++ b/src/import-yum.c Tue Apr 24 19:27:29 2018 +0100
1.3 @@ -23,6 +23,7 @@
1.4 #include <string.h>
1.5 #include <stdio.h>
1.6 #include <stdint.h>
1.7 +#include <assert.h>
1.8 #include <sys/stat.h>
1.9 #include <unistd.h>
1.10 #include <fcntl.h>
1.11 @@ -31,6 +32,7 @@
1.12 #include <expat.h>
1.13 #include <zlib.h>
1.14 #include "razor.h"
1.15 +#include "import.h"
1.16
1.17 /* Import a yum filelist as a razor package set. */
1.18
1.19 @@ -282,14 +284,76 @@
1.20
1.21 #define XML_BUFFER_SIZE 4096
1.22
1.23 +struct razor_stream {
1.24 + z_stream strm;
1.25 + void *in;
1.26 + size_t in_length;
1.27 +};
1.28 +
1.29 +static int razor_stream_open(struct razor_stream *rs, const char *uri,
1.30 + struct razor_error **error)
1.31 +{
1.32 + rs->strm.zalloc = Z_NULL;
1.33 + rs->strm.zfree = Z_NULL;
1.34 + rs->strm.opaque = Z_NULL;
1.35 + rs->strm.avail_in = 0;
1.36 + rs->strm.next_in = Z_NULL;
1.37 +
1.38 + if (inflateInit2(&rs->strm, 15 + 16) != Z_OK) {
1.39 + razor_set_error(error, RAZOR_GENERAL_ERROR,
1.40 + RAZOR_GENERAL_ERROR_FAILED, uri,
1.41 + "Failed to initialize inflator");
1.42 + return -1;
1.43 + }
1.44 +
1.45 + rs->in = razor_uri_get_contents(uri, &rs->in_length, 0, error);
1.46 + if (!rs->in) {
1.47 + (void)inflateEnd(&rs->strm);
1.48 + return -1;
1.49 + }
1.50 +
1.51 + rs->strm.avail_in = rs->in_length;
1.52 + rs->strm.next_in = rs->in;
1.53 +
1.54 + return 0;
1.55 +}
1.56 +
1.57 +static ssize_t
1.58 +razor_stream_read(struct razor_stream *rs, unsigned char *buf, size_t len)
1.59 +{
1.60 + int r;
1.61 +
1.62 + rs->strm.avail_out = len;
1.63 + rs->strm.next_out = buf;
1.64 +
1.65 + r = inflate(&rs->strm, Z_NO_FLUSH);
1.66 + assert(r != Z_STREAM_ERROR); /* state not clobbered */
1.67 + switch (r) {
1.68 + case Z_NEED_DICT:
1.69 + case Z_DATA_ERROR:
1.70 + case Z_MEM_ERROR:
1.71 + return -1;
1.72 + }
1.73 +
1.74 + return len - rs->strm.avail_out;
1.75 +}
1.76 +
1.77 +static void razor_stream_close(struct razor_stream *rs)
1.78 +{
1.79 + (void)inflateEnd(&rs->strm);
1.80 + (void)razor_uri_free_contents(rs->in, rs->in_length);
1.81 +}
1.82 +
1.83 struct razor_set *
1.84 -razor_set_create_from_yum(void)
1.85 +razor_set_create_from_yum(const char *yum_uri)
1.86 {
1.87 struct yum_context ctx={0};
1.88 + char *uri;
1.89 void *buf;
1.90 - int len;
1.91 - gzFile primary, filelists;
1.92 + ssize_t len;
1.93 XML_ParsingStatus status;
1.94 + struct razor_error *error = NULL;
1.95 + struct razor_stream primary, filelists;
1.96
1.97 ctx.importer = razor_importer_create();
1.98 ctx.state = YUM_STATE_BEGIN;
1.99 @@ -310,16 +374,37 @@
1.100 XML_SetCharacterDataHandler(ctx.filelists_parser,
1.101 yum_character_data);
1.102
1.103 - primary = gzopen("primary.xml.gz", "rb");
1.104 - if (primary == NULL) {
1.105 - perror("primary.xml.gz");
1.106 + uri = razor_path_relative_to_uri(yum_uri, "repodata/primary.xml.gz",
1.107 + &error);
1.108 + if (!uri) {
1.109 + fprintf(stderr, "%s: %s\n", yum_uri,
1.110 + razor_error_get_msg(error));
1.111 + razor_error_free(error);
1.112 return NULL;
1.113 }
1.114 - filelists = gzopen("filelists.xml.gz", "rb");
1.115 - if (filelists == NULL) {
1.116 - perror("filelists.xml.gz");
1.117 + if (razor_stream_open(&primary, uri, &error)) {
1.118 + fprintf(stderr, "%s: %s\n", uri, razor_error_get_msg(error));
1.119 + free(uri);
1.120 return NULL;
1.121 }
1.122 + free(uri);
1.123 +
1.124 + uri = razor_path_relative_to_uri(yum_uri, "repodata/filelists.xml.gz",
1.125 + &error);
1.126 + if (!uri) {
1.127 + razor_stream_close(&primary);
1.128 + fprintf(stderr, "%s: %s\n", yum_uri,
1.129 + razor_error_get_msg(error));
1.130 + razor_error_free(error);
1.131 + return NULL;
1.132 + }
1.133 + if (razor_stream_open(&filelists, uri, &error)) {
1.134 + razor_stream_close(&primary);
1.135 + fprintf(stderr, "%s: %s\n", uri, razor_error_get_msg(error));
1.136 + free(uri);
1.137 + return NULL;
1.138 + }
1.139 + free(uri);
1.140
1.141 ctx.current_parser = ctx.primary_parser;
1.142
1.143 @@ -336,9 +421,11 @@
1.144 buf = XML_GetBuffer(ctx.current_parser,
1.145 XML_BUFFER_SIZE);
1.146 if (ctx.current_parser == ctx.primary_parser)
1.147 - len = gzread(primary, buf, XML_BUFFER_SIZE);
1.148 + len = razor_stream_read(&primary, buf,
1.149 + XML_BUFFER_SIZE);
1.150 else
1.151 - len = gzread(filelists, buf, XML_BUFFER_SIZE);
1.152 + len = razor_stream_read(&filelists, buf,
1.153 + XML_BUFFER_SIZE);
1.154 if (len < 0) {
1.155 fprintf(stderr,
1.156 "couldn't read input: %s\n",
1.157 @@ -357,8 +444,8 @@
1.158 XML_ParserFree(ctx.primary_parser);
1.159 XML_ParserFree(ctx.filelists_parser);
1.160
1.161 - gzclose(primary);
1.162 - gzclose(filelists);
1.163 + razor_stream_close(&primary);
1.164 + razor_stream_close(&filelists);
1.165
1.166 printf ("\nsaving\n");
1.167 return razor_importer_finish(ctx.importer);