blob: a779060a06a1b1e2a7084d8a16e29ef432ed3761 [file] [log] [blame]
alaffincc2e5552000-07-27 17:13:18 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
alaffincc2e5552000-07-27 17:13:18 +00004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
vapier45a8ba02009-07-20 10:59:32 +00007 *
alaffincc2e5552000-07-27 17:13:18 +00008 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
vapier45a8ba02009-07-20 10:59:32 +000011 *
alaffincc2e5552000-07-27 17:13:18 +000012 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
vapier45a8ba02009-07-20 10:59:32 +000018 *
alaffincc2e5552000-07-27 17:13:18 +000019 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
alaffincc2e5552000-07-27 17:13:18 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
alaffincc2e5552000-07-27 17:13:18 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32
vapier45a8ba02009-07-20 10:59:32 +000033/* $Id: rmobj.c,v 1.5 2009/07/20 10:59:32 vapier Exp $ */
alaffincc2e5552000-07-27 17:13:18 +000034
35/**********************************************************
36 *
37 * OS Testing - Silicon Graphics, Inc.
38 *
39 * FUNCTION NAME : rmobj()
40 *
41 * FUNCTION TITLE : Remove an object
42 *
43 * SYNOPSIS:
44 * int rmobj(char *obj, char **errmsg)
45 *
46 * AUTHOR : Kent Rogers
47 *
48 * INITIAL RELEASE : UNICOS 7.0
49 *
50 * USER DESCRIPTION
51 * This routine will remove the specified object. If the specified
52 * object is a directory, it will recursively remove the directory
53 * and everything underneath it. It assumes that it has privilege
54 * to remove everything that it tries to remove. If rmobj() encounters
55 * any problems, and errmsg is not NULL, errmsg is set to point to a
56 * string explaining the error.
57 *
58 * DETAILED DESCRIPTION
59 * Allocate space for the directory and its contents
60 * Open the directory to get access to what is in it
61 * Loop through the objects in the directory:
62 * If the object is not "." or "..":
63 * Determine the file type by calling lstat()
64 * If the object is not a directory:
65 * Remove the object with unlink()
66 * Else:
67 * Call rmobj(object) to remove the object's contents
68 * Determine the link count on object by calling lstat()
69 * If the link count >= 3:
70 * Remove the directory with unlink()
71 * Else
72 * Remove the directory with rmdir()
73 * Close the directory and free the pointers
74 *
75 * RETURN VALUE
76 * If there are any problems, rmobj() will set errmsg (if it was not
77 * NULL) and return -1. Otherwise it will return 0.
78 *
79 ************************************************************/
80#include <errno.h> /* for errno */
81#include <stdio.h> /* for NULL */
82#include <stdlib.h> /* for malloc() */
83#include <string.h> /* for string function */
84#include <limits.h> /* for PATH_MAX */
85#include <sys/types.h> /* for opendir(), readdir(), closedir(), stat() */
86#include <sys/stat.h> /* for [l]stat() */
87#include <dirent.h> /* for opendir(), readdir(), closedir() */
nstraz94181082000-08-30 18:43:38 +000088#include <unistd.h> /* for rmdir(), unlink() */
alaffincc2e5552000-07-27 17:13:18 +000089#include "rmobj.h"
90
91#define SYSERR strerror(errno)
92
93int
94rmobj(char *obj, char **errmsg)
95{
96 int ret_val = 0; /* return value from this routine */
97 DIR *dir; /* pointer to a directory */
98 struct dirent *dir_ent; /* pointer to directory entries */
99 char dirobj[PATH_MAX]; /* object inside directory to modify */
100 struct stat statbuf; /* used to hold stat information */
101 static char err_msg[1024]; /* error message */
102
103 /* Determine the file type */
104 if ( lstat(obj, &statbuf) < 0 ) {
105 if ( errmsg != NULL ) {
106 sprintf(err_msg, "lstat(%s) failed; errno=%d: %s",
107 obj, errno, SYSERR);
108 *errmsg = err_msg;
109 }
110 return -1;
111 }
112
113 /* Take appropriate action, depending on the file type */
114 if ( (statbuf.st_mode & S_IFMT) == S_IFDIR ) {
115 /* object is a directory */
116
117 /* Do NOT perform the request if the directory is "/" */
118 if ( !strcmp(obj, "/") ) {
119 if ( errmsg != NULL ) {
120 sprintf(err_msg, "Cannot remove /");
121 *errmsg = err_msg;
122 }
123 return -1;
124 }
125
126 /* Open the directory to get access to what is in it */
127 if ( (dir = opendir(obj)) == NULL ) {
128 if ( rmdir(obj) != 0 ) {
129 if ( errmsg != NULL ) {
130 sprintf(err_msg, "rmdir(%s) failed; errno=%d: %s",
131 obj, errno, SYSERR);
132 *errmsg = err_msg;
133 }
134 return -1;
135 } else {
136 return 0;
137 }
138 }
139
140 /* Loop through the entries in the directory, removing each one */
141 for ( dir_ent = (struct dirent *)readdir(dir);
142 dir_ent != NULL;
143 dir_ent = (struct dirent *)readdir(dir)) {
144
145 /* Don't remove "." or ".." */
146 if ( !strcmp(dir_ent->d_name, ".") || !strcmp(dir_ent->d_name, "..") )
147 continue;
148
149 /* Recursively call this routine to remove the current entry */
150 sprintf(dirobj, "%s/%s", obj, dir_ent->d_name);
151 if ( rmobj(dirobj, errmsg) != 0 )
152 ret_val = -1;
153 }
154
155 /* Close the directory */
156 closedir(dir);
157
158 /* If there were problems removing an entry, don't attempt to
159 remove the directory itself */
160 if ( ret_val == -1 )
161 return -1;
162
163 /* Get the link count, now that all the entries have been removed */
164 if ( lstat(obj, &statbuf) < 0 ) {
165 if ( errmsg != NULL ) {
166 sprintf(err_msg, "lstat(%s) failed; errno=%d: %s",
167 obj, errno, SYSERR);
168 *errmsg = err_msg;
169 }
170 return -1;
171 }
172
173 /* Remove the directory itself */
174 if ( statbuf.st_nlink >= 3 ) {
175 /* The directory is linked; unlink() must be used */
176 if ( unlink(obj) < 0 ) {
177 if ( errmsg != NULL ) {
178 sprintf(err_msg, "unlink(%s) failed; errno=%d: %s",
179 obj, errno, SYSERR);
180 *errmsg = err_msg;
181 }
182 return -1;
183 }
184 } else {
robbiewcc88dd92003-07-28 16:03:02 +0000185 /* The directory is not linked; remove() can be used */
186 if ( remove(obj) < 0 ) {
alaffincc2e5552000-07-27 17:13:18 +0000187 if ( errmsg != NULL ) {
robbiewcc88dd92003-07-28 16:03:02 +0000188 sprintf(err_msg, "remove(%s) failed; errno=%d: %s",
alaffincc2e5552000-07-27 17:13:18 +0000189 obj, errno, SYSERR);
190 *errmsg = err_msg;
191 }
192 return -1;
193 }
194 }
195 } else {
196 /* object is not a directory; just use unlink() */
197 if ( unlink(obj) < 0 ) {
198 if ( errmsg != NULL ) {
199 sprintf(err_msg, "unlink(%s) failed; errno=%d: %s",
200 obj, errno, SYSERR);
201 *errmsg = err_msg;
202 }
203 return -1;
204 }
205 } /* if obj is a directory */
206
207 /*
208 * Everything must have went ok.
209 */
210 return 0;
211} /* rmobj() */