1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 
32 #include <limits.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <libinst.h>
39 
40 char *
srcpath(char * dir,char * src,int part,int nparts)41 srcpath(char *dir, char *src, int part, int nparts)
42 {
43 	static char tmppath[PATH_MAX];
44 	char	*copy;
45 	size_t	copyLen;
46 
47 	copy = tmppath;
48 
49 	if (dir != NULL) {
50 		size_t theLen = strlen(dir);
51 
52 		(void) strcpy(copy, dir);
53 		copy += theLen;
54 		copyLen = (sizeof (tmppath) - theLen);
55 	} else {
56 		copy[0] = '\0';
57 		copyLen = sizeof (tmppath);
58 	}
59 
60 	if (nparts > 1) {
61 		(void) snprintf(copy, copyLen,
62 			((src[0] == '/') ? "/root.%d%s" : "/reloc.%d/%s"),
63 			part, src);
64 	} else {
65 		(void) snprintf(copy, copyLen,
66 			((src[0] == '/') ? "/root%s" : "/reloc/%s"), src);
67 	}
68 
69 	return (tmppath);
70 }
71 
72 /*
73  * During a partial install(Ex. Migration of a zone), if the'contchg' field of
74  * mstat structure is set i.e. there is a mismatch between the entry in pkgmap
75  * and package database and the file is of type 'f', the source path on the
76  * Global zone is to be generated(mostly for being copied again to the NGZ).
77  * Given the local source path(relocatable), this function builds the absolute
78  * source path.
79  *
80  * NOTE: This function is a private interface. Should only be called during a
81  *	 a partial install and for files of type 'f'.
82  *	 Source translation is done differently from 'e' and 'v' types.
83  */
84 char *
trans_srcp_pi(char * local_path)85 trans_srcp_pi(char *local_path)
86 {
87 	static char pi_srcPath[PATH_MAX];
88 	char *tmp_basedir, *tmp_inst_root;
89 	int inst_root_len, basedir_len;
90 
91 	/* Get the basedir and it's length */
92 	tmp_basedir = get_basedir();
93 	basedir_len = strlen(tmp_basedir);
94 
95 	/* Get the install root and it's length */
96 	tmp_inst_root = get_inst_root();
97 	inst_root_len = strlen(tmp_inst_root);
98 
99 	/*
100 	 * Get past install root if something exists
101 	 * Example:
102 	 * INSTROOT = /a (on scratch zone)
103 	 * BASEDIR = /a/usr (on scratch zone)
104 	 * local_path = "~bin/ls"
105 	 *
106 	 * Absolute path for source on GZ:
107 	 * a) If BASEDIR == INSTROOT
108 	 *	/<local_path string starting from index 1>
109 	 * In the above example, absolute path is
110 	 * 	/bin/ls
111 	 *
112 	 * b) If BASEDIR > INSTROOT
113 	 *	/usr/<local_path string starting from index 1>
114 	 * In the above example, absolute path is
115 	 * 	/usr/bin/ls
116 	 */
117 	if ((strncmp(tmp_inst_root, tmp_basedir, inst_root_len) == 0) &&
118 	    (inst_root_len == basedir_len)) {
119 		/*
120 		 * Prefix root to the local path. NOTE that local_path[0]
121 		 * has a '~' character. Move past it.
122 		 *
123 		 * NOTE: local_path array size is expected to be >= 2.
124 		 */
125 		(void) snprintf(pi_srcPath, PATH_MAX, "/%s",
126 		    &(local_path[1]));
127 	} else {
128 		/*
129 		 * NOTE: local_path array size is expected to be >= 2.
130 		 */
131 		(void) snprintf(pi_srcPath, PATH_MAX, "%s/%s",
132 		    &(tmp_basedir[inst_root_len]), &(local_path[1]));
133 	}
134 
135 	return (pi_srcPath);
136 }
137