Blame SOURCES/0145-idbm_rec_write-seperate-old-and-new-style-writes.patch

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