Split importers to import.c.
16 parse_package(struct import_context *ctx, const char **atts, void *data)
18 const char *name = NULL, *version = NULL;
21 for (i = 0; atts[i]; i += 2) {
22 if (strcmp(atts[i], "name") == 0)
24 else if (strcmp(atts[i], "version") == 0)
25 version = atts[i + 1];
28 if (name == NULL || version == NULL) {
29 fprintf(stderr, "invalid package tag, "
30 "missing name or version attributes\n");
34 import_context_add_package(ctx, name, version);
38 parse_property(struct import_context *ctx, const char **atts, void *data)
40 const char *name = NULL, *version = NULL;
43 for (i = 0; atts[i]; i += 2) {
44 if (strcmp(atts[i], "name") == 0)
46 if (strcmp(atts[i], "version") == 0)
47 version = atts[i + 1];
51 fprintf(stderr, "invalid tag, missing name attribute\n");
55 import_context_add_property(ctx, data, name, version);
59 start_element(void *data, const char *name, const char **atts)
61 struct import_context *ctx = data;
63 if (strcmp(name, "package") == 0)
64 parse_package(ctx, atts, NULL);
65 else if (strcmp(name, "requires") == 0)
66 parse_property(ctx, atts, &ctx->requires);
67 else if (strcmp(name, "provides") == 0)
68 parse_property(ctx, atts, &ctx->provides);
72 end_element (void *data, const char *name)
74 struct import_context *ctx = data;
76 if (strcmp(name, "package") == 0)
77 import_context_finish_package(ctx);
81 import_rzr_file(struct import_context *ctx, const char *filename)
88 unsigned char hash[20];
90 fd = open(filename, O_RDONLY);
91 if (fstat(fd, &stat) < 0)
93 p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
97 parser = XML_ParserCreate(NULL);
98 XML_SetUserData(parser, ctx);
99 XML_SetElementHandler(parser, start_element, end_element);
100 if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
102 "%s at line %d, %s\n",
103 XML_ErrorString(XML_GetErrorCode(parser)),
104 XML_GetCurrentLineNumber(parser),
109 XML_ParserFree(parser);
112 SHA1_Update(&sha1, p, stat.st_size);
113 SHA1_Final(hash, &sha1);
117 munmap(p, stat.st_size);
123 razor_import_rzr_files(int count, const char *files[])
125 struct import_context ctx;
128 razor_prepare_import(&ctx);
130 for (i = 0; i < count; i++) {
131 if (import_rzr_file(&ctx, files[i]) < 0) {
132 fprintf(stderr, "failed to import %s\n", files[i]);
137 return razor_finish_import(&ctx);
140 /* Import a yum filelist as a razor package set. */
144 YUM_STATE_PACKAGE_NAME
148 struct import_context ctx;
149 struct import_property_context *current_property_context;
155 yum_start_element(void *data, const char *name, const char **atts)
157 struct yum_context *ctx = data;
158 const char *n, *version;
161 if (strcmp(name, "name") == 0) {
162 ctx->state = YUM_STATE_PACKAGE_NAME;
163 } else if (strcmp(name, "version") == 0) {
165 for (i = 0; atts[i]; i += 2) {
166 if (strcmp(atts[i], "ver") == 0)
167 version = atts[i + 1];
169 import_context_add_package(&ctx->ctx, ctx->name, version);
170 } else if (strcmp(name, "rpm:requires") == 0) {
171 ctx->current_property_context = &ctx->ctx.requires;
172 } else if (strcmp(name, "rpm:provides") == 0) {
173 ctx->current_property_context = &ctx->ctx.provides;
174 } else if (strcmp(name, "rpm:entry") == 0 &&
175 ctx->current_property_context != NULL) {
178 for (i = 0; atts[i]; i += 2) {
179 if (strcmp(atts[i], "name") == 0)
181 else if (strcmp(atts[i], "ver") == 0)
182 version = atts[i + 1];
186 fprintf(stderr, "invalid rpm:entry, "
187 "missing name or version attributes\n");
191 import_context_add_property(&ctx->ctx,
192 ctx->current_property_context,
198 yum_end_element (void *data, const char *name)
200 struct yum_context *ctx = data;
202 if (strcmp(name, "package") == 0) {
204 import_context_finish_package(&ctx->ctx);
205 } else if (strcmp(name, "name") == 0) {
207 } else if (strcmp(name, "rpm:requires") == 0) {
208 ctx->current_property_context = NULL;
209 } else if (strcmp(name, "rpm:provides") == 0) {
210 ctx->current_property_context = NULL;
215 yum_character_data (void *data, const XML_Char *s, int len)
217 struct yum_context *ctx = data;
219 if (ctx->state == YUM_STATE_PACKAGE_NAME)
220 ctx->name = strndup(s, len);
224 razor_set_create_from_yum_filelist(int fd)
226 struct yum_context ctx;
231 razor_prepare_import(&ctx.ctx);
233 parser = XML_ParserCreate(NULL);
234 XML_SetUserData(parser, &ctx);
235 XML_SetElementHandler(parser, yum_start_element, yum_end_element);
236 XML_SetCharacterDataHandler(parser, yum_character_data);
239 len = read(fd, buf, sizeof buf);
242 "couldn't read input: %s\n", strerror(errno));
247 if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
250 XML_ErrorString(XML_GetErrorCode(parser)),
251 XML_GetCurrentLineNumber(parser));
256 XML_ParserFree(parser);
258 return razor_finish_import(&ctx.ctx);