diff mbox series

[11/20] Make full_{read,write}() return proper error codes instead of bool

Message ID 20200424205504.2586682-12-Jes.Sorensen@gmail.com (mailing list archive)
State Superseded
Headers show
Series Split fsverity-utils into a shared library | expand

Commit Message

Jes Sorensen April 24, 2020, 8:54 p.m. UTC
From: Jes Sorensen <jsorensen@fb.com>

This allows for better error reporting, and simplifies the read callback
handler.

Signed-off-by: Jes Sorensen <jsorensen@fb.com>
---
 cmd_enable.c |  4 +++-
 cmd_sign.c   | 13 +++++--------
 util.c       | 14 +++++++-------
 util.h       |  4 ++--
 4 files changed, 17 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/cmd_enable.c b/cmd_enable.c
index 1bed3ef..9612778 100644
--- a/cmd_enable.c
+++ b/cmd_enable.c
@@ -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;
diff --git a/cmd_sign.c b/cmd_sign.c
index 15d0937..959e6d9 100644
--- a/cmd_sign.c
+++ b/cmd_sign.c
@@ -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. */
diff --git a/util.c b/util.c
index 2218f2e..586d2b0 100644
--- a/util.c
+++ b/util.c
@@ -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)
diff --git a/util.h b/util.h
index dd9b803..c4dc066 100644
--- a/util.h
+++ b/util.h
@@ -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 ========== */