3 #include "build/rpmbuild.h"
5 #include "rpm_malloc.h"
8 static int labelstrlistToLabelList(char * str, int length,
9 struct oldrpmdbLabel ** list);
10 static char * getScript(char * which, struct oldrpmdb *oldrpmdb,
11 struct oldrpmdbLabel label);
13 static char * prefix = "/var/lib/rpm";
15 char * oldrpmdbLabelToLabelstr(struct oldrpmdbLabel label, int withFileNum) {
19 if (withFileNum && label.fileNumber > -1)
20 c = malloc(strlen(label.name) + strlen(label.version) +
21 strlen(label.release) + 10);
23 c = malloc(strlen(label.name) + strlen(label.version) +
24 strlen(label.release) + 3);
26 strcpy(c, label.name);
28 strcat(c, label.version);
30 strcat(c, label.release);
32 if (withFileNum && label.fileNumber > -1) {
33 sprintf(buffer, "%d", label.fileNumber);
41 int oldrpmdbLabelstrToLabel(char * str, int length, struct oldrpmdbLabel * label) {
44 label->freeType = RPMDB_FREENAME;
46 label->name = malloc(length + 1);
50 memcpy(label->name, str, length);
51 label->name[length] = '\0';
54 while (*chptr != ':') chptr++;
56 label->version = ++chptr;
57 while (*chptr != ':') chptr++;
59 label->release = chptr + 1;
61 label->fileNumber = -1;
63 /* there might be a path number tagged on to the end of this */
64 while ((chptr - label->name) < length && *chptr != ':') chptr++;
65 if ((chptr - label->name) < length) {
67 label->fileNumber = atoi(chptr + 1);
73 static int labelstrlistToLabelList(char * str, int length,
74 struct oldrpmdbLabel ** list) {
75 char * start, * chptr;
76 struct oldrpmdbLabel * head = NULL;
77 struct oldrpmdbLabel * tail = NULL;
78 struct oldrpmdbLabel * label;
81 for (chptr = start; (chptr - str) < length; chptr++) {
82 /* spaces following a space get ignored */
83 if (*chptr == ' ' && start < chptr) {
84 label = malloc(sizeof(struct oldrpmdbLabel));
86 oldrpmdbFreeLabelList(head);
89 if (oldrpmdbLabelstrToLabel(start, chptr - start, label)) {
91 oldrpmdbFreeLabelList(head);
107 /* a space on the end would break things horribly w/o this test */
109 label = malloc(sizeof(struct oldrpmdbLabel));
111 oldrpmdbFreeLabelList(head);
114 if (oldrpmdbLabelstrToLabel(start, chptr - start, label)) {
116 oldrpmdbFreeLabelList(head);
135 /* returns 0 on success, -1 on failure */
136 int oldrpmdbOpen(struct oldrpmdb * oldrpmdb) {
137 unsigned int gdbmFlags;
141 oldrpmdb->rpmdbError = RPMDB_NONE;
143 gdbmFlags = GDBM_READER;
145 strcpy(path, prefix);
146 strcat(path, "/packages");
147 oldrpmdb->packages = gdbm_open(path, 1024, gdbmFlags, 0644, NULL);
148 if (!oldrpmdb->packages) {
149 rpmError(RPMERR_GDBMOPEN, path, gdbm_strerror(gdbm_errno));
153 strcpy(path, prefix);
154 strcat(path, "/nameidx");
155 oldrpmdb->nameIndex = gdbm_open(path, 1024, gdbmFlags, 0644, NULL);
156 if (!oldrpmdb->packages) {
157 rpmError(RPMERR_GDBMOPEN, path, gdbm_strerror(gdbm_errno));
161 strcpy(path, prefix);
162 strcat(path, "/pathidx");
163 oldrpmdb->pathIndex = gdbm_open(path, 1024, gdbmFlags, 0644, NULL);
164 if (!oldrpmdb->packages) {
165 rpmError(RPMERR_GDBMOPEN, path, gdbm_strerror(gdbm_errno));
169 strcpy(path, prefix);
170 strcat(path, "/iconidx");
171 oldrpmdb->iconIndex = gdbm_open(path, 1024, gdbmFlags, 0644, NULL);
172 if (!oldrpmdb->iconIndex) {
173 rpmError(RPMERR_GDBMOPEN, path, gdbm_strerror(gdbm_errno));
177 strcpy(path, prefix);
178 strcat(path, "/groupindex");
179 oldrpmdb->groupIndex = gdbm_open(path, 1024, gdbmFlags, 0644, NULL);
180 if (!oldrpmdb->packages) {
181 rpmError(RPMERR_GDBMOPEN, path, gdbm_strerror(gdbm_errno));
185 strcpy(path, prefix);
186 strcat(path, "/postidx");
187 oldrpmdb->postIndex = gdbm_open(path, 1024, gdbmFlags, 0644, NULL);
188 if (!oldrpmdb->postIndex) {
189 rpmError(RPMERR_GDBMOPEN, path, gdbm_strerror(gdbm_errno));
194 oldrpmdbClose(oldrpmdb);
201 void oldrpmdbClose(struct oldrpmdb * oldrpmdb) {
202 gdbm_close(oldrpmdb->packages);
203 gdbm_close(oldrpmdb->nameIndex);
204 gdbm_close(oldrpmdb->pathIndex);
205 gdbm_close(oldrpmdb->postIndex);
206 gdbm_close(oldrpmdb->groupIndex);
207 gdbm_close(oldrpmdb->iconIndex);
210 struct oldrpmdbLabel * oldrpmdbGetAllLabels(struct oldrpmdb * oldrpmdb) {
213 struct oldrpmdbLabel * head = NULL;
214 struct oldrpmdbLabel * tail = NULL;
215 struct oldrpmdbLabel * label;
217 oldrpmdb->rpmdbError = RPMDB_NONE;
219 rec = gdbm_firstkey(oldrpmdb->packages);
221 label = malloc(sizeof(struct oldrpmdbLabel));
223 oldrpmdbFreeLabelList(head);
224 oldrpmdb->rpmdbError = RPMDB_NO_MEMORY;
227 if (oldrpmdbLabelstrToLabel(rec.dptr, rec.dsize, label)) {
229 oldrpmdbFreeLabelList(head);
230 oldrpmdb->rpmdbError = RPMDB_NO_MEMORY;
242 rec = gdbm_nextkey(oldrpmdb->packages, rec);
248 struct oldrpmdbLabel * oldrpmdbFindPackagesByFile(struct oldrpmdb * oldrpmdb, char * path) {
251 struct oldrpmdbLabel * list;
253 oldrpmdb->rpmdbError = RPMDB_NONE;
256 key.dsize = strlen(path);
257 rec = gdbm_fetch(oldrpmdb->pathIndex, key);
261 if (labelstrlistToLabelList(rec.dptr, rec.dsize, &list)) {
263 oldrpmdb->rpmdbError = RPMDB_NO_MEMORY;
271 struct oldrpmdbLabel * oldrpmdbFindPackagesByLabel(struct oldrpmdb * oldrpmdb,
272 struct oldrpmdbLabel label)
274 /* the Name has to be here. The version/release fields optionally
275 restrict the search. Either will do. */
280 struct oldrpmdbLabel * list;
281 struct oldrpmdbLabel * prospect;
282 struct oldrpmdbLabel * parent;
285 oldrpmdb->rpmdbError = RPMDB_NONE;
287 key.dptr = label.name;
288 key.dsize = strlen(label.name);
289 rec = gdbm_fetch(oldrpmdb->nameIndex, key);
293 if (labelstrlistToLabelList(rec.dptr, rec.dsize, &list)) {
295 oldrpmdb->rpmdbError = RPMDB_NO_MEMORY;
304 if (label.version && strcmp(label.version, prospect->version))
306 else if (label.release && strcmp(label.release, prospect->release))
310 oldrpmdbFreeLabel(*prospect);
312 list = prospect->next;
316 parent->next = prospect->next;
318 prospect = parent->next;
321 prospect = prospect->next;
328 struct oldrpmdbLabel oldrpmdbMakeLabel(char * name, char * version, char * release,
329 int fileNumber, enum oldrpmdbFreeType freeType) {
330 struct oldrpmdbLabel label;
333 label.freeType = freeType;
335 label.version = version;
336 label.release = release;
337 label.fileNumber = fileNumber;
342 void oldrpmdbFreeLabelList(struct oldrpmdbLabel * list) {
343 struct oldrpmdbLabel * saved;
346 oldrpmdbFreeLabel(*list);
353 void oldrpmdbFreeLabel(struct oldrpmdbLabel label) {
354 if (label.freeType == RPMDB_NOFREE) return;
357 if (label.freeType == RPMDB_FREEALL) {
363 /* Returns NULL on error */
364 char * oldrpmdbGetPackageGroup(struct oldrpmdb * oldrpmdb, struct oldrpmdbLabel label) {
368 key.dptr = label.name;
369 key.dsize = strlen(label.name);
371 rec = gdbm_fetch(oldrpmdb->groupIndex, key);
373 return strdup("Unknown");
376 g = malloc(rec.dsize + 1);
377 strncpy(g, rec.dptr, rec.dsize);
384 static char * getScript(char * which, struct oldrpmdb *oldrpmdb,
385 struct oldrpmdbLabel label) {
387 char * labelstr, * l;
389 labelstr = oldrpmdbLabelToLabelstr(label, 0);
390 labelstr = realloc(labelstr, strlen(labelstr) + 10);
391 strcat(labelstr, ":");
392 strcat(labelstr, which);
395 key.dsize = strlen(labelstr);
397 rec = gdbm_fetch(oldrpmdb->postIndex, key);
400 if (!rec.dptr) return NULL;
402 l = malloc(rec.dsize + 1);
403 strncpy(l, rec.dptr, rec.dsize);
411 char *oldrpmdbGetPackagePostun (struct oldrpmdb *oldrpmdb,
412 struct oldrpmdbLabel label) {
413 return getScript("post", oldrpmdb, label);
416 char *oldrpmdbGetPackagePreun (struct oldrpmdb *oldrpmdb,
417 struct oldrpmdbLabel label) {
418 return getScript("pre", oldrpmdb, label);
421 /* Returns NULL on error or if no icon exists */
422 char * oldrpmdbGetPackageGif(struct oldrpmdb * oldrpmdb,
423 struct oldrpmdbLabel label, int * size) {
427 labelstr = oldrpmdbLabelToLabelstr(label, 0);
430 key.dsize = strlen(labelstr);
432 rec = gdbm_fetch(oldrpmdb->iconIndex, key);
443 /* return 0 on success, 1 on failure */
444 int oldrpmdbGetPackageInfo(struct oldrpmdb * oldrpmdb, struct oldrpmdbLabel label,
445 struct oldrpmdbPackageInfo * pinfo) {
447 char ** list, ** prelist;
452 labelstr = oldrpmdbLabelToLabelstr(label, 0);
454 rpmMessage(RPMMESS_DEBUG, _("pulling %s from database\n"), labelstr);
457 key.dsize = strlen(labelstr);
459 rec = gdbm_fetch(oldrpmdb->packages, key);
461 rpmError(RPMERR_OLDDBCORRUPT, _("package not found in database"));
467 list = splitString(rec.dptr, rec.dsize, '\1');
470 pinfo->version = strdup(list[1]);
471 pinfo->release = strdup(list[2]);
472 /* list[3] == "1" always */
473 pinfo->name = malloc(strlen(list[0]) + strlen(list[4]) + 2);
474 strcpy(pinfo->name, list[0]);
475 if (strlen(list[4])) {
476 strcat(pinfo->name, "-");
477 strcat(pinfo->name, list[4]);
479 pinfo->labelstr = malloc(strlen(pinfo->name) + strlen(pinfo->version) +
480 strlen(pinfo->release) + 3);
481 strcpy(pinfo->labelstr, pinfo->name);
482 strcat(pinfo->labelstr, ":");
483 strcat(pinfo->labelstr, pinfo->version);
484 strcat(pinfo->labelstr, ":");
485 strcat(pinfo->labelstr, pinfo->release);
487 pinfo->preamble = strdup(list[5]);
488 pinfo->installTime = atoi(list[6]);
489 pinfo->fileCount = atoi(list[7]);
491 prelist = splitString(pinfo->preamble, strlen(pinfo->preamble), '\n');
493 /* these are optional */
494 pinfo->distribution = NULL;
495 pinfo->vendor = NULL;
496 pinfo->description = NULL;
497 pinfo->copyright = NULL;
499 for (strptr = prelist; *strptr; strptr++) {
500 if (!strncasecmp("Description: ", *strptr, 13))
501 pinfo->description = strdup((*strptr) + 13);
502 else if (!strncasecmp("Copyright: ", *strptr, 11))
503 pinfo->copyright = strdup((*strptr) + 11);
504 else if (!strncasecmp("Distribution: ", *strptr, 14))
505 pinfo->distribution = strdup((*strptr) + 14);
506 else if (!strncasecmp("Vendor: ", *strptr, 8))
507 pinfo->vendor = strdup((*strptr) + 8);
508 else if (!strncasecmp("size: ", *strptr, 6))
509 pinfo->size = atoi((*strptr) + 6);
510 else if (!strncasecmp("BuildDate: ", *strptr, 11))
511 pinfo->buildTime =atoi((*strptr) + 11);
512 else if (!strncasecmp("BuildHost: ", *strptr, 11))
513 pinfo->buildHost = strdup((*strptr) + 11);
515 freeSplitString(prelist);
517 if (!pinfo->vendor) pinfo->vendor = strdup("");
518 if (!pinfo->description) pinfo->description = strdup("");
519 if (!pinfo->distribution) pinfo->distribution = strdup("");
520 if (!pinfo->copyright) {
521 pinfo->copyright = strdup("");
522 fprintf(stdout, _("no copyright!\n"));
525 pinfo->files = malloc(sizeof(struct oldrpmFileInfo) * pinfo->fileCount);
528 for (i = 0; i < pinfo->fileCount; i++) {
529 oldrpmfileFromInfoLine(list[j], list[j + 1], list[j + 2],
534 freeSplitString(list);
539 void oldrpmdbFreePackageInfo(struct oldrpmdbPackageInfo package) {
542 free(package.version);
543 free(package.release);
545 free(package.labelstr);
546 free(package.buildHost);
547 free(package.vendor);
548 free(package.description);
549 free(package.copyright);
550 free(package.distribution);
551 free(package.preamble);
553 for (i = 0; i < package.fileCount; i++) {
554 oldrpmfileFree(&package.files[i]);
560 int oldrpmdbLabelCmp(struct oldrpmdbLabel * one, struct oldrpmdbLabel * two) {
563 if ((i = strcmp(one->name, two->name)))
565 else if ((i = strcmp(one->version, two->version)))
568 return strcmp(one->release, two->release);
571 void oldrpmdbSetPrefix(char * new) {