c2d5d21
From 954a9492b5ed1de5907ad2a7d7cc0ae6215d8fac Mon Sep 17 00:00:00 2001
c2d5d21
From: Chris Leech <cleech@redhat.com>
c2d5d21
Date: Tue, 13 Aug 2013 11:34:31 -0700
c2d5d21
Subject: idbm_rec_write, seperate old and new style writes
c2d5d21
c2d5d21
Duplicates a small bit of code, but easier to understand and extened.
c2d5d21
---
c2d5d21
 usr/idbm.c | 116 +++++++++++++++++++++++++++++++++++++++++--------------------
c2d5d21
 1 file changed, 79 insertions(+), 37 deletions(-)
c2d5d21
c2d5d21
diff --git a/usr/idbm.c b/usr/idbm.c
c2d5d21
index 0a88699..cb6ffd1 100644
c2d5d21
--- a/usr/idbm.c
c2d5d21
+++ b/usr/idbm.c
c2d5d21
@@ -1808,7 +1808,7 @@ mkdir_portal:
c2d5d21
 	return f;
c2d5d21
 }
c2d5d21
 
c2d5d21
-static int idbm_rec_write(node_rec_t *rec)
c2d5d21
+static int idbm_rec_write_new(node_rec_t *rec)
c2d5d21
 {
c2d5d21
 	struct stat statb;
c2d5d21
 	FILE *f;
c2d5d21
@@ -1820,38 +1820,8 @@ static int idbm_rec_write(node_rec_t *rec)
c2d5d21
 		log_error("Could not alloc portal\n");
c2d5d21
 		return ISCSI_ERR_NOMEM;
c2d5d21
 	}
c2d5d21
-
c2d5d21
-	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
c2d5d21
-	if (access(portal, F_OK) != 0) {
c2d5d21
-		if (mkdir(portal, 0660) != 0) {
c2d5d21
-			log_error("Could not make %s: %s\n", portal,
c2d5d21
-				  strerror(errno));
c2d5d21
-			rc = ISCSI_ERR_IDBM;
c2d5d21
-			goto free_portal;
c2d5d21
-		}
c2d5d21
-	}
c2d5d21
-
c2d5d21
-	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
c2d5d21
-	if (access(portal, F_OK) != 0) {
c2d5d21
-		if (mkdir(portal, 0660) != 0) {
c2d5d21
-			log_error("Could not make %s: %s\n", portal,
c2d5d21
-				  strerror(errno));
c2d5d21
-			rc = ISCSI_ERR_IDBM;
c2d5d21
-			goto free_portal;
c2d5d21
-		}
c2d5d21
-	}
c2d5d21
-
c2d5d21
 	snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
c2d5d21
 		 rec->name, rec->conn[0].address, rec->conn[0].port);
c2d5d21
-	log_debug(5, "Looking for config file %s", portal);
c2d5d21
-
c2d5d21
-	rc = idbm_lock();
c2d5d21
-	if (rc)
c2d5d21
-		goto free_portal;
c2d5d21
-
c2d5d21
-	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
c2d5d21
-		/* drop down to old style portal as config */
c2d5d21
-		goto open_conf;
c2d5d21
 
c2d5d21
 	rc = stat(portal, &statb);
c2d5d21
 	if (rc) {
c2d5d21
@@ -1872,11 +1842,11 @@ static int idbm_rec_write(node_rec_t *rec)
c2d5d21
 			log_error("Could not convert %s: %s\n", portal,
c2d5d21
 				  strerror(errno));
c2d5d21
 			rc = ISCSI_ERR_IDBM;
c2d5d21
-			goto unlock;
c2d5d21
+			goto free_portal;
c2d5d21
 		}
c2d5d21
 	} else {
c2d5d21
 		rc = ISCSI_ERR_INVAL;
c2d5d21
-		goto unlock;
c2d5d21
+		goto free_portal;
c2d5d21
 	}	
c2d5d21
 
c2d5d21
 mkdir_portal:
c2d5d21
@@ -1887,24 +1857,96 @@ mkdir_portal:
c2d5d21
 			log_error("Could not make dir %s: %s\n",
c2d5d21
 				  portal, strerror(errno));
c2d5d21
 			rc = ISCSI_ERR_IDBM;
