implement abstract fd type almost everywhere.
[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
13 /* The lead needs to be 8 byte aligned */
14
15 int writeLead(FD_t fd, struct rpmlead *lead)
16 {
17     struct rpmlead l;
18
19     memcpy(&l, lead, sizeof(*lead));
20     
21     l.magic[0] = RPMLEAD_MAGIC0;
22     l.magic[1] = RPMLEAD_MAGIC1;
23     l.magic[2] = RPMLEAD_MAGIC2;
24     l.magic[3] = RPMLEAD_MAGIC3;
25
26     l.type = htons(l.type);
27     l.archnum = htons(l.archnum);
28     l.osnum = htons(l.osnum);
29     l.signature_type = htons(l.signature_type);
30         
31     if (fdWrite(fd, &l, sizeof(l)) < 0) {
32         return 1;
33     }
34
35     return 0;
36 }
37
38 int readLead(FD_t fd, struct rpmlead *lead)
39 {
40     if (timedRead(fd, lead, sizeof(*lead)) != sizeof(*lead)) {
41         rpmError(RPMERR_READERROR, _("read failed: %s (%d)"), strerror(errno), 
42               errno);
43         return 1;
44     }
45
46     lead->type = ntohs(lead->type);
47     lead->archnum = ntohs(lead->archnum);
48     lead->osnum = ntohs(lead->osnum);
49
50     if (lead->major >= 2)
51         lead->signature_type = ntohs(lead->signature_type);
52
53     return 0;
54 }
55