Modify eu-strip option to perform strip in post script of rpm package & add option...
[platform/upstream/rpm.git] / lib / rpmlead.c
index 8e317b9..8e56bfc 100644 (file)
@@ -12,6 +12,7 @@
 #include <rpm/rpmstring.h>
 
 #include "lib/signature.h"
+#include "lib/header_internal.h"       /* Freadall() */
 #include "lib/rpmlead.h"
 
 #include "debug.h"
@@ -38,11 +39,6 @@ struct rpmlead_s {
     char reserved[16];      /*!< Pad to 96 bytes -- 8 byte aligned! */
 };
 
-rpmlead rpmLeadNew(void)
-{
-    return xcalloc(1, sizeof(struct rpmlead_s));
-}
-
 rpmlead rpmLeadFromHeader(Header h)
 {
     rpmlead l = NULL;
@@ -55,7 +51,7 @@ rpmlead rpmLeadFromHeader(Header h)
        rpmGetArchInfo(NULL, &archnum);
        rpmGetOsInfo(NULL, &osnum);
 
-       l = rpmLeadNew();
+       l = xcalloc(1, sizeof(*l));
        l->major = 3;
        l->minor = 0;
        l->archnum = archnum;
@@ -74,7 +70,6 @@ rpmlead rpmLeadFromHeader(Header h)
 
 rpmlead rpmLeadFree(rpmlead lead)
 {
-    assert(lead != NULL);
     free(lead);
     return NULL;
 }
@@ -82,62 +77,76 @@ rpmlead rpmLeadFree(rpmlead lead)
 /* The lead needs to be 8 byte aligned */
 rpmRC rpmLeadWrite(FD_t fd, rpmlead lead)
 {
-    struct rpmlead_s l;
-    assert(lead != NULL);
-
-    memcpy(&l, lead, sizeof(l));
-    
-    l.type = htons(lead->type);
-    l.archnum = htons(lead->archnum);
-    l.osnum = htons(lead->osnum);
-    l.signature_type = htons(lead->signature_type);
-       
-    if (Fwrite(&l, 1, sizeof(l), fd) != sizeof(l))
-       return RPMRC_FAIL;
+    rpmRC rc = RPMRC_FAIL;
 
-    return RPMRC_OK;
+    if (lead != NULL) {
+       struct rpmlead_s l;
+       memcpy(&l, lead, sizeof(l));
+       
+       l.type = htons(lead->type);
+       l.archnum = htons(lead->archnum);
+       l.osnum = htons(lead->osnum);
+       l.signature_type = htons(lead->signature_type);
+           
+       if (Fwrite(&l, 1, sizeof(l), fd) == sizeof(l))
+           rc = RPMRC_OK;
+    }
+    return rc;
 }
 
-rpmRC rpmLeadCheck(rpmlead lead, const char **msg)
+static rpmRC rpmLeadCheck(rpmlead lead, char **msg)
 {
     if (memcmp(lead->magic, lead_magic, sizeof(lead_magic))) {
-       if (msg) *msg = _("not an rpm package");
+       *msg = xstrdup(_("not an rpm package"));
        return RPMRC_NOTFOUND;
     }
     if (lead->signature_type != RPMSIGTYPE_HEADERSIG) {
-       if (msg) *msg = _("illegal signature type");
+       *msg = xstrdup(_("illegal signature type"));
        return RPMRC_FAIL;
     }
     if (lead->major < 3 || lead->major > 4) {
-       if (msg) *msg = _("unsupported RPM package version");
+       *msg = xstrdup(_("unsupported RPM package version"));
        return RPMRC_FAIL;
     }
     return RPMRC_OK;
 }
 
-rpmRC rpmLeadRead(FD_t fd, rpmlead lead)
+rpmRC rpmLeadRead(FD_t fd, rpmlead *lead, int *type, char **emsg)
 {
-    assert(lead != NULL);
-    memset(lead, 0, sizeof(*lead));
-    if (Fread(lead, 1, sizeof(*lead), fd) != sizeof(*lead)) {
+    rpmRC rc = RPMRC_OK;
+    struct rpmlead_s l;
+    char *err = NULL;
+
+    memset(&l, 0, sizeof(l));
+    if (Freadall(fd, &l, sizeof(l)) != sizeof(l)) {
        if (Ferror(fd)) {
-           rpmlog(RPMLOG_ERR, _("read failed: %s (%d)\n"),
-                       Fstrerror(fd), errno);
-           return RPMRC_FAIL;
+           rasprintf(&err, _("read failed: %s (%d)\n"), Fstrerror(fd), errno);
+           rc = RPMRC_FAIL;
        } else {
-           rpmlog(RPMLOG_ERR, _("not an rpm package\n"));
-           return RPMRC_NOTFOUND;
+           err = xstrdup(_("not an rpm package\n"));
+           rc = RPMRC_NOTFOUND;
        }
+    } else {
+       l.type = ntohs(l.type);
+       l.archnum = ntohs(l.archnum);
+       l.osnum = ntohs(l.osnum);
+       l.signature_type = ntohs(l.signature_type);
+       rc = rpmLeadCheck(&l, &err);
     }
-    lead->type = ntohs(lead->type);
-    lead->archnum = ntohs(lead->archnum);
-    lead->osnum = ntohs(lead->osnum);
-    lead->signature_type = ntohs(lead->signature_type);
 
-    return RPMRC_OK;
-}
+    if (rc == RPMRC_OK) {
+       if (lead != NULL) {
+           *lead = xmalloc(sizeof(l));
+           memcpy(*lead, &l, sizeof(l));
+       }
+       if (type != NULL)
+           *type = l.type;
+    } else {
+       if (emsg != NULL)
+           *emsg = err;
+       else
+           free(err);
+    }
 
-int rpmLeadType(rpmlead lead)
-{
-    return lead ? lead->type : -1;
+    return rc;
 }