c2d5d21
-			goto unlock;
c2d5d21
+			goto free_portal;
c2d5d21
 		}
c2d5d21
 	}
c2d5d21
 
c2d5d21
 	snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
c2d5d21
 		 rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
c2d5d21
 		 rec->iface.name);
c2d5d21
-open_conf:
c2d5d21
+/* open_conf: */
c2d5d21
 	f = fopen(portal, "w");
c2d5d21
 	if (!f) {
c2d5d21
 		log_error("Could not open %s: %sd\n", portal, strerror(errno));
c2d5d21
 		rc = ISCSI_ERR_IDBM;
c2d5d21
-		goto unlock;
c2d5d21
+		goto free_portal;
c2d5d21
 	}
c2d5d21
 
c2d5d21
 	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
c2d5d21
 	fclose(f);
c2d5d21
-unlock:
c2d5d21
+free_portal:
c2d5d21
+	free(portal);
c2d5d21
+	return rc;
c2d5d21
+}
c2d5d21
+
c2d5d21
+static int idbm_rec_write_old(node_rec_t *rec)
c2d5d21
+{
c2d5d21
+	FILE *f;
c2d5d21
+	char *portal;
c2d5d21
+	int rc = 0;
c2d5d21
+
c2d5d21
+	portal = malloc(PATH_MAX);
c2d5d21
+	if (!portal) {
c2d5d21
+		log_error("Could not alloc portal\n");
c2d5d21
+		return ISCSI_ERR_NOMEM;
c2d5d21
+	}
c2d5d21
+	snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
c2d5d21
+		 rec->name, rec->conn[0].address, rec->conn[0].port);
c2d5d21
+
c2d5d21
+	f = fopen(portal, "w");
c2d5d21
+	if (!f) {
c2d5d21
+		log_error("Could not open %s: %sd\n", portal, strerror(errno));
c2d5d21
+		rc = ISCSI_ERR_IDBM;
c2d5d21
+		goto free_portal;
c2d5d21
+	}
c2d5d21
+	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
c2d5d21
+	fclose(f);
c2d5d21
+free_portal:
c2d5d21
+	free(portal);
c2d5d21
+	return rc;
c2d5d21
+}
c2d5d21
+
c2d5d21
+static int idbm_rec_write(node_rec_t *rec)
c2d5d21
+{
c2d5d21
+	char *portal;
c2d5d21
+	int rc = 0;
c2d5d21
+
c2d5d21
+	portal = malloc(PATH_MAX);
c2d5d21
+	if (!portal) {
c2d5d21
+		log_error("Could not alloc portal\n");
c2d5d21
+		return ISCSI_ERR_NOMEM;
c2d5d21
+	}
c2d5d21
+
c2d5d21
+	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
c2d5d21
+	if (access(portal, F_OK) != 0) {
c2d5d21
+		if (mkdir(portal, 0660) != 0) {
c2d5d21
+			log_error("Could not make %s: %s\n", portal,
c2d5d21
+				  strerror(errno));
c2d5d21
+			rc = ISCSI_ERR_IDBM;
c2d5d21
+			goto free_portal;
c2d5d21
+		}
c2d5d21
+	}
c2d5d21
+
c2d5d21
+	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
c2d5d21
+	if (access(portal, F_OK) != 0) {
c2d5d21
+		if (mkdir(portal, 0660) != 0) {
c2d5d21
+			log_error("Could not make %s: %s\n", portal,
c2d5d21
+				  strerror(errno));
c2d5d21
+			rc = ISCSI_ERR_IDBM;
c2d5d21
+			goto free_portal;
c2d5d21
+		}
c2d5d21
+	}
c2d5d21
+
c2d5d21
+	rc = idbm_lock();
c2d5d21
+	if (rc)
c2d5d21
+		goto free_portal;
c2d5d21
+
c2d5d21
+	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
c2d5d21
+		/* old style portal as config */
c2d5d21
+		rc = idbm_rec_write_old(rec);
c2d5d21
+	else
c2d5d21
+		rc = idbm_rec_write_new(rec);
c2d5d21
+
c2d5d21
 	idbm_unlock();
c2d5d21
 free_portal:
c2d5d21
 	free(portal);
c2d5d21
-- 
c2d5d21
1.8.1.4
c2d5d21