c2d5d21
From cdb98072f7690cd9f5ec4aa5aa503711e552b5da Mon Sep 17 00:00:00 2001
c2d5d21
From: Chris Leech <cleech@redhat.com>
c2d5d21
Date: Tue, 13 Aug 2013 12:39:07 -0700
c2d5d21
Subject: idbw_rec_write, pick tpgt from existing record
c2d5d21
c2d5d21
On a static add (-m node -o new) without a user specified tpgt, looks
c2d5d21
for existing new style records with tpgt before creating an old style
c2d5d21
record without.  If one exists, take the tpgt from it an write an
c2d5d21
updated new style record instead.
c2d5d21
---
c2d5d21
 usr/idbm.c | 36 ++++++++++++++++++++++++++++++++++++
c2d5d21
 1 file changed, 36 insertions(+)
c2d5d21
c2d5d21
diff --git a/usr/idbm.c b/usr/idbm.c
c2d5d21
index cb6ffd1..5adbd95 100644
c2d5d21
--- a/usr/idbm.c
c2d5d21
+++ b/usr/idbm.c
c2d5d21
@@ -27,6 +27,7 @@
c2d5d21
 #include <errno.h>
c2d5d21
 #include <dirent.h>
c2d5d21
 #include <limits.h>
c2d5d21
+#include <glob.h>
c2d5d21
 #include <sys/stat.h>
c2d5d21
 #include <sys/file.h>
c2d5d21
 
c2d5d21
@@ -1884,12 +1885,47 @@ static int idbm_rec_write_old(node_rec_t *rec)
c2d5d21
 	FILE *f;
c2d5d21
 	char *portal;
c2d5d21
 	int rc = 0;
c2d5d21
+	glob_t globbuf;
c2d5d21
+	int i;
c2d5d21
+	int tpgt = PORTAL_GROUP_TAG_UNKNOWN;
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
+	/* check for newer portal dir with tpgt */
c2d5d21
+	snprintf(portal, PATH_MAX, "%s/%s/%s,%d,*", NODE_CONFIG_DIR,
c2d5d21
+		 rec->name, rec->conn[0].address, rec->conn[0].port);
c2d5d21
+	rc = glob(portal, GLOB_ONLYDIR, NULL, &globbuf);
c2d5d21
+	if (!rc) {
c2d5d21
+		if (globbuf.gl_pathc > 1)
c2d5d21
+			log_warning("multiple tpg records for portal "
c2d5d21
+				    "%s/%s:%d found", rec->name,
c2d5d21
+				    rec->conn[0].address, rec->conn[0].port);
c2d5d21
+		/* set pattern for sscanf matching of tpgt */
c2d5d21
+		snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%%u", NODE_CONFIG_DIR,
c2d5d21
+			 rec->name, rec->conn[0].address, rec->conn[0].port);
c2d5d21
+		for (i = 0; i < globbuf.gl_pathc; i++) {
c2d5d21
+			rc = sscanf(globbuf.gl_pathv[i], portal, &tpgt);
c2d5d21
+			if (rc == 1)
c2d5d21
+				break;
c2d5d21
+		}
c2d5d21
+		if (tpgt == PORTAL_GROUP_TAG_UNKNOWN)
c2d5d21
+			log_warning("glob match on existing records, "
c2d5d21
+				    "but no valid tpgt found");
c2d5d21
+	}
c2d5d21
+	globfree(&globbuf);
c2d5d21
+
c2d5d21
+	/* if a tpgt was selected from an old record, write entry in new format */
c2d5d21
+	if (tpgt != PORTAL_GROUP_TAG_UNKNOWN) {
c2d5d21
+		log_warning("using tpgt %u from existing record", tpgt);
c2d5d21
+		rec->tpgt = tpgt;
c2d5d21
+		free(portal);
c2d5d21
+		return idbm_rec_write_new(rec);
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
 
c2d5d21
-- 
c2d5d21
1.8.1.4
c2d5d21