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