xref: /onnv-gate/usr/src/lib/libpkg/common/rrmdir.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*9781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM 
32*9781SMoriah.Waterland@Sun.COM #include <limits.h>
33*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
34*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
35*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
36*9781SMoriah.Waterland@Sun.COM #include <string.h>
37*9781SMoriah.Waterland@Sun.COM #include <strings.h>
38*9781SMoriah.Waterland@Sun.COM #include "pkglocale.h"
39*9781SMoriah.Waterland@Sun.COM #include "pkglib.h"
40*9781SMoriah.Waterland@Sun.COM 
41*9781SMoriah.Waterland@Sun.COM int
rrmdir(char * a_path)42*9781SMoriah.Waterland@Sun.COM rrmdir(char *a_path)
43*9781SMoriah.Waterland@Sun.COM {
44*9781SMoriah.Waterland@Sun.COM 	char	path[PATH_MAX+13];
45*9781SMoriah.Waterland@Sun.COM 	int	i;
46*9781SMoriah.Waterland@Sun.COM 	int	status;
47*9781SMoriah.Waterland@Sun.COM 
48*9781SMoriah.Waterland@Sun.COM 	/*
49*9781SMoriah.Waterland@Sun.COM 	 * For some reason, a simple "rm -rf" will remove the contents
50*9781SMoriah.Waterland@Sun.COM 	 * of the directory, but will fail to remove the directory itself
51*9781SMoriah.Waterland@Sun.COM 	 * with "No such file or directory" when running the pkg commands
52*9781SMoriah.Waterland@Sun.COM 	 * under a virtual root via the "chroot" command.  This has been
53*9781SMoriah.Waterland@Sun.COM 	 * seen so far only with the `pkgremove' command, and when the
54*9781SMoriah.Waterland@Sun.COM 	 * the directory is NFS mounted from a 4.x server.  This should
55*9781SMoriah.Waterland@Sun.COM 	 * probably be revisited at a later time, but for now we'll just
56*9781SMoriah.Waterland@Sun.COM 	 * remove the directory contents first, then the directory.
57*9781SMoriah.Waterland@Sun.COM 	 */
58*9781SMoriah.Waterland@Sun.COM 
59*9781SMoriah.Waterland@Sun.COM 	/* do not allow removal of all root files via blank path */
60*9781SMoriah.Waterland@Sun.COM 
61*9781SMoriah.Waterland@Sun.COM 	if ((a_path == NULL) || (*a_path == '\0')) {
62*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr,
63*9781SMoriah.Waterland@Sun.COM 		    pkg_gt("warning: rrmdir(path==NULL): nothing deleted\n"));
64*9781SMoriah.Waterland@Sun.COM 		return (0);
65*9781SMoriah.Waterland@Sun.COM 	}
66*9781SMoriah.Waterland@Sun.COM 
67*9781SMoriah.Waterland@Sun.COM 	/*
68*9781SMoriah.Waterland@Sun.COM 	 * first generate path with slash-star at the end and attempt to remove
69*9781SMoriah.Waterland@Sun.COM 	 * all files first. If successful then remove with just the path only.
70*9781SMoriah.Waterland@Sun.COM 	 */
71*9781SMoriah.Waterland@Sun.COM 
72*9781SMoriah.Waterland@Sun.COM 	(void) snprintf(path, sizeof (path), "%s/", a_path);
73*9781SMoriah.Waterland@Sun.COM 	i = e_ExecCmdList(&status, (char **)NULL, (char *)NULL,
74*9781SMoriah.Waterland@Sun.COM 		"/bin/rm", "rm", "-rf", path, (char *)NULL);
75*9781SMoriah.Waterland@Sun.COM 
76*9781SMoriah.Waterland@Sun.COM 	if (access(a_path, F_OK) == 0) {
77*9781SMoriah.Waterland@Sun.COM 		i = e_ExecCmdList(&status, (char **)NULL, (char *)NULL,
78*9781SMoriah.Waterland@Sun.COM 			"/bin/rmdir", "rmdir", a_path, (char *)NULL);
79*9781SMoriah.Waterland@Sun.COM 	}
80*9781SMoriah.Waterland@Sun.COM 
81*9781SMoriah.Waterland@Sun.COM 	/* return 0 if last command successful, else return 1 */
82*9781SMoriah.Waterland@Sun.COM 	return ((i == 0 && status == 0) ? 0 : 1);
83*9781SMoriah.Waterland@Sun.COM }
84