diff mbox

[05/13] nsdbc: nsdb-list should list by NCE

Message ID 20130201172027.63192.41881.stgit@seurat.1015granger.net
State Accepted
Headers show

Commit Message

Chuck Lever Feb. 1, 2013, 5:20 p.m. UTC
The output of nsdb-list assumes an NSDB has just one NCE.   It may
have more than one, so list the FSN's by NCE.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---

 doc/man/nsdb-list.8   |   14 +++--
 src/nsdbc/nsdb-list.c |  137 ++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 108 insertions(+), 43 deletions(-)
diff mbox

Patch

diff --git a/doc/man/nsdb-list.8 b/doc/man/nsdb-list.8
index bd125e0..11f993f 100644
--- a/doc/man/nsdb-list.8
+++ b/doc/man/nsdb-list.8
@@ -202,17 +202,19 @@  is an NSDB.  Use:
 .sp
 $ nsdb-list -l ldap.example.net -e o=fedfs
 .br
-NSDB: ldap.example.net:389, o=fedfs
-.br
-  FSN UUID: dc25a644-06e4-11e0-ae55-000c29dc7f8a
+NSDB: ldap.example.net:389
+.sp
+  NCE: o=fedfs
+.sp
+    FSN UUID: c1c21720-1fcd-4ad6-a837-f57af4cf2972
 .br
-    FSL UUID: f4e88676-06f6-11e0-96ca-000c29dc7f8a
+      FSL UUID: 4c887035-ad2f-4ba8-ab75-7118df9714cd
 .br
-    FSL UUID: 4d2da352-06f2-11e0-a4c7-000c29dc7f8a
+      FSL UUID: 84445758-b5fb-4acc-814b-cc121b3bafe9
 .sp
 .RE
 There is a single file set name, with two file set location records,
-registered on this NSDB.
+registered under "o=fedfs" on this NSDB.
 To resolve the listed FSN UUID, use the
 .BR nsdb-resolve-junction (8)
 command.
diff --git a/src/nsdbc/nsdb-list.c b/src/nsdbc/nsdb-list.c
index 72b05a2..02c9c09 100644
--- a/src/nsdbc/nsdb-list.c
+++ b/src/nsdbc/nsdb-list.c
@@ -99,7 +99,7 @@  static void
 nsdb_list_display_fsls(struct fedfs_fsl *fsls)
 {
 	for ( ; fsls != NULL; fsls = fsls->fl_next) {
-		printf("    FSL UUID: %s\n", fsls->fl_fsluuid);
+		printf("      FSL UUID: %s\n", fsls->fl_fsluuid);
 	}
 }
 
