+/*\r
+ * Copyright (c) 2008 Voltaire, Inc. All rights reserved.\r
+ * Copyright (c) 2007 Lawrence Livermore National Lab\r
+ * Copyright (c) 2009 Intel Corp., Inc.\r
+ *\r
+ * This software is available to you under a choice of one of two\r
+ * licenses. You may choose to be licensed under the terms of the GNU\r
+ * General Public License (GPL) Version 2, available from the file\r
+ * COPYING in the main directory of this source tree, or the\r
+ * OpenIB.org BSD license below:\r
+ *\r
+ * Redistribution and use in source and binary forms, with or\r
+ * without modification, are permitted provided that the following\r
+ * conditions are met:\r
+ *\r
+ * - Redistributions of source code must retain the above\r
+ * copyright notice, this list of conditions and the following\r
+ * disclaimer.\r
+ *\r
+ * - Redistributions in binary form must reproduce the above\r
+ * copyright notice, this list of conditions and the following\r
+ * disclaimer in the documentation and/or other materials\r
+ * provided with the distribution.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+ * SOFTWARE.\r
+ *\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <errno.h>\r
+#include <_string.h>\r
+#include <stdlib.h>\r
+\r
+#include <complib/cl_nodenamemap.h>\r
+\r
+static int map_name(void *cxt, uint64_t guid, char *p)\r
+{\r
+ cl_qmap_t *map = cxt;\r
+ name_map_item_t *item;\r
+\r
+ p = strtok(p, "\"#");\r
+ if (!p)\r
+ return 0;\r
+\r
+ item = malloc(sizeof(*item));\r
+ if (!item)\r
+ return -1;\r
+ item->guid = guid;\r
+ item->name = strdup(p);\r
+ cl_qmap_insert(map, item->guid, (cl_map_item_t *)item);\r
+ return 0;\r
+}\r
+\r
+nn_map_t *\r
+open_node_name_map(char *node_name_map)\r
+{\r
+ nn_map_t *map;\r
+\r
+ if (!node_name_map) {\r
+#ifdef HAVE_DEFAULT_NODENAME_MAP\r
+ struct stat buf;\r
+ node_name_map = HAVE_DEFAULT_NODENAME_MAP;\r
+ if (stat(node_name_map, &buf))\r
+ return NULL;\r
+#else\r
+ return NULL;\r
+#endif /* HAVE_DEFAULT_NODENAME_MAP */\r
+ }\r
+\r
+ map = malloc(sizeof(*map));\r
+ if (!map)\r
+ return NULL;\r
+ cl_qmap_init(map);\r
+\r
+ if (parse_node_map(node_name_map, map_name, map)) {\r
+ fprintf(stderr,\r
+ "WARNING failed to open node name map \"%s\" (%s)\n",\r
+ node_name_map, strerror(errno));\r
+ close_node_name_map(map);\r
+ return NULL;\r
+ }\r
+\r
+ return map;\r
+}\r
+\r
+void\r
+close_node_name_map(nn_map_t *map)\r
+{\r
+ name_map_item_t *item = NULL;\r
+\r
+ if (!map)\r
+ return;\r
+\r
+ item = (name_map_item_t *)cl_qmap_head(map);\r
+ while (item != (name_map_item_t *)cl_qmap_end(map)) {\r
+ item = (name_map_item_t *)cl_qmap_remove(map, item->guid);\r
+ free(item->name);\r
+ free(item);\r
+ item = (name_map_item_t *)cl_qmap_head(map);\r
+ }\r
+ free(map);\r
+}\r
+\r
+char *\r
+remap_node_name(nn_map_t *map, uint64_t target_guid, char *nodedesc)\r
+{\r
+ char *rc = NULL;\r
+ name_map_item_t *item = NULL;\r
+\r
+ if (!map)\r
+ goto done;\r
+\r
+ item = (name_map_item_t *)cl_qmap_get(map, target_guid);\r
+ if (item != (name_map_item_t *)cl_qmap_end(map))\r
+ rc = strdup(item->name);\r
+\r
+done:\r
+ if (rc == NULL)\r
+ rc = strdup(clean_nodedesc(nodedesc));\r
+ return (rc);\r
+}\r
+\r
+char *\r
+clean_nodedesc(char *nodedesc)\r
+{\r
+ int i = 0;\r
+\r
+ nodedesc[63] = '\0';\r
+ while (nodedesc[i]) {\r
+ if (!isprint(nodedesc[i]))\r
+ nodedesc[i] = ' ';\r
+ i++;\r
+ }\r
+\r
+ return (nodedesc);\r
+}\r
+\r
+int parse_node_map(const char *file_name,\r
+ int (*create)(void *, uint64_t, char *), void *cxt)\r
+{\r
+ char line[256];\r
+ FILE *f;\r
+\r
+ if (!(f = fopen(file_name, "r")))\r
+ return -1;\r
+\r
+ while (fgets(line, sizeof(line), f)) {\r
+ uint64_t guid;\r
+ char *p, *e;\r
+\r
+ p = line;\r
+ while (isspace(*p))\r
+ p++;\r
+ if (*p == '\0' || *p == '\n' || *p == '#')\r
+ continue;\r
+\r
+ guid = strtoull(p, &e, 0);\r
+ if (e == p || (!isspace(*e) && *e != '#' && *e != '\0')) {\r
+ fclose(f);\r
+ return -1;\r
+ }\r
+\r
+ p = e;\r
+ while (isspace(*p))\r
+ p++;\r
+\r
+ e = strpbrk(p, "\n");\r
+ if (e)\r
+ *e = '\0';\r
+\r
+ if (create(cxt, guid, p)) {\r
+ fclose(f);\r
+ return -1;\r
+ }\r
+ }\r
+\r
+ fclose(f);\r
+ return 0;\r
+}\r