Start beating a little bit of sense into the braindamaged rpmlead API
authorPanu Matilainen <Panu Matilainen pmatilai@redhat.com>
Wed, 6 Jul 2011 09:09:21 +0000 (12:09 +0300)
committerPanu Matilainen <Panu Matilainen pmatilai@redhat.com>
Wed, 6 Jul 2011 09:09:21 +0000 (12:09 +0300)
- rpmLeadNew() should not populate the struct, only allocate it
- The only case where we're creating new lead data is
  rpmLeadFromHeader(), move all initialization there, and add a comment
  for RhBug:717898. Also eliminate the stupid assert, we can
  easily handle NULL header here.

lib/rpmlead.c

index 101d3ff..8e317b9 100644 (file)
@@ -40,30 +40,34 @@ struct rpmlead_s {
 
 rpmlead rpmLeadNew(void)
 {
-    int archnum, osnum;
-    rpmlead l = xcalloc(1, sizeof(*l));
-
-    rpmGetArchInfo(NULL, &archnum);
-    rpmGetOsInfo(NULL, &osnum);
-
-    l->major = 3;
-    l->minor = 0;
-    l->archnum = archnum;
-    l->osnum = osnum;
-    l->signature_type = RPMSIGTYPE_HEADERSIG;
-    return l;
+    return xcalloc(1, sizeof(struct rpmlead_s));
 }
 
 rpmlead rpmLeadFromHeader(Header h)
 {
-    char * nevr;
-    assert(h != NULL);
-    rpmlead l = rpmLeadNew();
+    rpmlead l = NULL;
+
+    if (h != NULL) {
+       int archnum, osnum;
+       char * nevr = headerGetAsString(h, RPMTAG_NEVR);
 
-    l->type = (headerIsSource(h) ? 1 : 0);
-    nevr = headerGetAsString(h, RPMTAG_NEVR);
-    rstrlcpy(l->name, nevr, sizeof(l->name));
-    free(nevr);
+       /* FIXME: should grab these from header instead (RhBug:717898) */
+       rpmGetArchInfo(NULL, &archnum);
+       rpmGetOsInfo(NULL, &osnum);
+
+       l = rpmLeadNew();
+       l->major = 3;
+       l->minor = 0;
+       l->archnum = archnum;
+       l->osnum = osnum;
+       l->signature_type = RPMSIGTYPE_HEADERSIG;
+       l->type = (headerIsSource(h) ? 1 : 0);
+
+       memcpy(l->magic, lead_magic, sizeof(l->magic));
+       rstrlcpy(l->name, nevr, sizeof(l->name));
+
+       free(nevr);
+    }
 
     return l;
 }
@@ -83,7 +87,6 @@ rpmRC rpmLeadWrite(FD_t fd, rpmlead lead)
 
     memcpy(&l, lead, sizeof(l));
     
-    memcpy(&l.magic, lead_magic, sizeof(l.magic));
     l.type = htons(lead->type);
     l.archnum = htons(lead->archnum);
     l.osnum = htons(lead->osnum);