Move <errno.h> include out of system.h to the places that need it
[platform/upstream/rpm.git] / lib / rpmlead.c
1 /** \ingroup lead
2  * \file lib/rpmlead.c
3  */
4
5 #include "system.h"
6
7 #include <errno.h>
8 #include <netinet/in.h>
9
10 #include <rpm/rpmlib.h>         /* rpmGetOs/ArchInfo() */
11 #include <rpm/rpmlog.h>
12 #include <rpm/rpmstring.h>
13
14 #include "lib/signature.h"
15 #include "lib/rpmlead.h"
16
17 #include "debug.h"
18
19 int _noDirTokens = 0;
20
21 static unsigned char const lead_magic[] = {
22     RPMLEAD_MAGIC0, RPMLEAD_MAGIC1, RPMLEAD_MAGIC2, RPMLEAD_MAGIC3
23 };
24
25 /** \ingroup lead
26  * The lead data structure.
27  * The lead needs to be 8 byte aligned.
28  * @deprecated The lead (except for signature_type) is legacy.
29  * @todo Don't use any information from lead.
30  */
31 struct rpmlead_s {
32     unsigned char magic[4];
33     unsigned char major;
34     unsigned char minor;
35     short type;
36     short archnum;
37     char name[66];
38     short osnum;
39     short signature_type;       /*!< Signature header type (RPMSIG_HEADERSIG) */
40     char reserved[16];      /*!< Pad to 96 bytes -- 8 byte aligned! */
41 };
42
43 rpmlead rpmLeadNew(void)
44 {
45     int archnum, osnum;
46     rpmlead l = xcalloc(1, sizeof(*l));
47
48     rpmGetArchInfo(NULL, &archnum);
49     rpmGetOsInfo(NULL, &osnum);
50
51     l->major = 3;
52     l->minor = 0;
53     l->archnum = archnum;
54     l->osnum = osnum;
55     l->signature_type = RPMSIGTYPE_HEADERSIG;
56     return l;
57 }
58
59 rpmlead rpmLeadFromHeader(Header h)
60 {
61     char * nevr;
62     assert(h != NULL);
63     rpmlead l = rpmLeadNew();
64
65     l->type = (headerIsSource(h) ? 1 : 0);
66     nevr = headerGetAsString(h, RPMTAG_NEVR);
67     rstrlcpy(l->name, nevr, sizeof(l->name));
68     free(nevr);
69
70     return l;
71 }
72
73 rpmlead rpmLeadFree(rpmlead lead)
74 {
75     assert(lead != NULL);
76     free(lead);
77     return NULL;
78 }
79
80 /* The lead needs to be 8 byte aligned */
81 rpmRC rpmLeadWrite(FD_t fd, rpmlead lead)
82 {
83     struct rpmlead_s l;
84     assert(lead != NULL);
85
86     memcpy(&l, lead, sizeof(l));
87     
88     memcpy(&l.magic, lead_magic, sizeof(l.magic));
89     l.type = htons(lead->type);
90     l.archnum = htons(lead->archnum);
91     l.osnum = htons(lead->osnum);
92     l.signature_type = htons(lead->signature_type);
93         
94     if (Fwrite(&l, 1, sizeof(l), fd) != sizeof(l))
95         return RPMRC_FAIL;
96
97     return RPMRC_OK;
98 }
99
100 rpmRC rpmLeadCheck(rpmlead lead, const char **msg)
101 {
102     if (memcmp(lead->magic, lead_magic, sizeof(lead_magic))) {
103         if (msg) *msg = _("not an rpm package");
104         return RPMRC_NOTFOUND;
105     }
106     if (lead->signature_type != RPMSIGTYPE_HEADERSIG) {
107         if (msg) *msg = _("illegal signature type");
108         return RPMRC_FAIL;
109     }
110     if (lead->major < 3 || lead->major > 4) {
111         if (msg) *msg = _("unsupported RPM package version");
112         return RPMRC_FAIL;
113     }
114     return RPMRC_OK;
115 }
116
117 rpmRC rpmLeadRead(FD_t fd, rpmlead lead)
118 {
119     assert(lead != NULL);
120     memset(lead, 0, sizeof(*lead));
121     /* FIX: remove timed read */
122     if (timedRead(fd, (char *)lead, sizeof(*lead)) != sizeof(*lead)) {
123         if (Ferror(fd)) {
124             rpmlog(RPMLOG_ERR, _("read failed: %s (%d)\n"),
125                         Fstrerror(fd), errno);
126             return RPMRC_FAIL;
127         } else {
128             rpmlog(RPMLOG_ERR, _("not an rpm package\n"));
129             return RPMRC_NOTFOUND;
130         }
131     }
132     lead->type = ntohs(lead->type);
133     lead->archnum = ntohs(lead->archnum);
134     lead->osnum = ntohs(lead->osnum);
135     lead->signature_type = ntohs(lead->signature_type);
136
137     return RPMRC_OK;
138 }
139
140 int rpmLeadType(rpmlead lead)
141 {
142     return lead ? lead->type : -1;
143 }