17aec1d6eScindi /*
27aec1d6eScindi  * CDDL HEADER START
37aec1d6eScindi  *
47aec1d6eScindi  * The contents of this file are subject to the terms of the
57aec1d6eScindi  * Common Development and Distribution License (the "License").
67aec1d6eScindi  * You may not use this file except in compliance with the License.
77aec1d6eScindi  *
87aec1d6eScindi  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97aec1d6eScindi  * or http://www.opensolaris.org/os/licensing.
107aec1d6eScindi  * See the License for the specific language governing permissions
117aec1d6eScindi  * and limitations under the License.
127aec1d6eScindi  *
137aec1d6eScindi  * When distributing Covered Code, include this CDDL HEADER in each
147aec1d6eScindi  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157aec1d6eScindi  * If applicable, add the following below this CDDL HEADER, with the
167aec1d6eScindi  * fields enclosed by brackets "[]" replaced with your own identifying
177aec1d6eScindi  * information: Portions Copyright [yyyy] [name of copyright owner]
187aec1d6eScindi  *
197aec1d6eScindi  * CDDL HEADER END
207aec1d6eScindi  */
217aec1d6eScindi 
227aec1d6eScindi /*
23*4557a2a1Srobj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247aec1d6eScindi  * Use is subject to license terms.
257aec1d6eScindi  */
267aec1d6eScindi 
277aec1d6eScindi #include <limits.h>
287aec1d6eScindi #include <string.h>
290eb822a1Scindi #include <unistd.h>
307aec1d6eScindi #include <sys/param.h>
317aec1d6eScindi #include <topo_error.h>
327aec1d6eScindi #include <topo_tree.h>
337aec1d6eScindi #include <topo_subr.h>
347aec1d6eScindi #include <topo_file.h>
357aec1d6eScindi 
367aec1d6eScindi /*
377aec1d6eScindi  * topo_file.c
387aec1d6eScindi  *
397aec1d6eScindi  *	This file hides the details of any file manipulation to
400eb822a1Scindi  *	establish topology for a given enumerator.
417aec1d6eScindi  */
427aec1d6eScindi 
430eb822a1Scindi #define	TOPO_DEFAULT_FILE	"maps/%s-%s-topology.xml"
440eb822a1Scindi #define	TOPO_COMMON_FILE	"maps/%s-topology.xml"
457aec1d6eScindi 
460eb822a1Scindi static void
topo_file_unload(topo_file_t * tfp)470eb822a1Scindi topo_file_unload(topo_file_t *tfp)
487aec1d6eScindi {
497aec1d6eScindi 
500eb822a1Scindi 	if (tfp == NULL)
510eb822a1Scindi 		return;
520eb822a1Scindi 
530eb822a1Scindi 	if (tfp->tf_filenm != NULL)
540eb822a1Scindi 		topo_mod_strfree(tfp->tf_mod, tfp->tf_filenm);
550eb822a1Scindi 
560eb822a1Scindi 	if (tfp->tf_tmap != NULL)
570eb822a1Scindi 		tf_info_free(tfp->tf_mod, tfp->tf_tmap);
580eb822a1Scindi 
590eb822a1Scindi 	topo_mod_free(tfp->tf_mod, tfp, sizeof (topo_file_t));
607aec1d6eScindi }
617aec1d6eScindi 
627aec1d6eScindi int
topo_file_load(topo_mod_t * mod,tnode_t * node,const char * name,const char * scheme,int pmap)630eb822a1Scindi topo_file_load(topo_mod_t *mod, tnode_t *node, const char *name,
64*4557a2a1Srobj     const char *scheme, int pmap)
657aec1d6eScindi {
667aec1d6eScindi 	topo_file_t *tfp;
670eb822a1Scindi 	char fp[MAXNAMELEN];
687aec1d6eScindi 
690eb822a1Scindi 	if ((tfp = topo_mod_zalloc(mod, sizeof (topo_file_t))) == NULL)
700eb822a1Scindi 		return (topo_mod_seterrno(mod, ETOPO_NOMEM));
717aec1d6eScindi 
727aec1d6eScindi 	tfp->tf_mod = mod;
737aec1d6eScindi 
740eb822a1Scindi 	if (name != NULL)
750eb822a1Scindi 		(void) snprintf(fp, MAXNAMELEN, TOPO_DEFAULT_FILE, name,
760eb822a1Scindi 		    scheme);
770eb822a1Scindi 	else
780eb822a1Scindi 		(void) snprintf(fp, MAXNAMELEN, TOPO_COMMON_FILE, scheme);
797aec1d6eScindi 
800eb822a1Scindi 	if ((tfp->tf_filenm = topo_search_path(mod, mod->tm_rootdir, fp))
810eb822a1Scindi 	    == NULL) {
820eb822a1Scindi 		topo_file_unload(tfp);
830eb822a1Scindi 		return (topo_mod_seterrno(mod, ETOPO_MOD_NOENT));
847aec1d6eScindi 	}
857aec1d6eScindi 
860eb822a1Scindi 	if ((tfp->tf_tmap = topo_xml_read(mod, tfp->tf_filenm, scheme))
870eb822a1Scindi 	    == NULL) {
880eb822a1Scindi 		topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
890eb822a1Scindi 		    "failed to load topology file %s: "
900eb822a1Scindi 		    "%s\n", tfp->tf_filenm, topo_strerror(ETOPO_MOD_XRD));
910eb822a1Scindi 		topo_file_unload(tfp);
920eb822a1Scindi 		return (topo_mod_seterrno(mod, ETOPO_MOD_XRD));
930eb822a1Scindi 	}
947aec1d6eScindi 
95*4557a2a1Srobj 	if (pmap)
96*4557a2a1Srobj 		tfp->tf_tmap->tf_flags |= TF_PROPMAP;
97*4557a2a1Srobj 
980eb822a1Scindi 	if (topo_xml_enum(mod, tfp->tf_tmap, node) < 0) {
990eb822a1Scindi 		topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
1000eb822a1Scindi 		    "Failed to enumerate topology: %s\n",
1010eb822a1Scindi 		    topo_strerror(ETOPO_MOD_XENUM));
1020eb822a1Scindi 		topo_file_unload(tfp);
1030eb822a1Scindi 		return (topo_mod_seterrno(mod, ETOPO_MOD_XENUM));
1040eb822a1Scindi 	}
1057aec1d6eScindi 
1060eb822a1Scindi 	topo_file_unload(tfp);
1077aec1d6eScindi 
1080eb822a1Scindi 	return (0);
1097aec1d6eScindi }
110