1/*	$NetBSD: remove.c,v 1.3 2021/04/10 19:49:59 nia Exp $	*/
2
3/*-
4 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
17 *
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.
30 */
31
32#if HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#include <nbcompat.h>
37
38#if HAVE_SYS_CDEFS_H
39#include <sys/cdefs.h>
40#endif
41
42__RCSID("$NetBSD: remove.c,v 1.3 2021/04/10 19:49:59 nia Exp $");
43
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>
59
60#include "lib.h"
61
62static int
63safe_fchdir(int cwd)
64{
65	int tmp_errno, rv;
66
67	tmp_errno = errno;
68	rv = fchdir(cwd);
69	errno = tmp_errno;
70
71	return rv;
72}
73
74static int
75long_remove(const char **path_ptr, int missing_ok, int *did_chdir)
76{
77	char tmp_path[PATH_MAX + 1];
78	const char *path;
79	size_t i, len;
80	int rv;
81
82	path = *path_ptr;
83	len = strlen(path);
84	*did_chdir = 0;
85
86	while (len >= PATH_MAX) {
87		for (i = PATH_MAX - 1; i > 0; --i) {
88			if (path[i] == '/')
89				break;
90		}
91		if (i == 0) {
92			errno = ENAMETOOLONG;
93			return -1; /* Assumes PATH_MAX > NAME_MAX */
94		}
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;
102	}
103
104	if (remove(path) == 0 || (errno == ENOENT && missing_ok))
105		rv = 0;
106	else
107		rv = -1;
108
109	*path_ptr = path;
110
111	return rv;
112}
113
114static int
115recursive_remove_internal(const char *path, int missing_ok, int cwd)
116{
117	DIR *dir;
118	struct dirent *de;
119	const char *sub_path;
120	char *subdir;
121	int did_chdir, rv;
122
123	/*
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.
127	 */
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;
135
136	if (rv != 1) {
137		if (did_chdir && safe_fchdir(cwd) == -1 && rv == 0)
138			rv = -1;
139		return rv;
140	}
141
142	if ((dir = opendir(sub_path)) == NULL) {
143		if (errno == EMFILE)
144			warn("opendir failed");
145		return -1;
146	}
147
148	if (did_chdir && fchdir(cwd) == -1)
149		return -1;
150
151	rv = 0;
152
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);
161	}
162
163	closedir(dir);
164
165	safe_fchdir(cwd);
166
167	rv |= long_remove(&path, missing_ok, &did_chdir);
168
169	if (did_chdir && safe_fchdir(cwd) == -1 && rv == 0)
170		rv = -1;
171
172	return rv;
173}
174
175int
176recursive_remove(const char *path, int missing_ok)
177{
178	int orig_cwd, rv;
179
180	/* First try the easy case of regular file or empty directory. */
181	if (remove(path) == 0 || (errno == ENOENT && missing_ok))
182		return 0;
183
184	/*
185	 * If the path is too long, long_remove will use chdir to shorten it,
186	 * so remember the current directory first.
187	 */
188	if ((orig_cwd = open(".", O_RDONLY)) == -1)
189		return -1;
190
191	rv = recursive_remove_internal(path, missing_ok, orig_cwd);
192
193	close(orig_cwd);
194	return rv;
195}
196