Replace all rpmError() uses with rpmlog()
[platform/upstream/rpm.git] / lib / rpmlead.c
1 /** \ingroup lead
2  * \file lib/rpmlead.c
3  */
4
5 #include "system.h"
6
7 #include <netinet/in.h>
8
9 #include <rpmlib.h>
10
11 #include "signature.h"
12 #include "rpmlead.h"
13 #include "debug.h"
14
15 static unsigned char lead_magic[] = {
16     RPMLEAD_MAGIC0, RPMLEAD_MAGIC1, RPMLEAD_MAGIC2, RPMLEAD_MAGIC3
17 };
18
19 /* The lead needs to be 8 byte aligned */
20
21 rpmRC writeLead(FD_t fd, const struct rpmlead *lead)
22 {
23     struct rpmlead l;
24
25     memcpy(&l, lead, sizeof(l));
26     
27     memcpy(&l.magic, lead_magic, sizeof(l.magic));
28     l.type = htons(l.type);
29     l.archnum = htons(l.archnum);
30     l.osnum = htons(l.osnum);
31     l.signature_type = htons(l.signature_type);
32         
33     if (Fwrite(&l, 1, sizeof(l), fd) != sizeof(l))
34         return RPMRC_FAIL;
35
36     return RPMRC_OK;
37 }
38
39 rpmRC readLead(FD_t fd, struct rpmlead *lead)
40 {
41     memset(lead, 0, sizeof(*lead));
42     /* FIX: remove timed read */
43     if (timedRead(fd, (char *)lead, sizeof(*lead)) != sizeof(*lead)) {
44         if (Ferror(fd)) {
45             rpmlog(RPMERR_READ, _("read failed: %s (%d)\n"),
46                         Fstrerror(fd), errno);
47             return RPMRC_FAIL;
48         }
49         return RPMRC_NOTFOUND;
50     }
51
52     if (memcmp(lead->magic, lead_magic, sizeof(lead_magic)))
53         return RPMRC_NOTFOUND;
54     lead->type = ntohs(lead->type);
55     lead->archnum = ntohs(lead->archnum);
56     lead->osnum = ntohs(lead->osnum);
57     lead->signature_type = ntohs(lead->signature_type);
58     if (lead->signature_type != RPMSIGTYPE_HEADERSIG)
59         return RPMRC_NOTFOUND;
60
61     return RPMRC_OK;
62 }