54e364e
From b9d89091daab823eb2dc72c6c568af7897f83137 Mon Sep 17 00:00:00 2001
c2d5d21
From: Chris Leech <cleech@redhat.com>
c2d5d21
Date: Tue, 13 Aug 2013 11:34:31 -0700
54e364e
Subject: [PATCH 02/32] 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
54e364e
index ab3577878e86..21ff61ab2bd8 100644
c2d5d21
--- a/usr/idbm.c
c2d5d21
+++ b/usr/idbm.c
54e364e
@@ -2001,7 +2001,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;
54e364e
@@ -2013,38 +2013,8 @@ static int idbm_rec_write(node_rec_t *rec)
929689a
 		log_error("Could not alloc portal");
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) {
929689a
-			log_error("Could not make %s: %s", 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) {
929689a
-			log_error("Could not make %s: %s", 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) {
54e364e
@@ -2065,11 +2035,11 @@ static int idbm_rec_write(node_rec_t *rec)
929689a
 			log_error("Could not convert %s: %s", 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;
54e364e
 	}
c2d5d21
 
c2d5d21
 mkdir_portal:
54e364e
@@ -2080,24 +2050,96 @@ mkdir_portal:
929689a
 			log_error("Could not make dir %s: %s",
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) {
929689a
 		log_error("Could not open %s: %s", 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) {
929689a
+		log_error("Could not alloc portal");
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) {
929689a
+		log_error("Could not open %s: %sd", 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) {
929689a
+		log_error("Could not alloc portal");
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) {
929689a
+			log_error("Could not make %s: %s", 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) {
929689a
+			log_error("Could not make %s: %s", 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
-- 
54e364e
2.14.4
c2d5d21