diff -r df914f383f5c -r 203fa998c6df src/import-yum.c --- a/src/import-yum.c Thu Oct 09 17:27:41 2014 +0100 +++ b/src/import-yum.c Tue Apr 24 19:27:29 2018 +0100 @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include #include #include "razor.h" +#include "import.h" /* Import a yum filelist as a razor package set. */ @@ -282,14 +284,76 @@ #define XML_BUFFER_SIZE 4096 +struct razor_stream { + z_stream strm; + void *in; + size_t in_length; +}; + +static int razor_stream_open(struct razor_stream *rs, const char *uri, + struct razor_error **error) +{ + rs->strm.zalloc = Z_NULL; + rs->strm.zfree = Z_NULL; + rs->strm.opaque = Z_NULL; + rs->strm.avail_in = 0; + rs->strm.next_in = Z_NULL; + + if (inflateInit2(&rs->strm, 15 + 16) != Z_OK) { + razor_set_error(error, RAZOR_GENERAL_ERROR, + RAZOR_GENERAL_ERROR_FAILED, uri, + "Failed to initialize inflator"); + return -1; + } + + rs->in = razor_uri_get_contents(uri, &rs->in_length, 0, error); + if (!rs->in) { + (void)inflateEnd(&rs->strm); + return -1; + } + + rs->strm.avail_in = rs->in_length; + rs->strm.next_in = rs->in; + + return 0; +} + +static ssize_t +razor_stream_read(struct razor_stream *rs, unsigned char *buf, size_t len) +{ + int r; + + rs->strm.avail_out = len; + rs->strm.next_out = buf; + + r = inflate(&rs->strm, Z_NO_FLUSH); + assert(r != Z_STREAM_ERROR); /* state not clobbered */ + switch (r) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: + return -1; + } + + return len - rs->strm.avail_out; +} + +static void razor_stream_close(struct razor_stream *rs) +{ + (void)inflateEnd(&rs->strm); + (void)razor_uri_free_contents(rs->in, rs->in_length); +} + struct razor_set * -razor_set_create_from_yum(void) +razor_set_create_from_yum(const char *yum_uri) { struct yum_context ctx={0}; + char *uri; void *buf; - int len; - gzFile primary, filelists; + ssize_t len; XML_ParsingStatus status; + struct razor_error *error = NULL; + struct razor_stream primary, filelists; ctx.importer = razor_importer_create(); ctx.state = YUM_STATE_BEGIN; @@ -310,16 +374,37 @@ XML_SetCharacterDataHandler(ctx.filelists_parser, yum_character_data); - primary = gzopen("primary.xml.gz", "rb"); - if (primary == NULL) { - perror("primary.xml.gz"); + uri = razor_path_relative_to_uri(yum_uri, "repodata/primary.xml.gz", + &error); + if (!uri) { + fprintf(stderr, "%s: %s\n", yum_uri, + razor_error_get_msg(error)); + razor_error_free(error); return NULL; } - filelists = gzopen("filelists.xml.gz", "rb"); - if (filelists == NULL) { - perror("filelists.xml.gz"); + if (razor_stream_open(&primary, uri, &error)) { + fprintf(stderr, "%s: %s\n", uri, razor_error_get_msg(error)); + free(uri); return NULL; } + free(uri); + + uri = razor_path_relative_to_uri(yum_uri, "repodata/filelists.xml.gz", + &error); + if (!uri) { + razor_stream_close(&primary); + fprintf(stderr, "%s: %s\n", yum_uri, + razor_error_get_msg(error)); + razor_error_free(error); + return NULL; + } + if (razor_stream_open(&filelists, uri, &error)) { + razor_stream_close(&primary); + fprintf(stderr, "%s: %s\n", uri, razor_error_get_msg(error)); + free(uri); + return NULL; + } + free(uri); ctx.current_parser = ctx.primary_parser; @@ -336,9 +421,11 @@ buf = XML_GetBuffer(ctx.current_parser, XML_BUFFER_SIZE); if (ctx.current_parser == ctx.primary_parser) - len = gzread(primary, buf, XML_BUFFER_SIZE); + len = razor_stream_read(&primary, buf, + XML_BUFFER_SIZE); else - len = gzread(filelists, buf, XML_BUFFER_SIZE); + len = razor_stream_read(&filelists, buf, + XML_BUFFER_SIZE); if (len < 0) { fprintf(stderr, "couldn't read input: %s\n", @@ -357,8 +444,8 @@ XML_ParserFree(ctx.primary_parser); XML_ParserFree(ctx.filelists_parser); - gzclose(primary); - gzclose(filelists); + razor_stream_close(&primary); + razor_stream_close(&filelists); printf ("\nsaving\n"); return razor_importer_finish(ctx.importer);