[ndctl,32/36] ndctl/namespace: Parse infoblocks from stdin
diff mbox series

Message ID 158300777490.2141307.3531523891640736244.stgit@dwillia2-desk3.amr.corp.intel.com
State New
Headers show
Series
  • Multiple topics / backlog for v68
Related show

Commit Message

Dan Williams Feb. 29, 2020, 8:22 p.m. UTC
In preparation for validating a new write-infoblock command, teach
read-infoblock to parse stdin by default.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 ndctl/namespace.c |   25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

Patch
diff mbox series

diff --git a/ndctl/namespace.c b/ndctl/namespace.c
index ef82254a7b18..7bfc7d1ab61d 100644
--- a/ndctl/namespace.c
+++ b/ndctl/namespace.c
@@ -334,8 +334,7 @@  static const char *parse_namespace_options(int argc, const char **argv,
 				break;
 		}
 
-		if ((action == ACTION_READ_INFOBLOCK && !param.infile)
-				|| action != ACTION_READ_INFOBLOCK) {
+		if (action != ACTION_READ_INFOBLOCK) {
 			error("specify a namespace to %s, or \"all\"\n", action_string);
 			rc = -EINVAL;
 		}
@@ -355,6 +354,8 @@  static const char *parse_namespace_options(int argc, const char **argv,
 		return NULL; /* we won't return from usage_with_options() */
 	}
 
+	if (action == ACTION_READ_INFOBLOCK && !param.infile && !argc)
+		return NULL;
 	return action == ACTION_CREATE ? param.reconfig : argv[0];
 }
 
@@ -1688,7 +1689,12 @@  static int file_read_infoblock(const char *path, struct ndctl_namespace *ndns,
 	if (!buf)
 		return -ENOMEM;
 
-	fd = open(path, O_RDONLY|O_EXCL);
+	if (!path) {
+		fd = STDIN_FILENO;
+		path = "<stdin>";
+	} else
+		fd = open(path, O_RDONLY|O_EXCL);
+
 	if (fd < 0) {
 		pr_verbose("%s: %s failed to open %s: %s\n",
 				cmd, devname, path, strerror(errno));
@@ -1697,17 +1703,20 @@  static int file_read_infoblock(const char *path, struct ndctl_namespace *ndns,
 	}
 
 	rc = read(fd, buf, INFOBLOCK_SZ);
-	if (rc < 0) {
+	if (rc < INFOBLOCK_SZ) {
 		pr_verbose("%s: %s failed to read %s: %s\n",
 				cmd, devname, path, strerror(errno));
-		rc = -errno;
+		if (rc < 0)
+			rc = -errno;
+		else
+			rc = -EIO;
 		goto out;
 	}
 
 	rc = parse_namespace_infoblock(buf, ndns, path, ri_ctx);
 out:
 	free(buf);
-	if (fd >= 0)
+	if (fd >= 0 && fd != STDIN_FILENO)
 		close(fd);
 	return rc;
 }
@@ -1766,10 +1775,12 @@  static int do_xaction_namespace(const char *namespace,
 			}
 		}
 
-		if (param.infile) {
+		if (param.infile || !namespace) {
 			rc = file_read_infoblock(param.infile, NULL, &ri_ctx);
 			if (ri_ctx.jblocks)
 				util_display_json_array(ri_ctx.f_out, ri_ctx.jblocks, 0);
+			if (rc >= 0)
+				(*processed)++;
 			return rc;
 		}
 	}