fileio: Add fileio_read_file_noerror()
We may want to read files which can be absent. In this case, we don't want to print an error. This change adds fileio_read_file_noerror(), which suppresses error output. Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
This commit is contained in:
parent
07328d85c3
commit
f9eed9cc42
2 changed files with 27 additions and 16 deletions
41
src/fileio.c
41
src/fileio.c
|
@ -33,7 +33,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <openssl/bio.h>
|
#include <openssl/bio.h>
|
||||||
|
@ -45,6 +45,8 @@
|
||||||
|
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
|
|
||||||
|
#define FLAG_NOERROR (1<<0)
|
||||||
|
|
||||||
EVP_PKEY *fileio_read_pkey(const char *filename)
|
EVP_PKEY *fileio_read_pkey(const char *filename)
|
||||||
{
|
{
|
||||||
EVP_PKEY *key = NULL;
|
EVP_PKEY *key = NULL;
|
||||||
|
@ -86,8 +88,8 @@ out:
|
||||||
return cert;
|
return cert;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fileio_read_file(void *ctx, const char *filename,
|
static int __fileio_read_file(void *ctx, const char *filename,
|
||||||
uint8_t **out_buf, size_t *out_len)
|
uint8_t **out_buf, size_t *out_len, int flags)
|
||||||
{
|
{
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
|
@ -97,29 +99,21 @@ int fileio_read_file(void *ctx, const char *filename,
|
||||||
rc = -1;
|
rc = -1;
|
||||||
|
|
||||||
fd = open(filename, O_RDONLY);
|
fd = open(filename, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0)
|
||||||
perror("open");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
|
||||||
|
|
||||||
rc = fstat(fd, &statbuf);
|
rc = fstat(fd, &statbuf);
|
||||||
if (rc) {
|
if (rc)
|
||||||
perror("fstat");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
|
||||||
|
|
||||||
len = statbuf.st_size;
|
len = statbuf.st_size;
|
||||||
|
|
||||||
buf = talloc_array(ctx, uint8_t, len);
|
buf = talloc_array(ctx, uint8_t, len);
|
||||||
if (!buf) {
|
if (!buf)
|
||||||
perror("talloc");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
|
||||||
|
|
||||||
if (!read_all(fd, buf, len)) {
|
if (!read_all(fd, buf, len))
|
||||||
perror("read_all");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
|
||||||
|
|
||||||
rc = 0;
|
rc = 0;
|
||||||
|
|
||||||
|
@ -127,7 +121,9 @@ out:
|
||||||
if (fd >= 0)
|
if (fd >= 0)
|
||||||
close(fd);
|
close(fd);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
fprintf(stderr, "Error reading file %s\n", filename);
|
if (!(flags & FLAG_NOERROR))
|
||||||
|
fprintf(stderr, "Error reading file %s: %s\n",
|
||||||
|
filename, strerror(errno));
|
||||||
} else {
|
} else {
|
||||||
*out_buf = buf;
|
*out_buf = buf;
|
||||||
*out_len = len;
|
*out_len = len;
|
||||||
|
@ -136,6 +132,19 @@ out:
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fileio_read_file(void *ctx, const char *filename,
|
||||||
|
uint8_t **out_buf, size_t *out_len)
|
||||||
|
{
|
||||||
|
return __fileio_read_file(ctx, filename, out_buf, out_len, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fileio_read_file_noerror(void *ctx, const char *filename,
|
||||||
|
uint8_t **out_buf, size_t *out_len)
|
||||||
|
{
|
||||||
|
return __fileio_read_file(ctx, filename, out_buf, out_len,
|
||||||
|
FLAG_NOERROR);
|
||||||
|
}
|
||||||
|
|
||||||
int fileio_write_file(const char *filename, uint8_t *buf, size_t len)
|
int fileio_write_file(const char *filename, uint8_t *buf, size_t len)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
|
@ -42,6 +42,8 @@ X509 *fileio_read_cert(const char *filename);
|
||||||
|
|
||||||
int fileio_read_file(void *ctx, const char *filename,
|
int fileio_read_file(void *ctx, const char *filename,
|
||||||
uint8_t **out_buf, size_t *out_len);
|
uint8_t **out_buf, size_t *out_len);
|
||||||
|
int fileio_read_file_noerror(void *ctx, const char *filename,
|
||||||
|
uint8_t **out_buf, size_t *out_len);
|
||||||
int fileio_write_file(const char *filename, uint8_t *buf, size_t len);
|
int fileio_write_file(const char *filename, uint8_t *buf, size_t len);
|
||||||
|
|
||||||
#endif /* FILEIO_H */
|
#endif /* FILEIO_H */
|
||||||
|
|
Loading…
Reference in a new issue