Clean up ioctl-fs-verity-measure and rename to fsveritymeasure
Signed-off-by: Eric Biggers <ebiggers@google.com>
diff --git a/fsveritymeasure.c b/fsveritymeasure.c
new file mode 100644
index 0000000..0be1c25
--- /dev/null
+++ b/fsveritymeasure.c
@@ -0,0 +1,44 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include "fsverity_api.h"
+
+static void usage(void)
+{
+ fprintf(stderr,
+"Usage: fsveritymeasure FILE EXPECTED_MEASUREMENT\n"
+"\n"
+"EXPECTED_MEASUREMENT must be a 64-character hex string.\n");
+ exit(2);
+}
+
+int main(int args, char *argv[])
+{
+ int fd, i;
+ unsigned int byte;
+ struct fsverity_root_hash measurement = { 0 };
+
+ if (args != 3 || strlen(argv[2]) != 64)
+ usage();
+
+ for (i = 0; i < 32; i++) {
+ if (sscanf(&argv[2][i*2], "%02x", &byte) != 1)
+ usage();
+ measurement.root_hash[i] = byte;
+ }
+ fd = open(argv[1], O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "Can't open %s: %m\n", argv[1]);
+ return 1;
+ }
+ if (ioctl(fd, FS_IOC_MEASURE_FSVERITY, &measurement)) {
+ fprintf(stderr, "FS_IOC_MEASURE_FSVERITY: %m\n");
+ return 1;
+ }
+ close(fd);
+ return 0;
+}
diff --git a/ioctl-fs-verity-measure.c b/ioctl-fs-verity-measure.c
deleted file mode 100644
index e4bf09e..0000000
--- a/ioctl-fs-verity-measure.c
+++ /dev/null
@@ -1,44 +0,0 @@
-#include <linux/fs.h>
-#include <stdio.h>
-#include <errno.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include <string.h>
-
-int main(int args, char *argv[])
-{
- int res, fd, i;
- struct fsverity_root_hash root_hash;
-
- if (args != 3 || strlen(argv[2]) != 64) {
- printf("Usage:\n ioctl-fs-verity [filepath] [root hash in hex; 64 characters]\n");
- return -EINVAL;
- }
-
- fd = open(argv[1], O_RDONLY);
-
- if (fd == -1) {
- printf("Could not open [%s]\n", argv[1]);
- return -EINVAL;
- }
- memset((void*)&root_hash, 0, sizeof(struct fsverity_root_hash));
- for (i = 0; i < 32; i++) {
- char hdigit[3] = {0, 0, 0};
-
- memcpy(hdigit, &argv[2][i*2], 2);
- res = sscanf(hdigit, "%x", &root_hash.root_hash[i]);
- if (res != 1)
- return -EINVAL;
- }
- res = ioctl(fd, FS_IOC_MEASURE_FSVERITY, &root_hash);
- if (res) {
- printf("ioctl() returned [%d]\n", res);
- return 1;
- }
- close(fd);
- return 0;
-}