1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland 
28*5c51f124SMoriah Waterland /*
29*5c51f124SMoriah Waterland  * System includes
30*5c51f124SMoriah Waterland  */
31*5c51f124SMoriah Waterland 
32*5c51f124SMoriah Waterland #include <stdio.h>
33*5c51f124SMoriah Waterland #include <time.h>
34*5c51f124SMoriah Waterland #include <wait.h>
35*5c51f124SMoriah Waterland #include <stdlib.h>
36*5c51f124SMoriah Waterland #include <unistd.h>
37*5c51f124SMoriah Waterland #include <ulimit.h>
38*5c51f124SMoriah Waterland #include <sys/stat.h>
39*5c51f124SMoriah Waterland #include <sys/statvfs.h>
40*5c51f124SMoriah Waterland #include <assert.h>
41*5c51f124SMoriah Waterland #include <fcntl.h>
42*5c51f124SMoriah Waterland #include <errno.h>
43*5c51f124SMoriah Waterland #include <ctype.h>
44*5c51f124SMoriah Waterland #include <dirent.h>
45*5c51f124SMoriah Waterland #include <string.h>
46*5c51f124SMoriah Waterland #include <signal.h>
47*5c51f124SMoriah Waterland #include <locale.h>
48*5c51f124SMoriah Waterland #include <libintl.h>
49*5c51f124SMoriah Waterland #include <pkgstrct.h>
50*5c51f124SMoriah Waterland #include <pkginfo.h>
51*5c51f124SMoriah Waterland #include <pkgdev.h>
52*5c51f124SMoriah Waterland #include <pkglocs.h>
53*5c51f124SMoriah Waterland #include <pwd.h>
54*5c51f124SMoriah Waterland 
55*5c51f124SMoriah Waterland 
56*5c51f124SMoriah Waterland /*
57*5c51f124SMoriah Waterland  * consolidation pkg command library includes
58*5c51f124SMoriah Waterland  */
59*5c51f124SMoriah Waterland 
60*5c51f124SMoriah Waterland #include <pkglib.h>
61*5c51f124SMoriah Waterland 
62*5c51f124SMoriah Waterland /*
63*5c51f124SMoriah Waterland  * local pkg command library includes
64*5c51f124SMoriah Waterland  */
65*5c51f124SMoriah Waterland 
66*5c51f124SMoriah Waterland #include "libinst.h"
67*5c51f124SMoriah Waterland #include "libadm.h"
68*5c51f124SMoriah Waterland #include "messages.h"
69*5c51f124SMoriah Waterland 
70*5c51f124SMoriah Waterland /*
71*5c51f124SMoriah Waterland  * Name:	unpack_package_from_stream
72*5c51f124SMoriah Waterland  * Description:	unpack a package from a stream into a temporary directory
73*5c51f124SMoriah Waterland  * Arguments:	a_idsName - pointer to string representing the input data
74*5c51f124SMoriah Waterland  *			stream containing the package to unpack
75*5c51f124SMoriah Waterland  *		a_pkginst - pointer to string representing the name of
76*5c51f124SMoriah Waterland  *			the package to unpack from the specified stream
77*5c51f124SMoriah Waterland  *		a_tempDir - pointer to string representing the path to a
78*5c51f124SMoriah Waterland  *			directory into which the package will be unpacked
79*5c51f124SMoriah Waterland  * Returns:	boolean_t
80*5c51f124SMoriah Waterland  *			== B_TRUE - package successfully unpacked from stream
81*5c51f124SMoriah Waterland  *			== B_FALSE - failed to unpack package from stream
82*5c51f124SMoriah Waterland  */
83*5c51f124SMoriah Waterland 
84*5c51f124SMoriah Waterland boolean_t
unpack_package_from_stream(char * a_idsName,char * a_pkginst,char * a_tempDir)85*5c51f124SMoriah Waterland unpack_package_from_stream(char *a_idsName, char *a_pkginst, char *a_tempDir)
86*5c51f124SMoriah Waterland {
87*5c51f124SMoriah Waterland 	int		dparts;
88*5c51f124SMoriah Waterland 	char		instdir[PATH_MAX];
89*5c51f124SMoriah Waterland 
90*5c51f124SMoriah Waterland 	/* entry assertions */
91*5c51f124SMoriah Waterland 
92*5c51f124SMoriah Waterland 	assert(a_idsName != (char *)NULL);
93*5c51f124SMoriah Waterland 	assert(a_pkginst != (char *)NULL);
94*5c51f124SMoriah Waterland 	assert(a_tempDir != (char *)NULL);
95*5c51f124SMoriah Waterland 
96*5c51f124SMoriah Waterland 	/* entry debug information */
97*5c51f124SMoriah Waterland 
98*5c51f124SMoriah Waterland 	echoDebug(DBG_UNPACKSTRM_ENTRY);
99*5c51f124SMoriah Waterland 	echoDebug(DBG_UNPACKSTRM_ARGS, a_pkginst, a_idsName, a_tempDir);
100*5c51f124SMoriah Waterland 
101*5c51f124SMoriah Waterland 	/* find the specified package in the datastream */
102*5c51f124SMoriah Waterland 
103*5c51f124SMoriah Waterland 	dparts = ds_findpkg(a_idsName, a_pkginst);
104*5c51f124SMoriah Waterland 	if (dparts < 1) {
105*5c51f124SMoriah Waterland 		progerr(gettext(ERR_DSARCH), a_pkginst);
106*5c51f124SMoriah Waterland 		return (B_FALSE);
107*5c51f124SMoriah Waterland 		/*NOTREACHED*/
108*5c51f124SMoriah Waterland 	}
109*5c51f124SMoriah Waterland 
110*5c51f124SMoriah Waterland 	/*
111*5c51f124SMoriah Waterland 	 * read in next part from stream, even if we decide
112*5c51f124SMoriah Waterland 	 * later that we don't need it
113*5c51f124SMoriah Waterland 	 */
114*5c51f124SMoriah Waterland 
115*5c51f124SMoriah Waterland 	/* create directory to hold this package instance */
116*5c51f124SMoriah Waterland 
117*5c51f124SMoriah Waterland 	if (snprintf(instdir, sizeof (instdir), "%s/%s", a_tempDir, a_pkginst)
118*5c51f124SMoriah Waterland 	    >= PATH_MAX) {
119*5c51f124SMoriah Waterland 		progerr(ERR_CREATE_PATH_2, a_tempDir, a_pkginst);
120*5c51f124SMoriah Waterland 		return (B_FALSE);
121*5c51f124SMoriah Waterland 	}
122*5c51f124SMoriah Waterland 
123*5c51f124SMoriah Waterland 	switch (fmkdir(instdir, 0755)) {
124*5c51f124SMoriah Waterland 	case 0:	/* directory created */
125*5c51f124SMoriah Waterland 		break;
126*5c51f124SMoriah Waterland 	case 1: /* could not remove existing non-directory node */
127*5c51f124SMoriah Waterland 		progerr(ERR_REMOVE, instdir, strerror(errno));
128*5c51f124SMoriah Waterland 		return (B_FALSE);
129*5c51f124SMoriah Waterland 	case 2: /* could not create specified new directory */
130*5c51f124SMoriah Waterland 	default:
131*5c51f124SMoriah Waterland 		progerr(ERR_UNPACK_FMKDIR, instdir, strerror(errno));
132*5c51f124SMoriah Waterland 		return (B_FALSE);
133*5c51f124SMoriah Waterland 	}
134*5c51f124SMoriah Waterland 
135*5c51f124SMoriah Waterland 	/* unpack package instance from stream to dir created */
136*5c51f124SMoriah Waterland 
137*5c51f124SMoriah Waterland 	echoDebug(DBG_UNPACKSTRM_UNPACKING, a_pkginst, a_idsName, instdir);
138*5c51f124SMoriah Waterland 
139*5c51f124SMoriah Waterland 	if (chdir(instdir)) {
140*5c51f124SMoriah Waterland 		progerr(ERR_CHDIR, instdir);
141*5c51f124SMoriah Waterland 		return (B_FALSE);
142*5c51f124SMoriah Waterland 	}
143*5c51f124SMoriah Waterland 
144*5c51f124SMoriah Waterland 	while (dparts--) {
145*5c51f124SMoriah Waterland 		if (ds_next(a_idsName, instdir)) {
146*5c51f124SMoriah Waterland 			progerr(ERR_UNPACK_DSREAD, dparts+1, a_idsName, instdir,
147*5c51f124SMoriah Waterland 				a_pkginst);
148*5c51f124SMoriah Waterland 			return (B_FALSE);
149*5c51f124SMoriah Waterland 		}
150*5c51f124SMoriah Waterland 	}
151*5c51f124SMoriah Waterland 
152*5c51f124SMoriah Waterland 	if (chdir(get_PKGADM())) {
153*5c51f124SMoriah Waterland 		progerr(gettext(ERR_CHDIR), get_PKGADM());
154*5c51f124SMoriah Waterland 		return (B_FALSE);
155*5c51f124SMoriah Waterland 	}
156*5c51f124SMoriah Waterland 
157*5c51f124SMoriah Waterland 	return (B_TRUE);
158*5c51f124SMoriah Waterland }
159