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