fileio: Unify whole-file reads
We do whole-file reads in a few places, so unify to a fileio_read_file() function. To do this, we change the type of struct image->buf to a uint8_t *. Where we do pointer manipulation on the image buffer, we need a temporary void * variable. Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
This commit is contained in:
parent
d19b993024
commit
6e4b3edcfb
7 changed files with 88 additions and 156 deletions
57
fileio.c
57
fileio.c
|
@ -31,11 +31,18 @@
|
|||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include <ccan/talloc/talloc.h>
|
||||
#include <ccan/read_write_all/read_write_all.h>
|
||||
|
||||
#include "fileio.h"
|
||||
|
||||
EVP_PKEY *fileio_read_pkey(const char *filename)
|
||||
|
@ -78,3 +85,53 @@ out:
|
|||
}
|
||||
return cert;
|
||||
}
|
||||
|
||||
int fileio_read_file(void *ctx, const char *filename,
|
||||
uint8_t **out_buf, size_t *out_len)
|
||||
{
|
||||
struct stat statbuf;
|
||||
uint8_t *buf;
|
||||
size_t len;
|
||||
int fd, rc;
|
||||
|
||||
rc = -1;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = fstat(fd, &statbuf);
|
||||
if (rc) {
|
||||
perror("fstat");
|
||||
goto out;
|
||||
}
|
||||
|
||||
len = statbuf.st_size;
|
||||
|
||||
buf = talloc_array(ctx, uint8_t, len);
|
||||
if (!buf) {
|
||||
perror("talloc");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!read_all(fd, buf, len)) {
|
||||
perror("read_all");
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
|
||||
out:
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
if (rc) {
|
||||
fprintf(stderr, "Error reading file %s\n", filename);
|
||||
} else {
|
||||
*out_buf = buf;
|
||||
*out_len = len;
|
||||
}
|
||||
return rc;
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue