added support for old packages, lots of bug fixes
[platform/upstream/rpm.git] / lib / rpmlead.c
1 #include <netinet/in.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include "rpmerr.h"
7 #include "rpmlead.h"
8
9 int writeLead(int fd, struct rpmlead *lead)
10 {
11     struct rpmlead l;
12
13     memcpy(&l, lead, sizeof(*lead));
14     
15     l.magic[0] = RPMLEAD_MAGIC0;
16     l.magic[1] = RPMLEAD_MAGIC1;
17     l.magic[2] = RPMLEAD_MAGIC2;
18     l.magic[3] = RPMLEAD_MAGIC3;
19
20     l.type = htons(l.type);
21     l.archnum = htons(l.archnum);
22     l.osnum = htons(l.osnum);
23     l.signature_type = htons(l.signature_type);
24         
25     write(fd, &l, sizeof(l));
26
27     return 0;
28 }
29
30 int readLead(int fd, struct rpmlead *lead)
31 {
32     if (read(fd, lead, sizeof(*lead)) != sizeof(*lead)) {
33         error(RPMERR_READERROR, "read failed: %s (%d)", strerror(errno), 
34               errno);
35         return 1;
36     }
37
38     lead->type = ntohs(lead->type);
39     lead->archnum = ntohs(lead->archnum);
40     lead->osnum = ntohs(lead->osnum);
41
42     if (lead->major >= 2)
43         lead->signature_type = ntohs(lead->signature_type);
44
45     return 0;
46 }
47