Consolidated includes for librpmbuild API.
[platform/upstream/rpm.git] / lib / rpmlead.c
1 #include "system.h"
2
3 #if HAVE_MACHINE_TYPES_H
4 # include <machine/types.h>
5 #endif
6
7 #include <netinet/in.h>
8
9 #include "rpmlib.h"
10
11 #include "rpmlead.h"
12 #include "tread.h"
13 #include "intl.h"
14
15 /* The lead needs to be 8 byte aligned */
16
17 int writeLead(int fd, struct rpmlead *lead)
18 {
19     struct rpmlead l;
20
21     memcpy(&l, lead, sizeof(*lead));
22     
23     l.magic[0] = RPMLEAD_MAGIC0;
24     l.magic[1] = RPMLEAD_MAGIC1;
25     l.magic[2] = RPMLEAD_MAGIC2;
26     l.magic[3] = RPMLEAD_MAGIC3;
27
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 (write(fd, &l, sizeof(l)) < 0) {
34         return 1;
35     }
36
37     return 0;
38 }
39
40 int readLead(int fd, struct rpmlead *lead)
41 {
42     if (timedRead(fd, lead, sizeof(*lead)) != sizeof(*lead)) {
43         rpmError(RPMERR_READERROR, _("read failed: %s (%d)"), strerror(errno), 
44               errno);
45         return 1;
46     }
47
48     lead->type = ntohs(lead->type);
49     lead->archnum = ntohs(lead->archnum);
50     lead->osnum = ntohs(lead->osnum);
51
52     if (lead->major >= 2)
53         lead->signature_type = ntohs(lead->signature_type);
54
55     return 0;
56 }
57