@@ -117,7 +117,7 @@  nsdb_list_resolve_and_display_fsn(nsdb_t host, const char *nce, const char *fsn_
 	unsigned int ldap_err;
 	FedFsStatus retval;
 
-	printf("  FSN UUID: %s\n", fsn_uuid);
+	printf("    FSN UUID: %s\n", fsn_uuid);
 
 	retval = nsdb_resolve_fsn_s(host, nce, fsn_uuid, &fsls, &ldap_err);
 	switch (retval) {
@@ -126,15 +126,14 @@  nsdb_list_resolve_and_display_fsn(nsdb_t host, const char *nce, const char *fsn_
 		nsdb_free_fedfs_fsls(fsls);
 		break;
 	case FEDFS_ERR_NSDB_NOFSL:
-		printf("    No FSL entries found\n");
+		printf("      No FSL entries found\n");
 		break;
 	case FEDFS_ERR_NSDB_LDAP_VAL:
-		fprintf(stderr, "    NSDB LDAP error: %s\n",
+		fprintf(stderr, "NSDB LDAP error: %s\n",
 			ldap_err2string(ldap_err));
 		break;
 	default:
-		fprintf(stderr, "    FedFsStatus code "
-			"while resolving FSN UUID %s: %s\n",
+		fprintf(stderr, "Failed to resolve FSN UUID %s: %s\n",
 			fsn_uuid, nsdb_display_fedfsstatus(retval));
 	}
 
@@ -142,6 +141,95 @@  nsdb_list_resolve_and_display_fsn(nsdb_t host, const char *nce, const char *fsn_
 }
 
 /**
+ * Display FSNs under a specific NCE
+ *
+ * @param host an initialized and bound nsdb_t object
+ * @param nce a NUL-terminated C string containing DN of NSDB container entry
+ * @param ldap_err OUT: possibly an LDAP error code
+ * @return a FedFsStatus code
+ */
+static FedFsStatus
+nsdb_list_display_one_nce(nsdb_t host, const char *nce, unsigned int *ldap_err)
+{
+	FedFsStatus retval;
+	char **fsns;
+	int i;
+
+	retval = nsdb_list_s(host, nce, &fsns, ldap_err);
+	switch (retval) {
+	case FEDFS_OK:
+		printf("  NCE: %s\n\n", nce);
+		for (i = 0; fsns[i] != NULL; i++)
+			nsdb_list_resolve_and_display_fsn(host, nce, fsns[i]);
+		nsdb_free_string_array(fsns);
+		break;
+	case FEDFS_ERR_NSDB_NOFSN:
+		printf("  NCE %s has no FSN records\n", nce);
+		break;
+	case FEDFS_ERR_NSDB_NONCE:
+		printf("  NCE %s does not exist\n", nce);
+		break;
+	case FEDFS_ERR_NSDB_LDAP_VAL:
+		break;
+	default:
+		fprintf(stderr, "Failed to retrieve FSNs: %s\n",
+			nsdb_display_fedfsstatus(retval));
+	}
+	return retval;
+}
+
+/**
+ * Display FSNs under a specific NCE, with header
+ *
+ * @param host an initialized and bound nsdb_t object
+ * @param nce a NUL-terminated C string containing DN of NSDB container entry
+ * @param ldap_err OUT: possibly an LDAP error code
+ * @return a FedFsStatus code
+ */
+static FedFsStatus
+nsdb_list_display_nce(nsdb_t host, const char *nce, unsigned int *ldap_err)
+{
+	printf("NSDB: %s:%u\n\n", nsdb_hostname(host), nsdb_port(host));
+	return nsdb_list_display_one_nce(host, nce, ldap_err);
+
+}
+
+/**
+ * Display FSNs under all NCEs, with header
+ *
+ * @param host an initialized and bound nsdb_t object
+ * @param ldap_err OUT: possibly an LDAP error code
+ * @return a FedFsStatus code
+ */
+static FedFsStatus
+nsdb_list_display_all_nces(nsdb_t host, unsigned int *ldap_err)
+{
+	FedFsStatus retval;
+	char *dn, **contexts;
+	int i;
+
+	retval = nsdb_get_naming_contexts_s(host, &contexts, ldap_err);
+	if (retval != FEDFS_OK)
+		return retval;
+
+	printf("NSDB: %s:%u\n\n", nsdb_hostname(host), nsdb_port(host));
+
+	retval = FEDFS_ERR_NSDB_NONCE;
+	for (i = 0; contexts[i] != NULL; i++) {
+		retval = nsdb_get_ncedn_s(host, contexts[i], &dn, ldap_err);
+		if (retval == FEDFS_OK) {
+			retval = nsdb_list_display_one_nce(host, dn, ldap_err);
+			free(dn);
+			if (retval != FEDFS_OK)
+				break;
+		}
+	}
+
+	nsdb_free_string_array(contexts);
+	return retval;
+}
+
+/**
  * Attempt to follow an LDAP referral to another NSDB
  *
  * @param host OUT: pointer to an initialized nsdb_t that may be replaced
@@ -202,9 +290,8 @@  main(int argc, char **argv)
 	unsigned int ldap_err;
 	FedFsStatus retval;
 	nsdb_t host;
-	char **fsns;
 	char *nce;
-	int arg, i;
+	int arg;
 
 	(void)umask(S_IRWXO);
 
@@ -303,31 +390,11 @@  again:
 		goto out_free;
 	}
 
-	retval = nsdb_list_s(host, nce, &fsns, &ldap_err);
-	switch (retval) {
-	case FEDFS_OK:
-		if (nce == NULL)
-			printf("NSDB: %s:%u\n",
-				nsdbname, nsdbport);
-		else
-			printf("NSDB: %s:%u, %s\n",
-				nsdbname, nsdbport, nce);
-		for (i = 0; fsns[i] != NULL; i++)
-			nsdb_list_resolve_and_display_fsn(host, nce, fsns[i]);
-		nsdb_free_string_array(fsns);
-		break;
-	case FEDFS_ERR_NSDB_NOFSN:
-		fprintf(stderr, "NSDB %s:%u has no FSN records\n",
-			nsdbname, nsdbport);
-		break;
-	case FEDFS_ERR_NSDB_NONCE:
-		if (nce == NULL)
-			fprintf(stderr, "NSDB %s:%u has no NCE\n",
-				nsdbname, nsdbport);
-		else
-			fprintf(stderr, "NCE %s does not exist\n", nce);
-		break;
-	case FEDFS_ERR_NSDB_LDAP_VAL:
+	if (nce != NULL)
+		retval = nsdb_list_display_nce(host, nce, &ldap_err);
+	else
+		retval = nsdb_list_display_all_nces(host, &ldap_err);
+	if (retval == FEDFS_ERR_NSDB_LDAP_VAL) {
 		switch (ldap_err) {
 		case LDAP_REFERRAL:
 			retval = nsdb_list_follow_ldap_referral(&host);
@@ -342,10 +409,6 @@  again:
 			fprintf(stderr, "Failed to list FSNs: %s\n",
 				ldap_err2string(ldap_err));
 		}
-		break;
-	default:
-		fprintf(stderr, "Failed to list FSNs: %s\n",
-			nsdb_display_fedfsstatus(retval));
 	}
 
 	nsdb_close_nsdb(host);