xref: /titanic_50/usr/src/lib/libpkg/common/rrmdir.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*5c51f124SMoriah Waterland /* All Rights Reserved */
29*5c51f124SMoriah Waterland 
30*5c51f124SMoriah Waterland 
31*5c51f124SMoriah Waterland 
32*5c51f124SMoriah Waterland #include <limits.h>
33*5c51f124SMoriah Waterland #include <stdio.h>
34*5c51f124SMoriah Waterland #include <stdlib.h>
35*5c51f124SMoriah Waterland #include <unistd.h>
36*5c51f124SMoriah Waterland #include <string.h>
37*5c51f124SMoriah Waterland #include <strings.h>
38*5c51f124SMoriah Waterland #include "pkglocale.h"
39*5c51f124SMoriah Waterland #include "pkglib.h"
40*5c51f124SMoriah Waterland 
41*5c51f124SMoriah Waterland int
rrmdir(char * a_path)42*5c51f124SMoriah Waterland rrmdir(char *a_path)
43*5c51f124SMoriah Waterland {
44*5c51f124SMoriah Waterland 	char	path[PATH_MAX+13];
45*5c51f124SMoriah Waterland 	int	i;
46*5c51f124SMoriah Waterland 	int	status;
47*5c51f124SMoriah Waterland 
48*5c51f124SMoriah Waterland 	/*
49*5c51f124SMoriah Waterland 	 * For some reason, a simple "rm -rf" will remove the contents
50*5c51f124SMoriah Waterland 	 * of the directory, but will fail to remove the directory itself
51*5c51f124SMoriah Waterland 	 * with "No such file or directory" when running the pkg commands
52*5c51f124SMoriah Waterland 	 * under a virtual root via the "chroot" command.  This has been
53*5c51f124SMoriah Waterland 	 * seen so far only with the `pkgremove' command, and when the
54*5c51f124SMoriah Waterland 	 * the directory is NFS mounted from a 4.x server.  This should
55*5c51f124SMoriah Waterland 	 * probably be revisited at a later time, but for now we'll just
56*5c51f124SMoriah Waterland 	 * remove the directory contents first, then the directory.
57*5c51f124SMoriah Waterland 	 */
58*5c51f124SMoriah Waterland 
59*5c51f124SMoriah Waterland 	/* do not allow removal of all root files via blank path */
60*5c51f124SMoriah Waterland 
61*5c51f124SMoriah Waterland 	if ((a_path == NULL) || (*a_path == '\0')) {
62*5c51f124SMoriah Waterland 		(void) fprintf(stderr,
63*5c51f124SMoriah Waterland 		    pkg_gt("warning: rrmdir(path==NULL): nothing deleted\n"));
64*5c51f124SMoriah Waterland 		return (0);
65*5c51f124SMoriah Waterland 	}
66*5c51f124SMoriah Waterland 
67*5c51f124SMoriah Waterland 	/*
68*5c51f124SMoriah Waterland 	 * first generate path with slash-star at the end and attempt to remove
69*5c51f124SMoriah Waterland 	 * all files first. If successful then remove with just the path only.
70*5c51f124SMoriah Waterland 	 */
71*5c51f124SMoriah Waterland 
72*5c51f124SMoriah Waterland 	(void) snprintf(path, sizeof (path), "%s/", a_path);
73*5c51f124SMoriah Waterland 	i = e_ExecCmdList(&status, (char **)NULL, (char *)NULL,
74*5c51f124SMoriah Waterland 		"/bin/rm", "rm", "-rf", path, (char *)NULL);
75*5c51f124SMoriah Waterland 
76*5c51f124SMoriah Waterland 	if (access(a_path, F_OK) == 0) {
77*5c51f124SMoriah Waterland 		i = e_ExecCmdList(&status, (char **)NULL, (char *)NULL,
78*5c51f124SMoriah Waterland 			"/bin/rmdir", "rmdir", a_path, (char *)NULL);
79*5c51f124SMoriah Waterland 	}
80*5c51f124SMoriah Waterland 
81*5c51f124SMoriah Waterland 	/* return 0 if last command successful, else return 1 */
82*5c51f124SMoriah Waterland 	return ((i == 0 && status == 0) ? 0 : 1);
83*5c51f124SMoriah Waterland }
84