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