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 2004 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 #include <stdio.h>
29*5c51f124SMoriah Waterland #include <stdlib.h>
30*5c51f124SMoriah Waterland #include <unistd.h>
31*5c51f124SMoriah Waterland #include <sys/stat.h>
32*5c51f124SMoriah Waterland #include <assert.h>
33*5c51f124SMoriah Waterland #include <fcntl.h>
34*5c51f124SMoriah Waterland #include <errno.h>
35*5c51f124SMoriah Waterland #include <ctype.h>
36*5c51f124SMoriah Waterland #include <string.h>
37*5c51f124SMoriah Waterland #include <locale.h>
38*5c51f124SMoriah Waterland #include <libintl.h>
39*5c51f124SMoriah Waterland #include <pwd.h>
40*5c51f124SMoriah Waterland 
41*5c51f124SMoriah Waterland /*
42*5c51f124SMoriah Waterland  * consolidation pkg command library includes
43*5c51f124SMoriah Waterland  */
44*5c51f124SMoriah Waterland 
45*5c51f124SMoriah Waterland #include <pkglib.h>
46*5c51f124SMoriah Waterland 
47*5c51f124SMoriah Waterland /*
48*5c51f124SMoriah Waterland  * local pkg command library includes
49*5c51f124SMoriah Waterland  */
50*5c51f124SMoriah Waterland 
51*5c51f124SMoriah Waterland #include "install.h"
52*5c51f124SMoriah Waterland #include "libinst.h"
53*5c51f124SMoriah Waterland #include "libadm.h"
54*5c51f124SMoriah Waterland #include "messages.h"
55*5c51f124SMoriah Waterland 
56*5c51f124SMoriah Waterland /*
57*5c51f124SMoriah Waterland  * Name:	setup_temporary_directory
58*5c51f124SMoriah Waterland  * Description:	create a temporary directory from specified components
59*5c51f124SMoriah Waterland  *		and return full path to the directory created
60*5c51f124SMoriah Waterland  * Arguments:	r_dirname - pointer to handle to string - on success,
61*5c51f124SMoriah Waterland  *			the full path to the temporary directory created
62*5c51f124SMoriah Waterland  *			is returned in this handle
63*5c51f124SMoriah Waterland  *		a_tmpdir - pointer to string representing the directory into
64*5c51f124SMoriah Waterland  *			which the new temporary directory should be created
65*5c51f124SMoriah Waterland  *		a_suffix - pointer to string representing the 5-character
66*5c51f124SMoriah Waterland  *			suffix to be used as the first part of the temporary
67*5c51f124SMoriah Waterland  *			directory name invented
68*5c51f124SMoriah Waterland  * Returns:	boolean_t
69*5c51f124SMoriah Waterland  *			== B_TRUE - temporary directory created, path returned
70*5c51f124SMoriah Waterland  *			== B_FALSE - failed to create temporary directory
71*5c51f124SMoriah Waterland  *				'errno' is set to the failure reason
72*5c51f124SMoriah Waterland  * NOTE:    	Any path returned is placed in new storage for the
73*5c51f124SMoriah Waterland  *		calling function. The caller must use 'free' to dispose
74*5c51f124SMoriah Waterland  *		of the storage once the path is no longer needed.
75*5c51f124SMoriah Waterland  */
76*5c51f124SMoriah Waterland 
77*5c51f124SMoriah Waterland boolean_t
setup_temporary_directory(char ** r_dirname,char * a_tmpdir,char * a_suffix)78*5c51f124SMoriah Waterland setup_temporary_directory(char **r_dirname, char *a_tmpdir, char *a_suffix)
79*5c51f124SMoriah Waterland {
80*5c51f124SMoriah Waterland 	char	*dirname;
81*5c51f124SMoriah Waterland 
82*5c51f124SMoriah Waterland 	/* entry assertions */
83*5c51f124SMoriah Waterland 
84*5c51f124SMoriah Waterland 	assert(a_tmpdir != (char *)NULL);
85*5c51f124SMoriah Waterland 
86*5c51f124SMoriah Waterland 	/* error if no pointer provided to return temporary name in */
87*5c51f124SMoriah Waterland 
88*5c51f124SMoriah Waterland 	if (r_dirname == (char **)NULL) {
89*5c51f124SMoriah Waterland 		errno = EFAULT;			/* bad address */
90*5c51f124SMoriah Waterland 		return (B_FALSE);
91*5c51f124SMoriah Waterland 	}
92*5c51f124SMoriah Waterland 
93*5c51f124SMoriah Waterland 	/* generate temporary directory name */
94*5c51f124SMoriah Waterland 
95*5c51f124SMoriah Waterland 	dirname = tempnam(a_tmpdir, a_suffix);
96*5c51f124SMoriah Waterland 	if (dirname == (char *)NULL) {
97*5c51f124SMoriah Waterland 		return (B_FALSE);
98*5c51f124SMoriah Waterland 	}
99*5c51f124SMoriah Waterland 
100*5c51f124SMoriah Waterland 	/* create the temporary directory */
101*5c51f124SMoriah Waterland 
102*5c51f124SMoriah Waterland 	if (mkdir(dirname, 0755) != 0) {
103*5c51f124SMoriah Waterland 		return (B_FALSE);
104*5c51f124SMoriah Waterland 	}
105*5c51f124SMoriah Waterland 
106*5c51f124SMoriah Waterland 	echoDebug(DBG_SETUP_TEMPDIR, dirname);
107*5c51f124SMoriah Waterland 
108*5c51f124SMoriah Waterland 	*r_dirname = dirname;
109*5c51f124SMoriah Waterland 
110*5c51f124SMoriah Waterland 	return (B_TRUE);
111*5c51f124SMoriah Waterland }
112