1 | /*-
|
---|
2 | * Copyright (c) 1990, 1993, 1994
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 4. Neither the name of the University nor the names of its contributors
|
---|
14 | * may be used to endorse or promote products derived from this software
|
---|
15 | * without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
27 | * SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #if 0
|
---|
31 | #ifndef lint
|
---|
32 | static const char copyright[] =
|
---|
33 | "@(#) Copyright (c) 1990, 1993, 1994\n\
|
---|
34 | The Regents of the University of California. All rights reserved.\n";
|
---|
35 | #endif /* not lint */
|
---|
36 |
|
---|
37 | #ifndef lint
|
---|
38 | static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94";
|
---|
39 | #endif /* not lint */
|
---|
40 | #include <sys/cdefs.h>
|
---|
41 | /*__FBSDID("$FreeBSD: src/bin/rm/rm.c,v 1.47 2004/04/06 20:06:50 markm Exp $");*/
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Header Files *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | #include "config.h"
|
---|
49 | #include <sys/stat.h>
|
---|
50 | #if !defined(_MSC_VER) && !defined(__HAIKU__)
|
---|
51 | # include <sys/param.h>
|
---|
52 | # ifndef __gnu_hurd__
|
---|
53 | # include <sys/mount.h>
|
---|
54 | # endif
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #include "err.h"
|
---|
58 | #include <errno.h>
|
---|
59 | #include <fcntl.h>
|
---|
60 | #include "fts.h"
|
---|
61 | #include <grp.h>
|
---|
62 | #include <pwd.h>
|
---|
63 | #include <stdio.h>
|
---|
64 | #include <stdlib.h>
|
---|
65 | #include <string.h>
|
---|
66 | #ifndef __HAIKU__
|
---|
67 | # include <sysexits.h>
|
---|
68 | #endif
|
---|
69 | #include <unistd.h>
|
---|
70 | #include <ctype.h>
|
---|
71 | #include "getopt.h"
|
---|
72 | #ifdef __HAIKU__
|
---|
73 | # include "haikufakes.h"
|
---|
74 | #endif
|
---|
75 | #ifdef __NetBSD__
|
---|
76 | # include <util.h>
|
---|
77 | # define fflagstostr(flags) flags_to_string(flags, "")
|
---|
78 | #endif
|
---|
79 | #ifdef KBUILD_OS_WINDOWS
|
---|
80 | # ifdef _MSC_VER
|
---|
81 | # include "mscfakes.h"
|
---|
82 | # endif
|
---|
83 | # include "nt/ntunlink.h"
|
---|
84 | /* Use the special unlink implementation to do rmdir too. */
|
---|
85 | # undef rmdir
|
---|
86 | # define rmdir(a_pszPath) birdUnlinkForced(a_pszPath)
|
---|
87 | #endif
|
---|
88 | #if defined(__OS2__) || defined(_MSC_VER)
|
---|
89 | # include <direct.h>
|
---|
90 | # include <limits.h>
|
---|
91 | #endif
|
---|
92 | #include "kmkbuiltin.h"
|
---|
93 | #include "kbuild_protection.h"
|
---|
94 | #include "k/kDefs.h" /* for K_OS */
|
---|
95 |
|
---|
96 |
|
---|
97 | /*********************************************************************************************************************************
|
---|
98 | * Defined Constants And Macros *
|
---|
99 | *********************************************************************************************************************************/
|
---|
100 | #if defined(__EMX__) || defined(KBUILD_OS_WINDOWS)
|
---|
101 | # define IS_SLASH(ch) ( (ch) == '/' || (ch) == '\\' )
|
---|
102 | # define HAVE_DOS_PATHS 1
|
---|
103 | # define DEFAULT_PROTECTION_DEPTH 1
|
---|
104 | #else
|
---|
105 | # define IS_SLASH(ch) ( (ch) == '/' )
|
---|
106 | # undef HAVE_DOS_PATHS
|
---|
107 | # define DEFAULT_PROTECTION_DEPTH 2
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | #ifdef __EMX__
|
---|
111 | #undef S_IFWHT
|
---|
112 | #undef S_ISWHT
|
---|
113 | #endif
|
---|
114 | #ifndef S_IFWHT
|
---|
115 | #define S_IFWHT 0
|
---|
116 | #define S_ISWHT(s) 0
|
---|
117 | #define undelete(s) (-1)
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | #if 1
|
---|
121 | #define CUR_LINE_H2(x) "[line " #x "]"
|
---|
122 | #define CUR_LINE_H1(x) CUR_LINE_H2(x)
|
---|
123 | #define CUR_LINE() CUR_LINE_H1(__LINE__)
|
---|
124 | #else
|
---|
125 | # define CUR_LINE()
|
---|
126 | #endif
|
---|
127 |
|
---|
128 |
|
---|
129 | /*********************************************************************************************************************************
|
---|
130 | * Structures and Typedefs *
|
---|
131 | *********************************************************************************************************************************/
|
---|
132 | typedef struct RMINSTANCE
|
---|
133 | {
|
---|
134 | PKMKBUILTINCTX pCtx;
|
---|
135 | int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
|
---|
136 | #ifdef KBUILD_OS_WINDOWS
|
---|
137 | int fUseNtDeleteFile;
|
---|
138 | #endif
|
---|
139 | uid_t uid;
|
---|
140 | KBUILDPROTECTION g_ProtData;
|
---|
141 | } RMINSTANCE;
|
---|
142 | typedef RMINSTANCE *PRMINSTANCE;
|
---|
143 |
|
---|
144 |
|
---|
145 | /*********************************************************************************************************************************
|
---|
146 | * Global Variables *
|
---|
147 | *********************************************************************************************************************************/
|
---|
148 | static struct option long_options[] =
|
---|
149 | {
|
---|
150 | { "help", no_argument, 0, 261 },
|
---|
151 | { "version", no_argument, 0, 262 },
|
---|
152 | { "disable-protection", no_argument, 0, 263 },
|
---|
153 | { "enable-protection", no_argument, 0, 264 },
|
---|
154 | { "enable-full-protection", no_argument, 0, 265 },
|
---|
155 | { "disable-full-protection", no_argument, 0, 266 },
|
---|
156 | { "protection-depth", required_argument, 0, 267 },
|
---|
157 | #ifdef KBUILD_OS_WINDOWS
|
---|
158 | { "nt-delete-file", no_argument, 0, 268 },
|
---|
159 | #endif
|
---|
160 | { 0, 0, 0, 0 },
|
---|
161 | };
|
---|
162 |
|
---|
163 |
|
---|
164 | /*********************************************************************************************************************************
|
---|
165 | * Internal Functions *
|
---|
166 | *********************************************************************************************************************************/
|
---|
167 | extern void bsd_strmode(mode_t mode, char *p); /* strmode.c */
|
---|
168 |
|
---|
169 | static int check(PRMINSTANCE, char *, char *, struct stat *);
|
---|
170 | static void checkdot(PRMINSTANCE, char **);
|
---|
171 | static int rm_file(PRMINSTANCE, char **);
|
---|
172 | static int rm_overwrite(PRMINSTANCE, char *, struct stat *);
|
---|
173 | static int rm_tree(PRMINSTANCE, char **);
|
---|
174 | static int usage(PKMKBUILTINCTX, int);
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * rm --
|
---|
180 | * This rm is different from historic rm's, but is expected to match
|
---|
181 | * POSIX 1003.2 behavior. The most visible difference is that -f
|
---|
182 | * has two specific effects now, ignore non-existent files and force
|
---|
183 | * file removal.
|
---|
184 | */
|
---|
185 | int
|
---|
186 | kmk_builtin_rm(int argc, char *argv[], char **envp, PKMKBUILTINCTX pCtx)
|
---|
187 | {
|
---|
188 | RMINSTANCE This;
|
---|
189 | int ch, rflag;
|
---|
190 |
|
---|
191 | /* Init global instance data */
|
---|
192 | This.pCtx = pCtx;
|
---|
193 | This.dflag = 0;
|
---|
194 | This.eval = 0;
|
---|
195 | This.fflag = 0;
|
---|
196 | This.iflag = 0;
|
---|
197 | This.Pflag = 0;
|
---|
198 | This.vflag = 0;
|
---|
199 | This.Wflag = 0;
|
---|
200 | This.stdin_ok = 0;
|
---|
201 | #ifdef KBUILD_OS_WINDOWS
|
---|
202 | This.fUseNtDeleteFile = 0;
|
---|
203 | #endif
|
---|
204 | This.uid = 0;
|
---|
205 | kBuildProtectionInit(&This.g_ProtData, pCtx);
|
---|
206 |
|
---|
207 | /* kmk: reset getopt and set program name. */
|
---|
208 | opterr = 1;
|
---|
209 | optarg = NULL;
|
---|
210 | optopt = 0;
|
---|
211 | optind = 0; /* init */
|
---|
212 |
|
---|
213 | rflag = 0;
|
---|
214 | while ((ch = getopt_long(argc, argv, "dfiPRvW", long_options, NULL)) != -1)
|
---|
215 | switch (ch) {
|
---|
216 | case 'd':
|
---|
217 | This.dflag = 1;
|
---|
218 | break;
|
---|
219 | case 'f':
|
---|
220 | This.fflag = 1;
|
---|
221 | This.iflag = 0;
|
---|
222 | break;
|
---|
223 | case 'i':
|
---|
224 | This.fflag = 0;
|
---|
225 | This.iflag = 1;
|
---|
226 | break;
|
---|
227 | case 'P':
|
---|
228 | This.Pflag = 1;
|
---|
229 | break;
|
---|
230 | case 'R':
|
---|
231 | #if 0
|
---|
232 | case 'r': /* Compatibility. */
|
---|
233 | #endif
|
---|
234 | rflag = 1;
|
---|
235 | break;
|
---|
236 | case 'v':
|
---|
237 | This.vflag = 1;
|
---|
238 | break;
|
---|
239 | #ifdef FTS_WHITEOUT
|
---|
240 | case 'W':
|
---|
241 | This.Wflag = 1;
|
---|
242 | break;
|
---|
243 | #endif
|
---|
244 | case 261:
|
---|
245 | kBuildProtectionTerm(&This.g_ProtData);
|
---|
246 | usage(pCtx, 0);
|
---|
247 | return 0;
|
---|
248 | case 262:
|
---|
249 | kBuildProtectionTerm(&This.g_ProtData);
|
---|
250 | return kbuild_version(argv[0]);
|
---|
251 | case 263:
|
---|
252 | kBuildProtectionDisable(&This.g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
|
---|
253 | break;
|
---|
254 | case 264:
|
---|
255 | kBuildProtectionEnable(&This.g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
|
---|
256 | break;
|
---|
257 | case 265:
|
---|
258 | kBuildProtectionEnable(&This.g_ProtData, KBUILDPROTECTIONTYPE_FULL);
|
---|
259 | break;
|
---|
260 | case 266:
|
---|
261 | kBuildProtectionDisable(&This.g_ProtData, KBUILDPROTECTIONTYPE_FULL);
|
---|
262 | break;
|
---|
263 | case 267:
|
---|
264 | if (kBuildProtectionSetDepth(&This.g_ProtData, optarg)) {
|
---|
265 | kBuildProtectionTerm(&This.g_ProtData);
|
---|
266 | return 1;
|
---|
267 | }
|
---|
268 | break;
|
---|
269 | #ifdef KBUILD_OS_WINDOWS
|
---|
270 | case 268:
|
---|
271 | This.fUseNtDeleteFile = 1;
|
---|
272 | break;
|
---|
273 | #endif
|
---|
274 | case '?':
|
---|
275 | default:
|
---|
276 | kBuildProtectionTerm(&This.g_ProtData);
|
---|
277 | return usage(pCtx, 1);
|
---|
278 | }
|
---|
279 | argc -= optind;
|
---|
280 | argv += optind;
|
---|
281 |
|
---|
282 | if (argc < 1) {
|
---|
283 | kBuildProtectionTerm(&This.g_ProtData);
|
---|
284 | if (This.fflag)
|
---|
285 | return (0);
|
---|
286 | return usage(pCtx, 1);
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (!kBuildProtectionScanEnv(&This.g_ProtData, envp, "KMK_RM_")) {
|
---|
290 | checkdot(&This, argv);
|
---|
291 | This.uid = geteuid();
|
---|
292 |
|
---|
293 | if (*argv) {
|
---|
294 | This.stdin_ok = isatty(STDIN_FILENO);
|
---|
295 | if (rflag)
|
---|
296 | This.eval |= rm_tree(&This, argv);
|
---|
297 | else
|
---|
298 | This.eval |= rm_file(&This, argv);
|
---|
299 | }
|
---|
300 | } else {
|
---|
301 | This.eval = 1;
|
---|
302 | }
|
---|
303 |
|
---|
304 | kBuildProtectionTerm(&This.g_ProtData);
|
---|
305 | return This.eval;
|
---|
306 | }
|
---|
307 |
|
---|
308 | #ifdef KMK_BUILTIN_STANDALONE
|
---|
309 | int main(int argc, char **argv, char **envp)
|
---|
310 | {
|
---|
311 | KMKBUILTINCTX Ctx = { "kmk_rm", NULL };
|
---|
312 | return kmk_builtin_rm(argc, argv, envp, &Ctx);
|
---|
313 | }
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | static int
|
---|
317 | rm_tree(PRMINSTANCE pThis, char **argv)
|
---|
318 | {
|
---|
319 | FTS *fts;
|
---|
320 | FTSENT *p;
|
---|
321 | int needstat;
|
---|
322 | int flags;
|
---|
323 | int rval;
|
---|
324 |
|
---|
325 | /*
|
---|
326 | * Check up front before anything is deleted. This will not catch
|
---|
327 | * everything, but we'll check the individual items later.
|
---|
328 | */
|
---|
329 | int i;
|
---|
330 | for (i = 0; argv[i]; i++) {
|
---|
331 | if (kBuildProtectionEnforce(&pThis->g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE, argv[i])) {
|
---|
332 | return 1;
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Remove a file hierarchy. If forcing removal (-f), or interactive
|
---|
338 | * (-i) or can't ask anyway (stdin_ok), don't stat the file.
|
---|
339 | */
|
---|
340 | needstat = !pThis->uid || (!pThis->fflag && !pThis->iflag && pThis->stdin_ok);
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * If the -i option is specified, the user can skip on the pre-order
|
---|
344 | * visit. The fts_number field flags skipped directories.
|
---|
345 | */
|
---|
346 | #define SKIPPED 1
|
---|
347 |
|
---|
348 | flags = FTS_PHYSICAL;
|
---|
349 | if (!needstat)
|
---|
350 | flags |= FTS_NOSTAT;
|
---|
351 | #ifdef FTS_WHITEOUT
|
---|
352 | if (pThis->Wflag)
|
---|
353 | flags |= FTS_WHITEOUT;
|
---|
354 | #endif
|
---|
355 | if (!(fts = fts_open(argv, flags, NULL))) {
|
---|
356 | return err(pThis->pCtx, 1, "fts_open");
|
---|
357 | }
|
---|
358 | while ((p = fts_read(fts)) != NULL) {
|
---|
359 | const char *operation = "chflags";
|
---|
360 | switch (p->fts_info) {
|
---|
361 | case FTS_DNR:
|
---|
362 | if (!pThis->fflag || p->fts_errno != ENOENT)
|
---|
363 | pThis->eval = errx(pThis->pCtx, 1, "fts: %s: %s" CUR_LINE() "\n",
|
---|
364 | p->fts_path, strerror(p->fts_errno));
|
---|
365 | continue;
|
---|
366 | case FTS_ERR:
|
---|
367 | fts_close(fts);
|
---|
368 | return errx(pThis->pCtx, 1, "fts: %s: %s " CUR_LINE(), p->fts_path, strerror(p->fts_errno));
|
---|
369 | case FTS_NS:
|
---|
370 | /*
|
---|
371 | * Assume that since fts_read() couldn't stat the
|
---|
372 | * file, it can't be unlinked.
|
---|
373 | */
|
---|
374 | if (!needstat)
|
---|
375 | break;
|
---|
376 | if (!pThis->fflag || p->fts_errno != ENOENT)
|
---|
377 | pThis->eval = errx(pThis->pCtx, 1, "fts: %s: %s " CUR_LINE() "\n",
|
---|
378 | p->fts_path, strerror(p->fts_errno));
|
---|
379 | continue;
|
---|
380 | case FTS_D:
|
---|
381 | /* Pre-order: give user chance to skip. */
|
---|
382 | if (!pThis->fflag && !check(pThis, p->fts_path, p->fts_accpath, p->fts_statp)) {
|
---|
383 | (void)fts_set(fts, p, FTS_SKIP);
|
---|
384 | p->fts_number = SKIPPED;
|
---|
385 | }
|
---|
386 | #ifdef UF_APPEND
|
---|
387 | else if (!pThis->uid &&
|
---|
388 | (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
|
---|
389 | !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
|
---|
390 | chflags(p->fts_accpath,
|
---|
391 | p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
|
---|
392 | goto err;
|
---|
393 | #endif
|
---|
394 | continue;
|
---|
395 | case FTS_DP:
|
---|
396 | /* Post-order: see if user skipped. */
|
---|
397 | if (p->fts_number == SKIPPED)
|
---|
398 | continue;
|
---|
399 | break;
|
---|
400 | default:
|
---|
401 | if (!pThis->fflag && !check(pThis, p->fts_path, p->fts_accpath, p->fts_statp))
|
---|
402 | continue;
|
---|
403 | }
|
---|
404 |
|
---|
405 | /*
|
---|
406 | * Protect against deleting root files and directories.
|
---|
407 | */
|
---|
408 | if (kBuildProtectionEnforce(&pThis->g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE, p->fts_accpath)) {
|
---|
409 | fts_close(fts);
|
---|
410 | return 1;
|
---|
411 | }
|
---|
412 |
|
---|
413 | rval = 0;
|
---|
414 | #ifdef UF_APPEND
|
---|
415 | if (!pThis->uid &&
|
---|
416 | (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
|
---|
417 | !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
|
---|
418 | rval = chflags(p->fts_accpath,
|
---|
419 | p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
|
---|
420 | #endif
|
---|
421 | if (rval == 0) {
|
---|
422 | /*
|
---|
423 | * If we can't read or search the directory, may still be
|
---|
424 | * able to remove it. Don't print out the un{read,search}able
|
---|
425 | * message unless the remove fails.
|
---|
426 | */
|
---|
427 | switch (p->fts_info) {
|
---|
428 | case FTS_DP:
|
---|
429 | case FTS_DNR:
|
---|
430 | #ifdef KBUILD_OS_WINDOWS
|
---|
431 | if (p->fts_parent->fts_dirfd != NT_FTS_INVALID_HANDLE_VALUE) {
|
---|
432 | rval = birdUnlinkForcedEx(p->fts_parent->fts_dirfd, p->fts_name);
|
---|
433 | } else {
|
---|
434 | rval = birdUnlinkForced(p->fts_accpath);
|
---|
435 | }
|
---|
436 | #else
|
---|
437 | rval = rmdir(p->fts_accpath);
|
---|
438 | #endif
|
---|
439 | if (rval == 0 || (pThis->fflag && errno == ENOENT)) {
|
---|
440 | if (rval == 0 && pThis->vflag)
|
---|
441 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", p->fts_path);
|
---|
442 | #if defined(KMK) && defined(KBUILD_OS_WINDOWS)
|
---|
443 | if (rval == 0) {
|
---|
444 | extern int dir_cache_deleted_directory(const char *pszDir);
|
---|
445 | dir_cache_deleted_directory(p->fts_accpath);
|
---|
446 | }
|
---|
447 | #endif
|
---|
448 | continue;
|
---|
449 | }
|
---|
450 | operation = "rmdir";
|
---|
451 | break;
|
---|
452 |
|
---|
453 | #ifdef FTS_W
|
---|
454 | case FTS_W:
|
---|
455 | rval = undelete(p->fts_accpath);
|
---|
456 | if (rval == 0 && (pThis->fflag && errno == ENOENT)) {
|
---|
457 | if (vflag)
|
---|
458 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", p->fts_path);
|
---|
459 | continue;
|
---|
460 | }
|
---|
461 | operation = "undelete";
|
---|
462 | break;
|
---|
463 | #endif
|
---|
464 |
|
---|
465 | case FTS_NS:
|
---|
466 | /*
|
---|
467 | * Assume that since fts_read() couldn't stat
|
---|
468 | * the file, it can't be unlinked.
|
---|
469 | */
|
---|
470 | if (pThis->fflag)
|
---|
471 | continue;
|
---|
472 | /* FALLTHROUGH */
|
---|
473 | default:
|
---|
474 | if (pThis->Pflag)
|
---|
475 | if (!rm_overwrite(pThis, p->fts_accpath, NULL))
|
---|
476 | continue;
|
---|
477 | #ifdef KBUILD_OS_WINDOWS
|
---|
478 | if (p->fts_parent->fts_dirfd != NT_FTS_INVALID_HANDLE_VALUE) {
|
---|
479 | rval = birdUnlinkForcedFastEx(p->fts_parent->fts_dirfd, p->fts_name);
|
---|
480 | } else {
|
---|
481 | rval = birdUnlinkForcedFast(p->fts_accpath);
|
---|
482 | }
|
---|
483 | #else
|
---|
484 | rval = unlink(p->fts_accpath);
|
---|
485 | #endif
|
---|
486 |
|
---|
487 | if (rval == 0 || (pThis->fflag && errno == ENOENT)) {
|
---|
488 | if (rval == 0 && pThis->vflag)
|
---|
489 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", p->fts_path);
|
---|
490 | continue;
|
---|
491 | }
|
---|
492 | operation = "unlink";
|
---|
493 | break;
|
---|
494 | }
|
---|
495 | }
|
---|
496 | #ifdef UF_APPEND
|
---|
497 | err:
|
---|
498 | #endif
|
---|
499 | pThis->eval = errx(pThis->pCtx, 1, "%s: %s failed: %s " CUR_LINE() "\n", p->fts_path, operation, strerror(errno));
|
---|
500 | }
|
---|
501 | if (errno) {
|
---|
502 | pThis->eval = errx(pThis->pCtx, 1, "fts_read: %s " CUR_LINE() "\n", strerror(errno));
|
---|
503 | }
|
---|
504 | fts_close(fts);
|
---|
505 | return pThis->eval;
|
---|
506 | }
|
---|
507 |
|
---|
508 | static int
|
---|
509 | rm_file(PRMINSTANCE pThis, char **argv)
|
---|
510 | {
|
---|
511 | struct stat sb;
|
---|
512 | int rval;
|
---|
513 | char *f;
|
---|
514 |
|
---|
515 | /*
|
---|
516 | * Check up front before anything is deleted.
|
---|
517 | */
|
---|
518 | int i;
|
---|
519 | for (i = 0; argv[i]; i++) {
|
---|
520 | if (kBuildProtectionEnforce(&pThis->g_ProtData, KBUILDPROTECTIONTYPE_FULL, argv[i]))
|
---|
521 | return 1;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /*
|
---|
525 | * Remove a file. POSIX 1003.2 states that, by default, attempting
|
---|
526 | * to remove a directory is an error, so must always stat the file.
|
---|
527 | */
|
---|
528 | while ((f = *argv++) != NULL) {
|
---|
529 | const char *operation = "?";
|
---|
530 | /* Assume if can't stat the file, can't unlink it. */
|
---|
531 | if (lstat(f, &sb)) {
|
---|
532 | #ifdef FTS_WHITEOUT
|
---|
533 | if (pThis->Wflag) {
|
---|
534 | sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
|
---|
535 | } else {
|
---|
536 | #else
|
---|
537 | {
|
---|
538 | #endif
|
---|
539 | if (!pThis->fflag || errno != ENOENT)
|
---|
540 | pThis->eval = errx(pThis->pCtx, 1, "%s: lstat failed: %s " CUR_LINE() "\n",
|
---|
541 | f, strerror(errno));
|
---|
542 | continue;
|
---|
543 | }
|
---|
544 | #ifdef FTS_WHITEOUT
|
---|
545 | } else if (pThis->Wflag) {
|
---|
546 | errx(pThis->pCtx, "%s: %s\n", f, strerror(EEXIST));
|
---|
547 | pThis->eval = 1;
|
---|
548 | continue;
|
---|
549 | #endif
|
---|
550 | }
|
---|
551 |
|
---|
552 | if (S_ISDIR(sb.st_mode) && !pThis->dflag) {
|
---|
553 | pThis->eval = errx(pThis->pCtx, 1, "%s: is a directory\n", f);
|
---|
554 | continue;
|
---|
555 | }
|
---|
556 | if (!pThis->fflag && !S_ISWHT(sb.st_mode) && !check(pThis, f, f, &sb))
|
---|
557 | continue;
|
---|
558 | rval = 0;
|
---|
559 | #ifdef UF_APPEND
|
---|
560 | if (!uid &&
|
---|
561 | (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
|
---|
562 | !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
|
---|
563 | rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
|
---|
564 | #endif
|
---|
565 | if (rval == 0) {
|
---|
566 | if (S_ISWHT(sb.st_mode)) {
|
---|
567 | rval = undelete(f);
|
---|
568 | operation = "undelete";
|
---|
569 | } else if (S_ISDIR(sb.st_mode)) {
|
---|
570 | rval = rmdir(f);
|
---|
571 | operation = "rmdir";
|
---|
572 | } else {
|
---|
573 | if (pThis->Pflag)
|
---|
574 | if (!rm_overwrite(pThis, f, &sb))
|
---|
575 | continue;
|
---|
576 | #ifndef KBUILD_OS_WINDOWS
|
---|
577 | rval = unlink(f);
|
---|
578 | operation = "unlink";
|
---|
579 | #else
|
---|
580 | if (pThis->fUseNtDeleteFile) {
|
---|
581 | rval = birdUnlinkForcedFast(f);
|
---|
582 | operation = "NtDeleteFile";
|
---|
583 | } else {
|
---|
584 | rval = birdUnlinkForced(f);
|
---|
585 | operation = "unlink";
|
---|
586 | }
|
---|
587 | #endif
|
---|
588 | }
|
---|
589 | }
|
---|
590 | if (rval && (!pThis->fflag || errno != ENOENT))
|
---|
591 | pThis->eval = errx(pThis->pCtx, 1, "%s: %s failed: %s" CUR_LINE() "\n", f, operation, strerror(errno));
|
---|
592 | if (pThis->vflag && rval == 0)
|
---|
593 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", f);
|
---|
594 | }
|
---|
595 | return pThis->eval;
|
---|
596 | }
|
---|
597 |
|
---|
598 | /*
|
---|
599 | * rm_overwrite --
|
---|
600 | * Overwrite the file 3 times with varying bit patterns.
|
---|
601 | *
|
---|
602 | * XXX
|
---|
603 | * This is a cheap way to *really* delete files. Note that only regular
|
---|
604 | * files are deleted, directories (and therefore names) will remain.
|
---|
605 | * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
|
---|
606 | * System V file system). In a logging file system, you'll have to have
|
---|
607 | * kernel support.
|
---|
608 | */
|
---|
609 | static int
|
---|
610 | rm_overwrite(PRMINSTANCE pThis, char *file, struct stat *sbp)
|
---|
611 | {
|
---|
612 | struct stat sb;
|
---|
613 | #ifdef HAVE_FSTATFS
|
---|
614 | struct statfs fsb;
|
---|
615 | #endif
|
---|
616 | off_t len;
|
---|
617 | int bsize, fd, wlen;
|
---|
618 | char *buf = NULL;
|
---|
619 | const char *operation = "lstat";
|
---|
620 | int error;
|
---|
621 |
|
---|
622 | fd = -1;
|
---|
623 | if (sbp == NULL) {
|
---|
624 | if (lstat(file, &sb))
|
---|
625 | goto err;
|
---|
626 | sbp = &sb;
|
---|
627 | }
|
---|
628 | if (!S_ISREG(sbp->st_mode))
|
---|
629 | return (1);
|
---|
630 | operation = "open";
|
---|
631 | if ((fd = open(file, O_WRONLY | KMK_OPEN_NO_INHERIT, 0)) == -1)
|
---|
632 | goto err;
|
---|
633 | #ifdef HAVE_FSTATFS
|
---|
634 | if (fstatfs(fd, &fsb) == -1)
|
---|
635 | goto err;
|
---|
636 | bsize = MAX(fsb.f_iosize, 1024);
|
---|
637 | #elif defined(HAVE_ST_BLKSIZE)
|
---|
638 | bsize = MAX(sb.st_blksize, 1024);
|
---|
639 | #else
|
---|
640 | bsize = 1024;
|
---|
641 | #endif
|
---|
642 | if ((buf = malloc(bsize)) == NULL) {
|
---|
643 | err(pThis->pCtx, 1, "%s: malloc", file);
|
---|
644 | close(fd);
|
---|
645 | return 1;
|
---|
646 | }
|
---|
647 |
|
---|
648 | #define PASS(byte) { \
|
---|
649 | operation = "write"; \
|
---|
650 | memset(buf, byte, bsize); \
|
---|
651 | for (len = sbp->st_size; len > 0; len -= wlen) { \
|
---|
652 | wlen = len < bsize ? len : bsize; \
|
---|
653 | if (write(fd, buf, wlen) != wlen) \
|
---|
654 | goto err; \
|
---|
655 | } \
|
---|
656 | }
|
---|
657 | PASS(0xff);
|
---|
658 | operation = "fsync/lseek";
|
---|
659 | if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
|
---|
660 | goto err;
|
---|
661 | PASS(0x00);
|
---|
662 | operation = "fsync/lseek";
|
---|
663 | if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
|
---|
664 | goto err;
|
---|
665 | PASS(0xff);
|
---|
666 | if (!fsync(fd) && !close(fd)) {
|
---|
667 | free(buf);
|
---|
668 | return (1);
|
---|
669 | }
|
---|
670 | operation = "fsync/close";
|
---|
671 |
|
---|
672 | err: pThis->eval = 1;
|
---|
673 | error = errno;
|
---|
674 | if (buf)
|
---|
675 | free(buf);
|
---|
676 | if (fd != -1)
|
---|
677 | close(fd);
|
---|
678 | errx(pThis->pCtx, 1, "%s: %s: %s: %s" CUR_LINE() "\n", operation, pThis->pCtx->pszProgName, file, strerror(error));
|
---|
679 | return (0);
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | static int
|
---|
684 | check(PRMINSTANCE pThis, char *path, char *name, struct stat *sp)
|
---|
685 | {
|
---|
686 | int ch, first;
|
---|
687 | char modep[15], *flagsp;
|
---|
688 |
|
---|
689 | /* Check -i first. */
|
---|
690 | if (pThis->iflag)
|
---|
691 | (void)fprintf(stderr, "%s: remove %s? ", pThis->pCtx->pszProgName, path);
|
---|
692 | else {
|
---|
693 | /*
|
---|
694 | * If it's not a symbolic link and it's unwritable and we're
|
---|
695 | * talking to a terminal, ask. Symbolic links are excluded
|
---|
696 | * because their permissions are meaningless. Check stdin_ok
|
---|
697 | * first because we may not have stat'ed the file.
|
---|
698 | * Also skip this check if the -P option was specified because
|
---|
699 | * we will not be able to overwrite file contents and will
|
---|
700 | * barf later.
|
---|
701 | */
|
---|
702 | if (!pThis->stdin_ok || S_ISLNK(sp->st_mode) || pThis->Pflag ||
|
---|
703 | (!access(name, W_OK) &&
|
---|
704 | #ifdef SF_APPEND
|
---|
705 | !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
|
---|
706 | (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !pThis->uid))
|
---|
707 | #else
|
---|
708 | 1)
|
---|
709 | #endif
|
---|
710 | )
|
---|
711 | return (1);
|
---|
712 | bsd_strmode(sp->st_mode, modep);
|
---|
713 | #if defined(SF_APPEND) && K_OS != K_OS_GNU_KFBSD && K_OS != K_OS_GNU_HURD
|
---|
714 | if ((flagsp = fflagstostr(sp->st_flags)) == NULL) {
|
---|
715 | err(pThis->pCtx, 1, "fflagstostr");
|
---|
716 | flagsp = "<bad-fflagstostr>";
|
---|
717 | }
|
---|
718 | (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
|
---|
719 | modep + 1, modep[9] == ' ' ? "" : " ",
|
---|
720 | user_from_uid(sp->st_uid, 0),
|
---|
721 | group_from_gid(sp->st_gid, 0),
|
---|
722 | *flagsp ? flagsp : "", *flagsp ? " " : "",
|
---|
723 | path);
|
---|
724 | free(flagsp);
|
---|
725 | #else
|
---|
726 | (void)flagsp;
|
---|
727 | (void)fprintf(stderr, "override %s%s %d/%d for %s? ",
|
---|
728 | modep + 1, modep[9] == ' ' ? "" : " ",
|
---|
729 | sp->st_uid, sp->st_gid, path);
|
---|
730 | #endif
|
---|
731 | }
|
---|
732 | (void)fflush(stderr);
|
---|
733 |
|
---|
734 | first = ch = getchar();
|
---|
735 | while (ch != '\n' && ch != EOF)
|
---|
736 | ch = getchar();
|
---|
737 | return (first == 'y' || first == 'Y');
|
---|
738 | }
|
---|
739 |
|
---|
740 | #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
|
---|
741 | static void
|
---|
742 | checkdot(PRMINSTANCE pThis, char **argv)
|
---|
743 | {
|
---|
744 | char *p, **save, **t;
|
---|
745 | int complained;
|
---|
746 |
|
---|
747 | complained = 0;
|
---|
748 | for (t = argv; *t;) {
|
---|
749 | #ifdef HAVE_DOS_PATHS
|
---|
750 | const char *tmp = p = *t;
|
---|
751 | while (*tmp) {
|
---|
752 | switch (*tmp) {
|
---|
753 | case '/':
|
---|
754 | case '\\':
|
---|
755 | case ':':
|
---|
756 | p = (char *)tmp + 1;
|
---|
757 | break;
|
---|
758 | }
|
---|
759 | tmp++;
|
---|
760 | }
|
---|
761 | #else
|
---|
762 | if ((p = strrchr(*t, '/')) != NULL)
|
---|
763 | ++p;
|
---|
764 | else
|
---|
765 | p = *t;
|
---|
766 | #endif
|
---|
767 | if (ISDOT(p)) {
|
---|
768 | if (!complained++)
|
---|
769 | warnx(pThis->pCtx, "\".\" and \"..\" may not be removed\n");
|
---|
770 | pThis->eval = 1;
|
---|
771 | for (save = t; (t[0] = t[1]) != NULL; ++t)
|
---|
772 | continue;
|
---|
773 | t = save;
|
---|
774 | } else
|
---|
775 | ++t;
|
---|
776 | }
|
---|
777 | }
|
---|
778 |
|
---|
779 | static int
|
---|
780 | usage(PKMKBUILTINCTX pCtx, int fIsErr)
|
---|
781 | {
|
---|
782 | kmk_builtin_ctx_printf(pCtx, fIsErr,
|
---|
783 | "usage: %s [options] file ...\n"
|
---|
784 | " or: %s --help\n"
|
---|
785 | " or: %s --version\n"
|
---|
786 | "\n"
|
---|
787 | "Options:\n"
|
---|
788 | " -f\n"
|
---|
789 | " Attempt to remove files without prompting, regardless of the file\n"
|
---|
790 | " permission. Ignore non-existing files. Overrides previous -i's.\n"
|
---|
791 | " -i\n"
|
---|
792 | " Prompt for each file. Always.\n"
|
---|
793 | " -d\n"
|
---|
794 | " Attempt to remove directories as well as other kinds of files.\n"
|
---|
795 | " -P\n"
|
---|
796 | " Overwrite regular files before deleting; three passes: ff,0,ff\n"
|
---|
797 | " -R\n"
|
---|
798 | " Attempt to remove the file hierachy rooted in each file argument.\n"
|
---|
799 | " This option implies -d and file protection.\n"
|
---|
800 | " -v\n"
|
---|
801 | " Be verbose, show files as they are removed.\n"
|
---|
802 | " -W\n"
|
---|
803 | " Undelete without files.\n"
|
---|
804 | " --disable-protection\n"
|
---|
805 | " Will disable the protection file protection applied with -R.\n"
|
---|
806 | " --enable-protection\n"
|
---|
807 | " Will enable the protection file protection applied with -R.\n"
|
---|
808 | " --enable-full-protection\n"
|
---|
809 | " Will enable the protection file protection for all operations.\n"
|
---|
810 | " --disable-full-protection\n"
|
---|
811 | " Will disable the protection file protection for all operations.\n"
|
---|
812 | " --protection-depth\n"
|
---|
813 | " Number or path indicating the file protection depth. Default: %d\n"
|
---|
814 | "\n"
|
---|
815 | "Environment:\n"
|
---|
816 | " KMK_RM_DISABLE_PROTECTION\n"
|
---|
817 | " Same as --disable-protection. Overrides command line.\n"
|
---|
818 | " KMK_RM_ENABLE_PROTECTION\n"
|
---|
819 | " Same as --enable-protection. Overrides everyone else.\n"
|
---|
820 | " KMK_RM_ENABLE_FULL_PROTECTION\n"
|
---|
821 | " Same as --enable-full-protection. Overrides everyone else.\n"
|
---|
822 | " KMK_RM_DISABLE_FULL_PROTECTION\n"
|
---|
823 | " Same as --disable-full-protection. Overrides command line.\n"
|
---|
824 | " KMK_RM_PROTECTION_DEPTH\n"
|
---|
825 | " Same as --protection-depth. Overrides command line.\n"
|
---|
826 | "\n"
|
---|
827 | "The file protection of the top %d layers of the file hierarchy is there\n"
|
---|
828 | "to try prevent makefiles from doing bad things to your system. This\n"
|
---|
829 | "protection is not bulletproof, but should help prevent you from shooting\n"
|
---|
830 | "yourself in the foot.\n"
|
---|
831 | ,
|
---|
832 | pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName,
|
---|
833 | kBuildProtectionDefaultDepth(), kBuildProtectionDefaultDepth());
|
---|
834 | return EX_USAGE;
|
---|
835 | }
|
---|
836 |
|
---|