@@ -56,6 +56,7 @@ static bool read_signature(const char *filename, u8 **sig_ret,
u64 file_size;
u8 *sig = NULL;
bool ok = false;
+ int status;
if (!open_file(&file, filename, O_RDONLY, 0))
goto out;
@@ -70,7 +71,8 @@ static bool read_signature(const char *filename, u8 **sig_ret,
goto out;
}
sig = xmalloc(file_size);
- if (!full_read(&file, sig, file_size))
+ status = full_read(&file, sig, file_size);
+ if (status < 0)
goto out;
*sig_ret = sig;
*sig_size_ret = file_size;
@@ -13,6 +13,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <unistd.h>
#include "commands.h"
#include "libfsverity.h"
@@ -20,12 +21,13 @@
static bool write_signature(const char *filename, const u8 *sig, u32 sig_size)
{
struct filedes file;
+ int status;
bool ok;
if (!open_file(&file, filename, O_WRONLY|O_CREAT|O_TRUNC, 0644))
return false;
- ok = full_write(&file, sig, sig_size);
- ok &= filedes_close(&file);
+ status = full_write(&file, sig, sig_size);
+ ok = (!status && filedes_close(&file));
return ok;
}
@@ -48,12 +50,7 @@ static const struct option longopts[] = {
static int read_callback(void *opague, void *buf, size_t count)
{
- int retval = -EBADF;
-
- if (full_read(opague, buf, count))
- retval = 0;
-
- return retval;
+ return full_read(opague, buf, count);
}
/* Sign a file for fs-verity by computing its measurement, then signing it. */
@@ -117,38 +117,38 @@ bool get_file_size(struct filedes *file, u64 *size_ret)
return true;
}
-bool full_read(struct filedes *file, void *buf, size_t count)
+int full_read(struct filedes *file, void *buf, size_t count)
{
while (count) {
int n = read(file->fd, buf, min(count, INT_MAX));
if (n < 0) {
error_msg_errno("reading from '%s'", file->name);
- return false;
+ return n;
}
if (n == 0) {
error_msg("unexpected end-of-file on '%s'", file->name);
- return false;
+ return -EBADFD;
}
buf += n;
count -= n;
}
- return true;
+ return 0;
}
-bool full_write(struct filedes *file, const void *buf, size_t count)
+int full_write(struct filedes *file, const void *buf, size_t count)
{
while (count) {
int n = write(file->fd, buf, min(count, INT_MAX));
if (n < 0) {
error_msg_errno("writing to '%s'", file->name);
- return false;
+ return n;
}
buf += n;
count -= n;
}
- return true;
+ return 0;
}
bool filedes_close(struct filedes *file)
@@ -113,8 +113,8 @@ struct filedes {
bool open_file(struct filedes *file, const char *filename, int flags, int mode);
bool get_file_size(struct filedes *file, u64 *size_ret);
-bool full_read(struct filedes *file, void *buf, size_t count);
-bool full_write(struct filedes *file, const void *buf, size_t count);
+int full_read(struct filedes *file, void *buf, size_t count);
+int full_write(struct filedes *file, const void *buf, size_t count);
bool filedes_close(struct filedes *file);
/* ========== String utilities ========== */