1 /* $NetBSD: remove.c,v 1.3 2009/08/02 17:56:45 joerg Exp $ */
4 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
42 __RCSID("$NetBSD: remove.c,v 1.3 2009/08/02 17:56:45 joerg Exp $");
75 long_remove(const char **path_ptr
, int missing_ok
, int *did_chdir
)
77 char tmp_path
[PATH_MAX
+ 1];
86 while (len
>= PATH_MAX
) {
87 for (i
= PATH_MAX
- 1; i
> 0; --i
) {
93 return -1; /* Assumes PATH_MAX > NAME_MAX */
95 memcpy(tmp_path
, path
, i
);
104 if (remove(path
) == 0 || (errno
== ENOENT
&& missing_ok
))
115 recursive_remove_internal(const char *path
, int missing_ok
, int cwd
)
119 const char *sub_path
;
124 * If the argument is longer than PATH_MAX, long_remove
125 * will try to shorten it using chdir. So before returning,
126 * make sure to fchdir back to the original cwd.
129 if (long_remove(&sub_path
, missing_ok
, &did_chdir
) == 0)
131 else if (errno
!= ENOTEMPTY
) /* Other errors are terminal. */
137 if (did_chdir
&& safe_fchdir(cwd
) == -1 && rv
== 0)
142 if ((dir
= opendir(sub_path
)) == NULL
) {
144 warn("opendir failed");
148 if (did_chdir
&& fchdir(cwd
) == -1)
153 while ((de
= readdir(dir
)) != NULL
) {
154 if (strcmp(de
->d_name
, ".") == 0)
156 if (strcmp(de
->d_name
, "..") == 0)
158 subdir
= xasprintf("%s/%s", path
, de
->d_name
);
159 rv
= recursive_remove_internal(subdir
, 1, cwd
);
167 rv
|= long_remove(&path
, missing_ok
, &did_chdir
);
169 if (did_chdir
&& safe_fchdir(cwd
) == -1 && rv
== 0)
176 recursive_remove(const char *path
, int missing_ok
)
180 /* First try the easy case of regular file or empty directory. */
181 if (remove(path
) == 0 || (errno
== ENOENT
&& missing_ok
))
185 * If the path is too long, long_remove will use chdir to shorten it,
186 * so remember the current directory first.
188 if ((orig_cwd
= open(".", O_RDONLY
)) == -1)
191 rv
= recursive_remove_internal(path
, missing_ok
, orig_cwd
);