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