Clean up the merging code a bit.
Use the hashtable instead of reusing razor_importer for this.
Introduce struct razor_merger for this. All this is steps
towards making the merging policy external to the core razor_set code.
8 #include <rpm/rpmlib.h>
13 #define RPM_LEAD_SIZE 96
16 unsigned char magic[4];
28 unsigned char magic[4];
29 unsigned char reserved[4];
34 struct rpm_header_index {
42 struct rpm_header_index *name;
43 struct rpm_header_index *version;
44 struct rpm_header_index *flags;
48 struct rpm_header *signature;
49 struct rpm_header *header;
51 struct rpm_header_index *name;
52 struct rpm_header_index *version;
53 struct rpm_header_index *release;
55 struct rpm_header_index *dirnames;
56 struct rpm_header_index *dirindexes;
57 struct rpm_header_index *basenames;
58 struct rpm_header_index *filesizes;
59 struct rpm_header_index *filemodes;
60 struct rpm_header_index *filestates;
63 struct properties provides;
64 struct properties requires;
65 struct properties obsoletes;
66 struct properties conflicts;
74 #define ALIGN(value, base) (((value) + (base - 1)) & ~((base) - 1))
77 import_properties(struct razor_importer *importer,
78 struct properties *properties,
79 const char *pool, unsigned long type)
81 const char *name, *version;
84 /* assert: count is the same for all arrays */
86 if (properties->name == NULL)
89 count = ntohl(properties->name->count);
90 name = pool + ntohl(properties->name->offset);
91 version = pool + ntohl(properties->version->offset);
92 for (i = 0; i < count; i++) {
93 razor_importer_add_property(importer, name, version, type);
94 name += strlen(name) + 1;
95 version += strlen(version) + 1;
100 import_files(struct razor_importer *importer, struct razor_rpm *rpm)
103 unsigned long *index;
107 /* assert: count is the same for all arrays */
109 if (rpm->dirnames == NULL)
112 count = ntohl(rpm->basenames->count);
113 index = (unsigned long *) (rpm->pool + ntohl(rpm->dirindexes->offset));
114 name = rpm->pool + ntohl(rpm->basenames->offset);
115 for (i = 0; i < count; i++) {
116 snprintf(buffer, sizeof buffer,
117 "%s%s", rpm->dirs[ntohl(*index)], name);
118 razor_importer_add_file(importer, buffer);
119 name += strlen(name) + 1;
125 razor_rpm_open(const char *filename)
127 struct razor_rpm *rpm;
128 struct rpm_header_index *base, *index;
130 int fd, nindex, hsize, i, count;
133 rpm = malloc(sizeof *rpm);
134 memset(rpm, 0, sizeof *rpm);
135 if (stat(filename, &buf) < 0) {
136 fprintf(stderr, "no such file %s (%m)\n", filename);
140 fd = open(filename, O_RDONLY);
142 fprintf(stderr, "couldn't open %s\n", filename);
145 rpm->size = buf.st_size;
146 rpm->map = mmap(NULL, rpm->size, PROT_READ, MAP_PRIVATE, fd, 0);
147 if (rpm->map == MAP_FAILED) {
148 fprintf(stderr, "couldn't mmap %s\n", filename);
153 rpm->signature = rpm->map + RPM_LEAD_SIZE;
154 nindex = ntohl(rpm->signature->nindex);
155 hsize = ntohl(rpm->signature->hsize);
156 rpm->header = (void *) (rpm->signature + 1) +
157 ALIGN(nindex * sizeof *index + hsize, 8);
158 nindex = ntohl(rpm->header->nindex);
159 hsize = ntohl(rpm->header->hsize);
160 rpm->payload = (void *) (rpm->header + 1) +
161 nindex * sizeof *index + hsize;
163 base = (struct rpm_header_index *) (rpm->header + 1);
164 rpm->pool = (void *) base + nindex * sizeof *index;
166 for (i = 0; i < nindex; i++) {
168 switch (ntohl(index->tag)) {
173 rpm->version = index;
176 rpm->release = index;
179 case RPMTAG_REQUIRENAME:
180 rpm->requires.name = index;
182 case RPMTAG_REQUIREVERSION:
183 rpm->requires.version = index;
185 case RPMTAG_REQUIREFLAGS:
186 rpm->requires.flags = index;
189 case RPMTAG_PROVIDENAME:
190 rpm->provides.name = index;
192 case RPMTAG_PROVIDEVERSION:
193 rpm->provides.version = index;
195 case RPMTAG_PROVIDEFLAGS:
196 rpm->provides.flags = index;
199 case RPMTAG_OBSOLETENAME:
200 rpm->obsoletes.name = index;
202 case RPMTAG_OBSOLETEVERSION:
203 rpm->obsoletes.version = index;
205 case RPMTAG_OBSOLETEFLAGS:
206 rpm->obsoletes.flags = index;
209 case RPMTAG_CONFLICTNAME:
210 rpm->conflicts.name = index;
212 case RPMTAG_CONFLICTVERSION:
213 rpm->conflicts.version = index;
215 case RPMTAG_CONFLICTFLAGS:
216 rpm->conflicts.flags = index;
219 case RPMTAG_DIRINDEXES:
220 rpm->dirindexes = index;
222 case RPMTAG_BASENAMES:
223 rpm->basenames = index;
225 case RPMTAG_DIRNAMES:
226 rpm->dirnames = index;
228 case RPMTAG_FILESIZES:
229 rpm->filesizes = index;
231 case RPMTAG_FILEMODES:
232 rpm->filemodes = index;
234 case RPMTAG_FILESTATES:
235 rpm->filestates = index;
240 /* Look up dir names now so we can index them directly. */
241 if (rpm->dirnames != NULL) {
242 count = ntohl(rpm->dirnames->count);
243 rpm->dirs = calloc(count, sizeof *rpm->dirs);
244 name = rpm->pool + ntohl(rpm->dirnames->offset);
245 for (i = 0; i < count; i++) {
247 name += strlen(name) + 1;
254 struct cpio_file_header {
273 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
274 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
275 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
276 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
277 #define COMMENT 0x10 /* bit 4 set: file comment present */
278 #define RESERVED 0xE0 /* bits 5..7: reserved */
281 create_path(const char *root, const char *path,
282 const char *name, unsigned mode, int *fd)
284 char buffer[256], *p;
285 const char *slash, *next;
288 /* Create all sub-directories in dir and then create name. We
289 * know root exists and is a dir, root does not end in a '/',
290 * and path has a leading '/'. */
292 strcpy(buffer, root);
293 p = buffer + strlen(buffer);
295 for (slash = path; slash[1] != '\0'; slash = next) {
296 next = strchr(slash + 1, '/');
297 memcpy(p, slash, next - slash);
301 if (stat(buffer, &buf) == 0) {
302 if (!S_ISDIR(buf.st_mode)) {
304 "%s exists but is not a directory\n",
308 } else if (mkdir(buffer, 0777) < 0) {
309 fprintf(stderr, "failed to make directory %s: %m\n",
313 /* FIXME: permissions */
319 switch (mode >> 12) {
322 *fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, mode & 0x1ff);
326 return mkdir(buffer, mode & 0x1ff);
331 razor_rpm_install(struct razor_rpm *rpm, const char *root)
334 unsigned char payload[32768], *gz_header;
336 int err, method, flags, count, i, fd, written;
337 struct cpio_file_header *header;
338 unsigned long *size, *index, rest, length;
339 unsigned short *mode;
340 const char *name, *dir;
343 if (stat(root, &buf) < 0 || !S_ISDIR(buf.st_mode)) {
345 "root installation directory \"%s\" does not exist\n",
350 gz_header = rpm->payload;
351 if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
352 fprintf(stderr, "payload section doesn't have gz header\n");
356 method = gz_header[2];
357 flags = gz_header[3];
359 if (method != Z_DEFLATED || flags != 0) {
361 "unknown payload compression method or flags set\n");
365 stream.zalloc = NULL;
367 stream.opaque = NULL;
369 stream.next_in = gz_header + 10;
370 stream.avail_in = (rpm->map + rpm->size) - (void *) stream.next_in;
371 stream.next_out = NULL;
372 stream.avail_out = 0;
374 err = inflateInit2(&stream, -MAX_WBITS);
376 fprintf(stderr, "inflateInit error: %d\n", err);
380 count = ntohl(rpm->basenames->count);
381 size = (unsigned long *) (rpm->pool + ntohl(rpm->filesizes->offset));
382 index = (unsigned long *) (rpm->pool + ntohl(rpm->dirindexes->offset));
383 mode = (unsigned short *) (rpm->pool + ntohl(rpm->filemodes->offset));
384 name = rpm->pool + ntohl(rpm->basenames->offset);
385 for (i = 0; i < count; i++) {
386 dir = rpm->dirs[ntohl(*index)];
387 snprintf(buffer, sizeof buffer, "%s%s", dir, name);
389 stream.next_out = payload;
390 /* Plus two for the leading '.' and the terminating NUL. */
392 ALIGN(sizeof *header + strlen(buffer) + 2, 4);
393 err = inflate(&stream, Z_SYNC_FLUSH);
395 fprintf(stderr, "inflate error: %d\n", err);
399 header = (struct cpio_file_header *) payload;
401 /* FIXME: Figure out if it's a symlink, device file,
402 * directorys or whatever. Maybe do this upfront. */
403 if (create_path(root, dir, name, ntohs(*mode), &fd) < 0)
405 if (ntohs(*mode) >> 12 == XDIR)
411 if (ALIGN(rest, 4) > sizeof payload)
412 length = sizeof payload;
415 stream.next_out = payload;
416 stream.avail_out = ALIGN(length, 4);
417 err = inflate(&stream, Z_SYNC_FLUSH);
418 if (err != Z_OK && err != Z_STREAM_END) {
420 "inflate error: %d (%m)\n", err);
424 stream.next_out = payload;
426 written = write(fd, stream.next_out, length);
428 fprintf(stderr, "write error: %m\n");
434 if (fd > 0 && close(fd) < 0) {
435 fprintf(stderr, "failed to close \"%s/%s%s\": %m\n",
439 name += strlen(name) + 1;
445 err = inflateEnd(&stream);
448 fprintf(stderr, "inflateEnd error: %d\n", err);
456 razor_rpm_close(struct razor_rpm *rpm)
461 err = munmap(rpm->map, rpm->size);
468 razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
470 razor_importer_begin_package(importer,
471 rpm->pool + ntohl(rpm->name->offset),
472 rpm->pool + ntohl(rpm->version->offset));
474 import_properties(importer, &rpm->requires,
475 rpm->pool, RAZOR_PROPERTY_REQUIRES);
476 import_properties(importer, &rpm->provides,
477 rpm->pool, RAZOR_PROPERTY_PROVIDES);
478 import_properties(importer, &rpm->conflicts,
479 rpm->pool, RAZOR_PROPERTY_CONFLICTS);
480 import_properties(importer, &rpm->obsoletes,
481 rpm->pool, RAZOR_PROPERTY_OBSOLETES);
482 import_files(importer, rpm);
484 razor_importer_finish_package(importer);