Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / lib / remove.c
blob9c08da7b1bb93249fcd95ef222d97d91e2559412
1 /* $NetBSD: remove.c,v 1.3 2009/08/02 17:56:45 joerg Exp $ */
3 /*-
4 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
29 * SUCH DAMAGE.
32 #if HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include <nbcompat.h>
38 #if HAVE_SYS_CDEFS_H
39 #include <sys/cdefs.h>
40 #endif
42 __RCSID("$NetBSD: remove.c,v 1.3 2009/08/02 17:56:45 joerg Exp $");
44 #if HAVE_DIRENT_H
45 #include <dirent.h>
46 #endif
47 #if HAVE_ERR_H
48 #include <err.h>
49 #endif
50 #include <errno.h>
51 #if HAVE_FCNTL_H
52 #include <fcntl.h>
53 #endif
54 #include <limits.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
60 #include "lib.h"
62 static int
63 safe_fchdir(int cwd)
65 int tmp_errno, rv;
67 tmp_errno = errno;
68 rv = fchdir(cwd);
69 errno = tmp_errno;
71 return rv;
74 static int
75 long_remove(const char **path_ptr, int missing_ok, int *did_chdir)
77 char tmp_path[PATH_MAX + 1];
78 const char *path;
79 size_t i, len;
80 int rv;
82 path = *path_ptr;
83 len = strlen(path);
84 *did_chdir = 0;
86 while (len >= PATH_MAX) {
87 for (i = PATH_MAX - 1; i > 0; --i) {
88 if (path[i] == '/')
89 break;
91 if (i == 0) {
92 errno = ENAMETOOLONG;
93 return -1; /* Assumes PATH_MAX > NAME_MAX */
95 memcpy(tmp_path, path, i);
96 tmp_path[i] = '\0';
97 if (chdir(tmp_path))
98 return -1;
99 *did_chdir = 1;
100 path += i + 1;
101 len -= i + 1;
104 if (remove(path) == 0 || (errno == ENOENT && missing_ok))
105 rv = 0;
106 else
107 rv = -1;
109 *path_ptr = path;
111 return rv;
114 static int
115 recursive_remove_internal(const char *path, int missing_ok, int cwd)
117 DIR *dir;
118 struct dirent *de;
119 const char *sub_path;
120 char *subdir;
121 int did_chdir, rv;
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.
128 sub_path = path;
129 if (long_remove(&sub_path, missing_ok, &did_chdir) == 0)
130 rv = 0;
131 else if (errno != ENOTEMPTY) /* Other errors are terminal. */
132 rv = -1;
133 else
134 rv = 1;
136 if (rv != 1) {
137 if (did_chdir && safe_fchdir(cwd) == -1 && rv == 0)
138 rv = -1;
139 return rv;
142 if ((dir = opendir(sub_path)) == NULL) {
143 if (errno == EMFILE)
144 warn("opendir failed");
145 return -1;
148 if (did_chdir && fchdir(cwd) == -1)
149 return -1;
151 rv = 0;
153 while ((de = readdir(dir)) != NULL) {
154 if (strcmp(de->d_name, ".") == 0)
155 continue;
156 if (strcmp(de->d_name, "..") == 0)
157 continue;
158 subdir = xasprintf("%s/%s", path, de->d_name);
159 rv = recursive_remove_internal(subdir, 1, cwd);
160 free(subdir);
163 closedir(dir);
165 safe_fchdir(cwd);
167 rv |= long_remove(&path, missing_ok, &did_chdir);
169 if (did_chdir && safe_fchdir(cwd) == -1 && rv == 0)
170 rv = -1;
172 return rv;
176 recursive_remove(const char *path, int missing_ok)
178 int orig_cwd, rv;
180 /* First try the easy case of regular file or empty directory. */
181 if (remove(path) == 0 || (errno == ENOENT && missing_ok))
182 return 0;
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)
189 return -1;
191 rv = recursive_remove_internal(path, missing_ok, orig_cwd);
193 close(orig_cwd);
194 return rv;