1 /* remove.c -- core functions for removing files and directories
2 Copyright (C) 88, 90, 91, 1994-2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Extracted from rm.c and librarified, then rewritten by Jim Meyering. */
22 #include <sys/types.h>
27 #include "cycle-check.h"
31 #include "euidaccess.h"
32 #include "euidaccess-stat.h"
33 #include "file-type.h"
41 #include "root-dev-ino.h"
42 #include "unlinkdir.h"
45 /* Avoid shadowing warnings because these are functions declared
46 in dirname.h as well as locals used below. */
47 #define dir_name rm_dir_name
48 #define dir_len rm_dir_len
50 #define obstack_chunk_alloc malloc
51 #define obstack_chunk_free free
53 /* This is the maximum number of consecutive readdir/unlink calls that
54 can be made (with no intervening rewinddir or closedir/opendir)
55 before triggering a bug that makes readdir return NULL even though
56 some directory entries have not been processed. The bug afflicts
57 SunOS's readdir when applied to ufs file systems and Darwin 6.5's
58 (and OSX v.10.3.8's) HFS+. This maximum is conservative in that
59 demonstrating the problem seems to require a directory containing
60 at least 254 deletable entries (which doesn't count . and ..), so
61 we could conceivably increase the maximum value to 254. */
64 CONSECUTIVE_READDIR_UNLINK_THRESHOLD = 200
73 typedef enum Ternary Ternary;
75 /* The prompt function may be called twice for a given directory.
76 The first time, we ask whether to descend into it, and the
77 second time, we ask whether to remove it. */
80 PA_DESCEND_INTO_DIR = 2,
84 /* Initial capacity of per-directory hash table of entries that have
85 been processed but not been deleted. */
86 enum { HT_UNREMOVABLE_INITIAL_CAPACITY = 13 };
88 /* An entry in the active directory stack.
89 Each entry corresponds to an `active' directory. */
92 /* For a given active directory, this is the set of names of
93 entries in that directory that could/should not be removed.
94 For example, `.' and `..', as well as files/dirs for which
95 unlink/rmdir failed e.g., due to access restrictions. */
96 Hash_table *unremovable;
98 /* Record the status for a given active directory; we need to know
99 whether an entry was not removed, either because of an error or
100 because the user declined. */
101 enum RM_status status;
103 /* The directory's dev/ino. Used to ensure that a malicious user does
104 not replace a directory we're about to process with a symlink to
105 some other directory. */
106 struct dev_ino dev_ino;
109 extern char *program_name;
111 struct dirstack_state
113 /* The name of the directory (starting with and relative to a command
114 line argument) being processed. When a subdirectory is entered, a new
115 component is appended (pushed). Remove (pop) the top component
116 upon chdir'ing out of a directory. This is used to form the full
117 name of the current directory or a file therein, when necessary. */
118 struct obstack dir_stack;
120 /* Stack of lengths of directory names (including trailing slash)
121 appended to dir_stack. We have to have a separate stack of lengths
122 (rather than just popping back to previous slash) because the first
123 element pushed onto the dir stack may contain slashes. */
124 struct obstack len_stack;
126 /* Stack of active directory entries.
127 The first `active' directory is the initial working directory.
128 Additional active dirs are pushed onto the stack as we `chdir'
129 into each directory to be processed. When finished with the
130 hierarchy under a directory, pop the active dir stack. */
131 struct obstack Active_dir;
133 /* Used to detect cycles. */
134 struct cycle_check_state cycle_check_state;
136 /* Target of a longjmp in case rm has to stop processing the current
137 command-line argument. This happens 1) when rm detects a directory
138 cycle or 2) when it has processed one or more directories, but then
139 is unable to return to the initial working directory to process
140 additional `.'-relative command-line arguments. */
141 jmp_buf current_arg_jumpbuf;
143 typedef struct dirstack_state Dirstack_state;
145 /* Just like close(fd), but don't modify errno. */
147 close_preserve_errno (int fd)
149 int saved_errno = errno;
150 int result = close (fd);
162 hash_compare_strings (void const *x, void const *y)
164 return STREQ (x, y) ? true : false;
168 push_dir (Dirstack_state *ds, const char *dir_name)
170 size_t len = strlen (dir_name);
172 /* Append the string onto the stack. */
173 obstack_grow (&ds->dir_stack, dir_name, len);
175 /* Append a trailing slash. */
176 obstack_1grow (&ds->dir_stack, '/');
178 /* Add one for the slash. */
181 /* Push the length (including slash) onto its stack. */
182 obstack_grow (&ds->len_stack, &len, sizeof (len));
185 /* Return the entry name of the directory on the top of the stack
186 in malloc'd storage. */
188 top_dir (Dirstack_state const *ds)
190 size_t n_lengths = obstack_object_size (&ds->len_stack) / sizeof (size_t);
191 size_t *length = obstack_base (&ds->len_stack);
192 size_t top_len = length[n_lengths - 1];
193 char const *p = obstack_next_free (&ds->dir_stack) - top_len;
194 char *q = xmalloc (top_len);
195 memcpy (q, p, top_len - 1);
201 pop_dir (Dirstack_state *ds)
203 size_t n_lengths = obstack_object_size (&ds->len_stack) / sizeof (size_t);
204 size_t *length = obstack_base (&ds->len_stack);
206 assert (n_lengths > 0);
207 size_t top_len = length[n_lengths - 1];
208 assert (top_len >= 2);
210 /* Pop the specified length of file name. */
211 assert (obstack_object_size (&ds->dir_stack) >= top_len);
212 obstack_blank (&ds->dir_stack, -top_len);
214 /* Pop the length stack, too. */
215 assert (obstack_object_size (&ds->len_stack) >= sizeof (size_t));
216 obstack_blank (&ds->len_stack, -(int) sizeof (size_t));
219 /* Copy the SRC_LEN bytes of data beginning at SRC into the DST_LEN-byte
220 buffer, DST, so that the last source byte is at the end of the destination
221 buffer. If SRC_LEN is longer than DST_LEN, then set *TRUNCATED.
222 Set *RESULT to point to the beginning of (the portion of) the source data
223 in DST. Return the number of bytes remaining in the destination buffer. */
226 right_justify (char *dst, size_t dst_len, const char *src, size_t src_len,
227 char **result, bool *truncated)
232 if (src_len <= dst_len)
235 dp = dst + (dst_len - src_len);
240 sp = src + (src_len - dst_len);
246 *result = memcpy (dp, sp, src_len);
247 return dst_len - src_len;
250 /* Using the global directory name obstack, create the full name FILENAME.
251 Return it in sometimes-realloc'd space that should not be freed by the
252 caller. Realloc as necessary. If realloc fails, use a static buffer
253 and put as long a suffix in that buffer as possible. */
255 #define full_filename(Filename) full_filename_ (ds, Filename)
257 full_filename_ (Dirstack_state const *ds, const char *filename)
259 static char *buf = NULL;
260 static size_t n_allocated = 0;
262 size_t dir_len = obstack_object_size (&ds->dir_stack);
263 char *dir_name = obstack_base (&ds->dir_stack);
264 size_t n_bytes_needed;
267 filename_len = strlen (filename);
268 n_bytes_needed = dir_len + filename_len + 1;
270 if (n_allocated < n_bytes_needed)
272 /* This code requires that realloc accept NULL as the first arg.
273 This function must not use xrealloc. Otherwise, an out-of-memory
274 error involving a file name to be expanded here wouldn't ever
275 be issued. Use realloc and fall back on using a static buffer
276 if memory allocation fails. */
277 char *new_buf = realloc (buf, n_bytes_needed);
278 n_allocated = n_bytes_needed;
282 #define SBUF_SIZE 512
283 #define ELLIPSES_PREFIX "[...]"
284 static char static_buf[SBUF_SIZE];
290 len = right_justify (static_buf, SBUF_SIZE, filename,
291 filename_len + 1, &p, &truncated);
292 right_justify (static_buf, len, dir_name, dir_len, &p, &truncated);
295 memcpy (static_buf, ELLIPSES_PREFIX,
296 sizeof (ELLIPSES_PREFIX) - 1);
304 if (filename_len == 1 && *filename == '.' && dir_len)
306 /* FILENAME is just `.' and dir_len is nonzero.
307 Copy the directory part, omitting the trailing slash,
308 and append a trailing zero byte. */
309 char *p = mempcpy (buf, dir_name, dir_len - 1);
314 /* Copy the directory part, including trailing slash, and then
315 append the filename part, including a trailing zero byte. */
316 memcpy (mempcpy (buf, dir_name, dir_len), filename, filename_len + 1);
317 assert (strlen (buf) + 1 == n_bytes_needed);
324 AD_stack_height (Dirstack_state const *ds)
326 return obstack_object_size (&ds->Active_dir) / sizeof (struct AD_ent);
329 static inline struct AD_ent *
330 AD_stack_top (Dirstack_state const *ds)
332 return (struct AD_ent *)
333 ((char *) obstack_next_free (&ds->Active_dir) - sizeof (struct AD_ent));
337 AD_stack_pop (Dirstack_state *ds)
339 assert (0 < AD_stack_height (ds));
341 /* operate on Active_dir. pop and free top entry */
342 struct AD_ent *top = AD_stack_top (ds);
343 if (top->unremovable)
344 hash_free (top->unremovable);
345 obstack_blank (&ds->Active_dir, -(int) sizeof (struct AD_ent));
348 static Dirstack_state *
351 Dirstack_state *ds = xmalloc (sizeof *ds);
352 obstack_init (&ds->dir_stack);
353 obstack_init (&ds->len_stack);
354 obstack_init (&ds->Active_dir);
359 ds_clear (Dirstack_state *ds)
361 obstack_free (&ds->dir_stack, obstack_finish (&ds->dir_stack));
362 obstack_free (&ds->len_stack, obstack_finish (&ds->len_stack));
363 while (0 < AD_stack_height (ds))
365 obstack_free (&ds->Active_dir, obstack_finish (&ds->Active_dir));
369 ds_free (Dirstack_state *ds)
371 obstack_free (&ds->dir_stack, NULL);
372 obstack_free (&ds->len_stack, NULL);
373 obstack_free (&ds->Active_dir, NULL);
377 /* Pop the active directory (AD) stack and move *DIRP `up' one level,
378 safely. Moving `up' usually means opening `..', but when we've just
379 finished recursively processing a command-line directory argument,
380 there's nothing left on the stack, so set *DIRP to NULL in that case.
381 The idea is to return with *DIRP opened on the parent directory,
382 assuming there are entries in that directory that we need to remove.
384 Whenever using chdir '..' (virtually, now, via openat), verify
385 that the post-chdir dev/ino numbers for `.' match the saved ones.
386 If any system call fails or if dev/ino don't match then give a
387 diagnostic and longjump out.
388 Set *PREV_DIR to the name (in malloc'd storage) of the
389 directory (usually now empty) from which we're coming, and which
390 corresponds to the input value of *DIRP. */
392 AD_pop_and_chdir (DIR **dirp, Dirstack_state *ds, char **prev_dir)
394 struct AD_ent *leaf_dir_ent = AD_stack_top(ds);
395 struct dev_ino leaf_dev_ino = leaf_dir_ent->dev_ino;
396 enum RM_status old_status = leaf_dir_ent->status;
399 /* Get the name of the current (but soon to be `previous') directory
400 from the top of the stack. */
401 *prev_dir = top_dir (ds);
405 top = AD_stack_top (ds);
407 /* If the directory we're about to leave (and try to rmdir)
408 is the one whose dev_ino is being used to detect a cycle,
409 reset cycle_check_state.dev_ino to that of the parent.
410 Otherwise, once that directory is removed, its dev_ino
411 could be reused in the creation (by some other process)
412 of a directory that this rm process would encounter,
413 which would result in a false-positive cycle indication. */
414 CYCLE_CHECK_REFLECT_CHDIR_UP (&ds->cycle_check_state,
415 top->dev_ino, leaf_dev_ino);
417 /* Propagate any failure to parent. */
418 UPDATE_STATUS (top->status, old_status);
420 assert (AD_stack_height (ds));
422 if (1 < AD_stack_height (ds))
425 int fd = openat (dirfd (*dirp), "..", O_RDONLY);
426 if (closedir (*dirp) != 0)
428 error (0, errno, _("FATAL: failed to close directory %s"),
429 quote (full_filename (*prev_dir)));
430 goto next_cmdline_arg;
433 /* The above fails with EACCES when *DIRP is readable but not
434 searchable, when using Solaris' openat. Without this openat
435 call, tests/rm2 would fail to remove directories a/2 and a/3. */
437 fd = openat (AT_FDCWD, full_filename ("."), O_RDONLY);
441 error (0, errno, _("FATAL: cannot open .. from %s"),
442 quote (full_filename (*prev_dir)));
443 goto next_cmdline_arg;
449 _("FATAL: cannot ensure %s (returned to via ..) is safe"),
450 quote (full_filename (".")));
454 /* Ensure that post-chdir dev/ino match the stored ones. */
455 if ( ! SAME_INODE (sb, top->dev_ino))
457 error (0, 0, _("FATAL: directory %s changed dev/ino"),
458 quote (full_filename (".")));
462 *dirp = fdopendir (fd);
465 error (0, errno, _("FATAL: cannot return to .. from %s"),
466 quote (full_filename (".")));
472 longjmp (ds->current_arg_jumpbuf, 1);
477 if (closedir (*dirp) != 0)
479 error (0, errno, _("FATAL: failed to close directory %s"),
480 quote (full_filename (*prev_dir)));
481 goto next_cmdline_arg;
487 /* Initialize *HT if it is NULL.
488 Insert FILENAME into HT. */
490 AD_mark_helper (Hash_table **ht, char const *filename)
494 *ht = hash_initialize (HT_UNREMOVABLE_INITIAL_CAPACITY, NULL, hash_pjw,
495 hash_compare_strings, hash_freer);
499 if (! hash_insert (*ht, filename))
503 /* Mark FILENAME (in current directory) as unremovable. */
505 AD_mark_as_unremovable (Dirstack_state *ds, char const *filename)
507 AD_mark_helper (&AD_stack_top(ds)->unremovable, xstrdup (filename));
510 /* Mark the current directory as unremovable. I.e., mark the entry
511 in the parent directory corresponding to `.'.
512 This happens e.g., when an opendir fails and the only name
513 the caller has conveniently at hand is `.'. */
515 AD_mark_current_as_unremovable (Dirstack_state *ds)
517 struct AD_ent *top = AD_stack_top (ds);
518 char const *curr = top_dir (ds);
520 assert (1 < AD_stack_height (ds));
523 AD_mark_helper (&top->unremovable, curr);
526 /* Push an initial dummy entry onto the stack.
527 This will always be the bottommost entry on the stack. */
529 AD_push_initial (Dirstack_state *ds)
533 /* Extend the stack. */
534 obstack_blank (&ds->Active_dir, sizeof (struct AD_ent));
536 /* Fill in the new values. */
537 top = AD_stack_top (ds);
538 top->unremovable = NULL;
540 /* These should never be used.
541 Give them values that might look suspicious
542 in a debugger or in a diagnostic. */
543 top->dev_ino.st_dev = TYPE_MAXIMUM (dev_t);
544 top->dev_ino.st_ino = TYPE_MAXIMUM (ino_t);
547 /* Push info about the current working directory (".") onto the
548 active directory stack. DIR is the ./-relative name through
549 which we've just `chdir'd to this directory. DIR_SB_FROM_PARENT
550 is the result of calling lstat on DIR from the parent of DIR.
551 Longjump out (skipping the entire command line argument we're
552 dealing with) if `fstat (FD_CWD, ...' fails or if someone has
553 replaced DIR with e.g., a symlink to some other directory. */
555 AD_push (int fd_cwd, Dirstack_state *ds, char const *dir,
556 struct stat const *dir_sb_from_parent)
562 /* If our uses of openat are guaranteed not to
563 follow a symlink, then we can skip this check. */
567 if (fstat (fd_cwd, &sb) != 0)
569 error (0, errno, _("FATAL: cannot enter directory %s"),
570 quote (full_filename (".")));
571 longjmp (ds->current_arg_jumpbuf, 1);
574 if ( ! SAME_INODE (sb, *dir_sb_from_parent))
577 _("FATAL: just-changed-to directory %s changed dev/ino"),
578 quote (full_filename (".")));
579 longjmp (ds->current_arg_jumpbuf, 1);
583 /* Extend the stack. */
584 obstack_blank (&ds->Active_dir, sizeof (struct AD_ent));
586 /* The active directory stack must be one larger than the length stack. */
587 assert (AD_stack_height (ds) ==
588 1 + obstack_object_size (&ds->len_stack) / sizeof (size_t));
590 /* Fill in the new values. */
591 top = AD_stack_top (ds);
592 top->dev_ino.st_dev = dir_sb_from_parent->st_dev;
593 top->dev_ino.st_ino = dir_sb_from_parent->st_ino;
594 top->unremovable = NULL;
598 AD_is_removable (Dirstack_state const *ds, char const *file)
600 struct AD_ent *top = AD_stack_top (ds);
601 return ! (top->unremovable && hash_lookup (top->unremovable, file));
604 /* Return true if DIR is determined to be an empty directory. */
606 is_empty_dir (int fd_cwd, char const *dir)
609 struct dirent const *dp;
611 int fd = openat (fd_cwd, dir,
612 (O_RDONLY | O_DIRECTORY
613 | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK));
618 dirp = fdopendir (fd);
626 dp = readdir_ignoring_dot_and_dotdot (dirp);
631 return saved_errno == 0 ? true : false;
634 /* Return true if FILE is determined to be an unwritable non-symlink.
635 Otherwise, return false (including when lstat'ing it fails).
636 If lstat (aka fstatat) succeeds, set *BUF_P to BUF.
637 This is to avoid calling euidaccess when FILE is a symlink. */
639 write_protected_non_symlink (int fd_cwd,
641 Dirstack_state const *ds,
645 if (fstatat (fd_cwd, file, buf, AT_SYMLINK_NOFOLLOW) != 0)
648 if (S_ISLNK (buf->st_mode))
650 /* Here, we know FILE is not a symbolic link. */
652 /* In order to be reentrant -- i.e., to avoid changing the working
653 directory, and at the same time to be able to deal with alternate
654 access control mechanisms (ACLs, xattr-style attributes) and
655 arbitrarily deep trees -- we need a function like eaccessat, i.e.,
656 like Solaris' eaccess, but fd-relative, in the spirit of openat. */
658 /* In the absence of a native eaccessat function, here are some of
659 the implementation choices [#4 and #5 were suggested by Paul Eggert]:
660 1) call openat with O_WRONLY|O_NOCTTY
661 Disadvantage: may create the file and doesn't work for directory,
662 may mistakenly report `unwritable' for EROFS or ACLs even though
663 perm bits say the file is writable.
665 2) fake eaccessat (save_cwd, fchdir, call euidaccess, restore_cwd)
666 Disadvantage: changes working directory (not reentrant) and can't
667 work if save_cwd fails.
669 3) if (euidaccess (full_filename (file), W_OK) == 0)
670 Disadvantage: doesn't work if full_filename is too long.
671 Inefficient for very deep trees (O(Depth^2)).
673 4) If the full pathname is sufficiently short (say, less than
674 PATH_MAX or 8192 bytes, whichever is shorter):
675 use method (3) (i.e., euidaccess (full_filename (file), W_OK));
676 Otherwise: vfork, fchdir in the child, run euidaccess in the
677 child, then the child exits with a status that tells the parent
678 whether euidaccess succeeded.
680 This avoids the O(N**2) algorithm of method (3), and it also avoids
681 the failure-due-to-too-long-file-names of method (3), but it's fast
682 in the normal shallow case. It also avoids the lack-of-reentrancy
683 and the save_cwd problems.
684 Disadvantage; it uses a process slot for very-long file names,
685 and would be very slow for hierarchies with many such files.
687 5) If the full file name is sufficiently short (say, less than
688 PATH_MAX or 8192 bytes, whichever is shorter):
689 use method (3) (i.e., euidaccess (full_filename (file), W_OK));
690 Otherwise: look just at the file bits. Perhaps issue a warning
691 the first time this occurs.
693 This is like (4), except for the "Otherwise" case where it isn't as
694 "perfect" as (4) but is considerably faster. It conforms to current
695 POSIX, and is uniformly better than what Solaris and FreeBSD do (they
696 mess up with long file names). */
699 /* This implements #5: */
701 = obstack_object_size (&ds->dir_stack) + strlen (file);
703 return (file_name_len < MIN (PATH_MAX, 8192)
704 ? euidaccess (full_filename (file), W_OK) != 0 && errno == EACCES
705 : euidaccess_stat (buf, W_OK) != 0);
709 /* Prompt whether to remove FILENAME, if required via a combination of
710 the options specified by X and/or file attributes. If the file may
711 be removed, return RM_OK. If the user declines to remove the file,
712 return RM_USER_DECLINED. If not ignoring missing files and we
713 cannot lstat FILENAME, then return RM_ERROR.
715 Depending on MODE, ask whether to `descend into' or to `remove' the
716 directory FILENAME. MODE is ignored when FILENAME is not a directory.
717 Set *IS_EMPTY to T_YES if FILENAME is an empty directory, and it is
718 appropriate to try to remove it with rmdir (e.g. recursive mode).
719 Don't even try to set *IS_EMPTY when MODE == PA_REMOVE_DIR.
720 Set *IS_DIR to T_YES or T_NO if we happen to determine whether
721 FILENAME is a directory. */
722 static enum RM_status
723 prompt (int fd_cwd, Dirstack_state const *ds, char const *filename,
724 struct rm_options const *x, enum Prompt_action mode,
725 Ternary *is_dir, Ternary *is_empty)
727 bool write_protected = false;
728 struct stat *sbuf = NULL;
731 *is_empty = T_UNKNOWN;
734 if (((!x->ignore_missing_files & (x->interactive | x->stdin_tty))
735 && (write_protected = write_protected_non_symlink (fd_cwd, filename,
742 if (fstatat (fd_cwd, filename, sbuf, AT_SYMLINK_NOFOLLOW))
744 /* lstat failed. This happens e.g., with `rm '''. */
745 error (0, errno, _("cannot remove %s"),
746 quote (full_filename (filename)));
751 if (S_ISDIR (sbuf->st_mode) && !x->recursive)
753 error (0, EISDIR, _("cannot remove directory %s"),
754 quote (full_filename (filename)));
758 /* Using permissions doesn't make sense for symlinks. */
759 if (S_ISLNK (sbuf->st_mode))
761 if ( ! x->interactive)
763 write_protected = false;
766 /* Issue the prompt. */
768 char const *quoted_name = quote (full_filename (filename));
770 *is_dir = (S_ISDIR (sbuf->st_mode) ? T_YES : T_NO);
772 /* FIXME: use a variant of error (instead of fprintf) that doesn't
773 append a newline. Then we won't have to declare program_name in
775 if (S_ISDIR (sbuf->st_mode)
777 && mode == PA_DESCEND_INTO_DIR
778 && ((*is_empty = (is_empty_dir (fd_cwd, filename) ? T_YES : T_NO))
782 ? _("%s: descend into write-protected directory %s? ")
783 : _("%s: descend into directory %s? ")),
784 program_name, quoted_name);
787 /* TRANSLATORS: You may find it more convenient to translate
788 the equivalent of _("%s: remove %s (write-protected) %s? ").
789 It should avoid grammatical problems with the output
793 ? _("%s: remove write-protected %s %s? ")
794 : _("%s: remove %s %s? ")),
795 program_name, file_type (sbuf), quoted_name);
799 return RM_USER_DECLINED;
805 /* Return true if FILENAME is a directory (and not a symlink to a directory).
806 Otherwise, including the case in which lstat fails, return false.
807 Do not modify errno. */
809 is_dir_lstat (char const *filename)
812 int saved_errno = errno;
813 bool is_dir = lstat (filename, &sbuf) == 0 && S_ISDIR (sbuf.st_mode);
818 #if HAVE_STRUCT_DIRENT_D_TYPE
820 /* True if the type of the directory entry D is known. */
821 # define DT_IS_KNOWN(d) ((d)->d_type != DT_UNKNOWN)
823 /* True if the type of the directory entry D must be T. */
824 # define DT_MUST_BE(d, t) ((d)->d_type == (t))
827 # define DT_IS_KNOWN(d) false
828 # define DT_MUST_BE(d, t) false
831 #define DO_UNLINK(Fd_cwd, Filename, X) \
834 if (unlinkat (Fd_cwd, Filename, 0) == 0) \
837 printf (_("removed %s\n"), quote (full_filename (Filename))); \
841 if (errno == ENOENT && (X)->ignore_missing_files) \
846 #define DO_RMDIR(Fd_cwd, Filename, X) \
849 if (unlinkat (Fd_cwd, Filename, AT_REMOVEDIR) == 0) /* rmdir */ \
852 printf (_("removed directory: %s\n"), \
853 quote (full_filename (Filename))); \
857 if (errno == ENOENT && (X)->ignore_missing_files) \
860 if (errno == ENOTEMPTY || errno == EEXIST) \
861 return RM_NONEMPTY_DIR; \
865 /* Remove the file or directory specified by FILENAME.
866 Return RM_OK if it is removed, and RM_ERROR or RM_USER_DECLINED if not.
867 But if FILENAME specifies a non-empty directory, return RM_NONEMPTY_DIR. */
869 static enum RM_status
870 remove_entry (int fd_cwd, Dirstack_state const *ds, char const *filename,
871 struct rm_options const *x, struct dirent const *dp)
874 Ternary is_empty_directory;
875 enum RM_status s = prompt (fd_cwd, ds, filename, x, PA_DESCEND_INTO_DIR,
876 &is_dir, &is_empty_directory);
881 /* Why bother with the following if/else block? Because on systems with
882 an unlink function that *can* unlink directories, we must determine the
883 type of each entry before removing it. Otherwise, we'd risk unlinking
884 an entire directory tree simply by unlinking a single directory; then
885 all the storage associated with that hierarchy would not be freed until
886 the next fsck. Not nice. To avoid that, on such slightly losing
887 systems, we need to call lstat to determine the type of each entry,
888 and that represents extra overhead that -- it turns out -- we can
889 avoid on non-losing systems, since there, unlink will never remove
890 a directory. Also, on systems where unlink may unlink directories,
891 we're forced to allow a race condition: we lstat a non-directory, then
892 go to unlink it, but in the mean time, a malicious someone could have
893 replaced it with a directory. */
895 if (cannot_unlink_dir ())
897 if (is_dir == T_YES && ! x->recursive)
899 error (0, EISDIR, _("cannot remove directory %s"),
900 quote (full_filename (filename)));
904 /* is_empty_directory is set iff it's ok to use rmdir.
905 Note that it's set only in interactive mode -- in which case it's
906 an optimization that arranges so that the user is asked just
907 once whether to remove the directory. */
908 if (is_empty_directory == T_YES)
909 DO_RMDIR (fd_cwd, filename, x);
911 /* If we happen to know that FILENAME is a directory, return now
912 and let the caller remove it -- this saves the overhead of a failed
913 unlink call. If FILENAME is a command-line argument, then dp is NULL,
914 so we'll first try to unlink it. Using unlink here is ok, because it
915 cannot remove a directory. */
916 if ((dp && DT_MUST_BE (dp, DT_DIR)) || is_dir == T_YES)
917 return RM_NONEMPTY_DIR;
919 DO_UNLINK (fd_cwd, filename, x);
921 /* Upon a failed attempt to unlink a directory, most non-Linux systems
922 set errno to the POSIX-required value EPERM. In that case, change
923 errno to EISDIR so that we emit a better diagnostic. */
924 if (! x->recursive && errno == EPERM && is_dir_lstat (filename))
928 || errno == ENOENT || errno == ENOTDIR
929 || errno == ELOOP || errno == ENAMETOOLONG)
931 /* Either --recursive is not in effect, or the file cannot be a
932 directory. Report the unlink problem and fail. */
933 error (0, errno, _("cannot remove %s"),
934 quote (full_filename (filename)));
940 /* If we don't already know whether FILENAME is a directory, find out now.
941 Then, if it's a non-directory, we can use unlink on it. */
942 if (is_dir == T_UNKNOWN)
944 if (dp && DT_IS_KNOWN (dp))
945 is_dir = DT_MUST_BE (dp, DT_DIR) ? T_YES : T_NO;
949 if (fstatat (fd_cwd, filename, &sbuf, AT_SYMLINK_NOFOLLOW))
951 if (errno == ENOENT && x->ignore_missing_files)
954 error (0, errno, _("cannot remove %s"),
955 quote (full_filename (filename)));
959 is_dir = S_ISDIR (sbuf.st_mode) ? T_YES : T_NO;
965 /* At this point, barring race conditions, FILENAME is known
966 to be a non-directory, so it's ok to try to unlink it. */
967 DO_UNLINK (fd_cwd, filename, x);
969 /* unlink failed with some other error code. report it. */
970 error (0, errno, _("cannot remove %s"),
971 quote (full_filename (filename)));
977 error (0, EISDIR, _("cannot remove directory %s"),
978 quote (full_filename (filename)));
982 if (is_empty_directory == T_YES)
984 DO_RMDIR (fd_cwd, filename, x);
985 /* Don't diagnose any failure here.
986 It'll be detected when the caller tries another way. */
990 return RM_NONEMPTY_DIR;
993 /* Given FD_CWD, the file descriptor for an open directory,
994 open its subdirectory F (F is already `known' to be a directory,
995 so if it is no longer one, someone is playing games), return a DIR*
996 pointer for F, and put F's `stat' data in *SUBDIR_SB.
997 Upon failure give a diagnostic and return NULL.
998 If PREV_ERRNO is nonzero, it is the errno value from a preceding failed
999 unlink- or rmdir-like system call -- use that value instead of ENOTDIR
1000 if an opened file turns out not to be a directory. This is important
1001 when the preceding non-dir-unlink failed due to e.g., EPERM or EACCES.
1002 The caller must use a nonnnull CWD_ERRNO the first
1003 time this function is called for each command-line-specified directory.
1004 If CWD_ERRNO is not null, set *CWD_ERRNO to the appropriate error number
1005 if this function fails to restore the initial working directory.
1006 If it is null, report an error and exit if the working directory
1009 fd_to_subdirp (int fd_cwd, char const *f,
1010 struct rm_options const *x, int prev_errno,
1011 struct stat *subdir_sb, Dirstack_state *ds,
1012 int *cwd_errno ATTRIBUTE_UNUSED)
1014 int open_flags = O_RDONLY | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK;
1015 int fd_sub = openat_permissive (fd_cwd, f, open_flags, 0, cwd_errno);
1017 /* Record dev/ino of F. We may compare them against saved values
1018 to thwart any attempt to subvert the traversal. They are also used
1019 to detect directory cycles. */
1020 if (fd_sub < 0 || fstat (fd_sub, subdir_sb) != 0)
1023 close_preserve_errno (fd_sub);
1027 if (! S_ISDIR (subdir_sb->st_mode))
1029 errno = prev_errno ? prev_errno : ENOTDIR;
1030 close_preserve_errno (fd_sub);
1034 DIR *subdir_dirp = fdopendir (fd_sub);
1035 if (subdir_dirp == NULL)
1037 close_preserve_errno (fd_sub);
1044 /* Remove entries in the directory open on DIRP
1045 Upon finding a directory that is both non-empty and that can be chdir'd
1046 into, return RM_OK and set *SUBDIR and fill in SUBDIR_SB, where
1047 SUBDIR is the malloc'd name of the subdirectory if the chdir succeeded,
1048 NULL otherwise (e.g., if opendir failed or if there was no subdirectory).
1049 Likewise, SUBDIR_SB is the result of calling lstat on SUBDIR.
1050 Return RM_OK if all entries are removed. Return RM_ERROR if any
1051 entry cannot be removed. Otherwise, return RM_USER_DECLINED if
1052 the user declines to remove at least one entry. Remove as much as
1053 possible, continuing even if we fail to remove some entries. */
1054 static enum RM_status
1055 remove_cwd_entries (DIR **dirp,
1056 Dirstack_state *ds, char **subdir, struct stat *subdir_sb,
1057 struct rm_options const *x)
1059 struct AD_ent *top = AD_stack_top (ds);
1060 enum RM_status status = top->status;
1061 size_t n_unlinked_since_opendir_or_last_rewind = 0;
1063 assert (VALID_STATUS (status));
1068 struct dirent const *dp;
1069 enum RM_status tmp_status;
1072 /* Set errno to zero so we can distinguish between a readdir failure
1073 and when readdir simply finds that there are no more entries. */
1075 dp = readdir_ignoring_dot_and_dotdot (*dirp);
1082 else if (CONSECUTIVE_READDIR_UNLINK_THRESHOLD
1083 < n_unlinked_since_opendir_or_last_rewind)
1085 /* Call rewinddir if we've called unlink or rmdir so many times
1086 (since the opendir or the previous rewinddir) that this
1087 NULL-return may be the symptom of a buggy readdir. */
1089 n_unlinked_since_opendir_or_last_rewind = 0;
1097 /* Skip files we've already tried/failed to remove. */
1098 if ( ! AD_is_removable (ds, f))
1101 /* Pass dp->d_type info to remove_entry so the non-glibc
1102 case can decide whether to use unlink or chdir.
1103 Systems without the d_type member will have to endure
1104 the performance hit of first calling lstat F. */
1105 tmp_status = remove_entry (dirfd (*dirp), ds, f, x, dp);
1109 /* Count how many files we've unlinked since the initial
1110 opendir or the last rewinddir. On buggy systems, if you
1111 remove too many, readdir returns NULL even though there
1112 remain unprocessed directory entries. */
1113 ++n_unlinked_since_opendir_or_last_rewind;
1117 case RM_USER_DECLINED:
1118 AD_mark_as_unremovable (ds, f);
1119 UPDATE_STATUS (status, tmp_status);
1122 case RM_NONEMPTY_DIR:
1124 DIR *subdir_dirp = fd_to_subdirp (dirfd (*dirp), f,
1125 x, errno, subdir_sb, ds, NULL);
1126 if (subdir_dirp == NULL)
1130 /* CAUTION: this test and diagnostic are identical to
1131 those following the other use of fd_to_subdirp. */
1132 if (errno == ENOENT && x->ignore_missing_files)
1134 /* With -f, don't report "file not found". */
1138 /* Upon fd_to_subdirp failure, try to remove F directly,
1139 in case it's just an empty directory. */
1140 int saved_errno = errno;
1141 if (unlinkat (dirfd (*dirp), f, AT_REMOVEDIR) == 0)
1144 error (0, saved_errno,
1145 _("cannot remove %s"), quote (full_filename (f)));
1148 if (status == RM_ERROR)
1149 AD_mark_as_unremovable (ds, f);
1153 if (cycle_check (&ds->cycle_check_state, subdir_sb))
1156 WARNING: Circular directory structure.\n\
1157 This almost certainly means that you have a corrupted file system.\n\
1158 NOTIFY YOUR SYSTEM MANAGER.\n\
1159 The following directory is part of the cycle:\n %s\n"),
1160 quote (full_filename (".")));
1161 longjmp (ds->current_arg_jumpbuf, 1);
1164 *subdir = xstrdup (f);
1165 if (closedir (*dirp) != 0)
1167 error (0, 0, _("failed to close directory %s"),
1168 quote (full_filename (".")));
1171 *dirp = subdir_dirp;
1177 /* Record status for this directory. */
1178 UPDATE_STATUS (top->status, status);
1184 /* Ensure that *dirp is not NULL and that its file descriptor is valid. */
1185 assert (*dirp != NULL);
1186 assert (0 <= fcntl (dirfd (*dirp), F_GETFD));
1191 /* Do this after each call to AD_push or AD_push_initial.
1192 Because the status = RM_OK bit is too remove-specific to
1193 go into the general-purpose AD_* package. */
1194 #define AD_INIT_OTHER_MEMBERS() \
1197 AD_stack_top(ds)->status = RM_OK; \
1201 /* Remove the hierarchy rooted at DIR.
1202 Do that by changing into DIR, then removing its contents, then
1203 returning to the original working directory and removing DIR itself.
1204 Don't use recursion. Be careful when using chdir ".." that we
1205 return to the same directory from which we came, if necessary.
1206 Return an RM_status value to indicate success or failure. */
1208 static enum RM_status
1209 remove_dir (int fd_cwd, Dirstack_state *ds, char const *dir,
1210 struct rm_options const *x, int *cwd_errno)
1212 enum RM_status status;
1215 /* There is a race condition in that an attacker could replace the nonempty
1216 directory, DIR, with a symlink between the preceding call to rmdir
1217 (unlinkat, in our caller) and fd_to_subdirp's openat call. But on most
1218 systems, even those without openat, this isn't a problem, since we ensure
1219 that opening a symlink will fail, when that is possible. Otherwise,
1220 fd_to_subdirp's fstat, along with the `fstat' and the dev/ino
1221 comparison in AD_push ensure that we detect it and fail. */
1223 DIR *dirp = fd_to_subdirp (fd_cwd, dir, x, 0, &dir_sb, ds, cwd_errno);
1227 /* CAUTION: this test and diagnostic are identical to
1228 those following the other use of fd_to_subdirp. */
1229 if (errno == ENOENT && x->ignore_missing_files)
1231 /* With -f, don't report "file not found". */
1235 /* Upon fd_to_subdirp failure, try to remove DIR directly,
1236 in case it's just an empty directory. */
1237 int saved_errno = errno;
1238 if (unlinkat (fd_cwd, dir, AT_REMOVEDIR) == 0)
1241 error (0, saved_errno,
1242 _("cannot remove %s"), quote (full_filename (dir)));
1248 if (ROOT_DEV_INO_CHECK (x->root_dev_ino, &dir_sb))
1250 ROOT_DEV_INO_WARN (full_filename (dir));
1252 goto closedir_and_return;
1255 AD_push (dirfd (dirp), ds, dir, &dir_sb);
1256 AD_INIT_OTHER_MEMBERS ();
1262 char *subdir = NULL;
1263 struct stat subdir_sb;
1264 enum RM_status tmp_status;
1266 tmp_status = remove_cwd_entries (&dirp, ds, &subdir, &subdir_sb, x);
1268 if (tmp_status != RM_OK)
1270 UPDATE_STATUS (status, tmp_status);
1271 AD_mark_current_as_unremovable (ds);
1275 AD_push (dirfd (dirp), ds, subdir, &subdir_sb);
1276 AD_INIT_OTHER_MEMBERS ();
1282 /* Execution reaches this point when we've removed the last
1283 removable entry from the current directory. */
1285 /* The name of the directory that we have just processed,
1286 nominally removing all of its contents. */
1289 AD_pop_and_chdir (&dirp, ds, &empty_dir);
1290 int fd = (dirp != NULL ? dirfd (dirp) : AT_FDCWD);
1291 assert (dirp != NULL || AD_stack_height (ds) == 1);
1293 /* Try to remove EMPTY_DIR only if remove_cwd_entries succeeded. */
1294 if (tmp_status == RM_OK)
1296 /* This does a little more work than necessary when it actually
1297 prompts the user. E.g., we already know that D is a directory
1298 and that it's almost certainly empty, yet we lstat it.
1299 But that's no big deal since we're interactive. */
1302 enum RM_status s = prompt (fd, ds, empty_dir, x,
1303 PA_REMOVE_DIR, &is_dir, &is_empty);
1309 goto closedir_and_return;
1312 if (unlinkat (fd, empty_dir, AT_REMOVEDIR) == 0)
1315 printf (_("removed directory: %s\n"),
1316 quote (full_filename (empty_dir)));
1320 error (0, errno, _("cannot remove directory %s"),
1321 quote (full_filename (empty_dir)));
1322 AD_mark_as_unremovable (ds, empty_dir);
1324 UPDATE_STATUS (AD_stack_top(ds)->status, status);
1330 if (AD_stack_height (ds) == 1)
1335 /* If the first/final hash table of unremovable entries was used,
1339 closedir_and_return:;
1340 if (dirp != NULL && closedir (dirp) != 0)
1342 error (0, 0, _("failed to close directory %s"),
1343 quote (full_filename (".")));
1350 /* Remove the file or directory specified by FILENAME.
1351 Return RM_OK if it is removed, and RM_ERROR or RM_USER_DECLINED if not. */
1353 static enum RM_status
1354 rm_1 (Dirstack_state *ds, char const *filename,
1355 struct rm_options const *x, int *cwd_errno)
1357 char const *base = last_component (filename);
1358 if (DOT_OR_DOTDOT (base))
1360 error (0, 0, _("cannot remove `.' or `..'"));
1364 AD_push_initial (ds);
1365 AD_INIT_OTHER_MEMBERS ();
1367 int fd_cwd = AT_FDCWD;
1368 enum RM_status status = remove_entry (fd_cwd, ds, filename, x, NULL);
1369 if (status == RM_NONEMPTY_DIR)
1371 /* In the event that remove_dir->remove_cwd_entries detects
1372 a directory cycle, arrange to fail, give up on this FILE, but
1373 continue on with any other arguments. */
1374 if (setjmp (ds->current_arg_jumpbuf))
1377 status = remove_dir (fd_cwd, ds, filename, x, cwd_errno);
1385 /* Remove all files and/or directories specified by N_FILES and FILE.
1386 Apply the options in X. */
1387 extern enum RM_status
1388 rm (size_t n_files, char const *const *file, struct rm_options const *x)
1390 enum RM_status status = RM_OK;
1391 Dirstack_state *ds = ds_init ();
1395 for (i = 0; i < n_files; i++)
1397 if (cwd_errno && IS_RELATIVE_FILE_NAME (file[i]))
1399 error (0, 0, _("cannot remove relative-named %s"), quote (file[i]));
1404 cycle_check_init (&ds->cycle_check_state);
1405 enum RM_status s = rm_1 (ds, file[i], x, &cwd_errno);
1406 assert (VALID_STATUS (s));
1407 UPDATE_STATUS (status, s);
1410 if (x->require_restore_cwd && cwd_errno)
1412 error (0, cwd_errno,
1413 _("cannot restore current working directory"));