image: use read_write_all from ccan

Rather than using our own functions for reading/writing an entire
buffer, use ccan's.

Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
This commit is contained in:
Jeremy Kerr 2012-05-28 22:44:39 +08:00
parent 3bb18f8ed9
commit 9a4440676c
2 changed files with 12 additions and 38 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
ccan_modules=talloc ccan_modules=talloc read-write-all
# Add ccan upstream sources # Add ccan upstream sources
if [ ! -e lib/ccan.git/Makefile ] if [ ! -e lib/ccan.git/Makefile ]

48
image.c
View file

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/read_write_all/read_write_all.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include "image.h" #include "image.h"
@ -64,18 +65,8 @@ struct image *image_load(const char *filename)
goto err; goto err;
} }
for (bytes_read = 0; bytes_read < image->size; bytes_read += rc) { if (!read_all(image->fd, image->buf, image->size)) {
rc = read(image->fd, image->buf + bytes_read, perror("read_all");
image->size - bytes_read);
if (rc < 0) {
perror("read");
break;
}
if (rc == 0)
break;
}
if (bytes_read < image->size) {
fprintf(stderr, "error reading input file\n"); fprintf(stderr, "error reading input file\n");
goto err; goto err;
} }
@ -325,23 +316,6 @@ int image_hash_sha256(struct image *image, uint8_t digest[])
return !rc; return !rc;
} }
static int write_buf(int fd, void *buf, int size)
{
int bytes_written, rc;
for (bytes_written = 0; bytes_written < size;) {
rc = write(fd, buf + bytes_written, size - bytes_written);
if (rc <= 0) {
perror("write");
return -1;
}
bytes_written += rc;
}
return 0;
}
int image_write_signed(struct image *image, const char *filename) int image_write_signed(struct image *image, const char *filename)
{ {
struct cert_table_header cert_table_header; struct cert_table_header cert_table_header;
@ -367,24 +341,24 @@ int image_write_signed(struct image *image, const char *filename)
return -1; return -1;
} }
rc = write_buf(fd, image->buf, image->size); rc = write_all(fd, image->buf, image->size);
if (rc) if (!rc)
goto out; goto out;
rc = write_buf(fd, &cert_table_header, sizeof(cert_table_header)); rc = write_all(fd, &cert_table_header, sizeof(cert_table_header));
if (rc) if (!rc)
goto out; goto out;
rc = write_buf(fd, image->sigbuf, image->sigsize); rc = write_all(fd, image->sigbuf, image->sigsize);
if (rc) if (!rc)
goto out; goto out;
if (padlen) { if (padlen) {
memset(pad, 0, sizeof(pad)); memset(pad, 0, sizeof(pad));
rc = write_buf(fd, pad, padlen); rc = write_all(fd, pad, padlen);
} }
out: out:
close(fd); close(fd);
return rc; return !rc;
} }