📄 remove.c
字号:
if (dp == NULL) return true; f = dp->d_name; if ( ! DOT_OR_DOTDOT (f)) return false; }}/* Prompt whether to remove FILENAME, if required via a combination of the options specified by X and/or file attributes. If the file may be removed, return RM_OK. If the user declines to remove the file, return RM_USER_DECLINED. If not ignoring missing files and we cannot lstat FILENAME, then return RM_ERROR. Depending on MODE, ask whether to `descend into' or to `remove' the directory FILENAME. MODE is ignored when FILENAME is not a directory. Set *IS_EMPTY to T_YES if FILENAME is an empty directory, and it is appropriate to try to remove it with rmdir (e.g. recursive mode). Don't even try to set *IS_EMPTY when MODE == PA_REMOVE_DIR. Set *IS_DIR to T_YES or T_NO if we happen to determine whether FILENAME is a directory. */static enum RM_statusprompt (char const *filename, struct rm_options const *x, enum Prompt_action mode, Ternary *is_dir, Ternary *is_empty){ int write_protected = 0; *is_empty = T_UNKNOWN; *is_dir = T_UNKNOWN; if ((!x->ignore_missing_files && (x->interactive || x->stdin_tty) && (write_protected = (euidaccess (filename, W_OK) && errno == EACCES))) || x->interactive) { struct stat sbuf; if (lstat (filename, &sbuf)) { /* lstat failed. This happens e.g., with `rm '''. */ error (0, errno, _("cannot lstat %s"), quote (full_filename (filename))); return RM_ERROR; } /* Using permissions doesn't make sense for symlinks. */ if (S_ISLNK (sbuf.st_mode)) write_protected = 0; /* Issue the prompt. */ { char const *quoted_name = quote (full_filename (filename)); *is_dir = (S_ISDIR (sbuf.st_mode) ? T_YES : T_NO); /* FIXME: use a variant of error (instead of fprintf) that doesn't append a newline. Then we won't have to declare program_name in this file. */ if (S_ISDIR (sbuf.st_mode) && x->recursive && mode == PA_DESCEND_INTO_DIR && ((*is_empty = (is_empty_dir (filename) ? T_YES : T_NO)) == T_NO)) fprintf (stderr, (write_protected ? _("%s: descend into write-protected directory %s? ") : _("%s: descend into directory %s? ")), program_name, quoted_name); else { /* TRANSLATORS: You may find it more convenient to translate the equivalent of _("%1$s: %3$s is write-protected and is of type `%2$s'. Remove it? "). This is more verbose than the original but it should avoid grammatical problems with the output of file_type. */ fprintf (stderr, (write_protected ? _("%s: remove write-protected %s %s? ") : _("%s: remove %s %s? ")), program_name, file_type (&sbuf), quoted_name); } if (!yesno ()) return RM_USER_DECLINED; } } return RM_OK;}#if HAVE_STRUCT_DIRENT_D_TYPE# define DT_IS_DIR(D) ((D)->d_type == DT_DIR)#else/* Use this only if the member exists -- i.e., don't return 0. */# define DT_IS_DIR(D) do_not_use_this_macro#endif#define DO_UNLINK(Filename, X) \ do \ { \ if (unlink (Filename) == 0) \ { \ if ((X)->verbose) \ printf (_("removed %s\n"), quote (full_filename (Filename))); \ return RM_OK; \ } \ \ if (errno == ENOENT && (X)->ignore_missing_files) \ return RM_OK; \ } \ while (0)#define DO_RMDIR(Filename, X) \ do \ { \ if (rmdir (Filename) == 0) \ { \ if ((X)->verbose) \ printf (_("removed directory: %s\n"), \ quote (full_filename (Filename))); \ return RM_OK; \ } \ \ if (errno == ENOENT && (X)->ignore_missing_files) \ return RM_OK; \ \ if (errno == ENOTEMPTY || errno == EEXIST) \ return RM_NONEMPTY_DIR; \ } \ while (0)/* Remove the file or directory specified by FILENAME. Return RM_OK if it is removed, and RM_ERROR or RM_USER_DECLINED if not. But if FILENAME specifies a non-empty directory, return RM_NONEMPTY_DIR. */static enum RM_statusremove_entry (char const *filename, struct rm_options const *x, struct dirent const *dp){ Ternary is_dir; Ternary is_empty_directory; enum RM_status s = prompt (filename, x, PA_DESCEND_INTO_DIR, &is_dir, &is_empty_directory); if (s != RM_OK) return s; /* Why bother with the following #if/#else block? Because on systems with an unlink function that *can* unlink directories, we must determine the type of each entry before removing it. Otherwise, we'd risk unlinking an entire directory tree simply by unlinking a single directory; then all the storage associated with that hierarchy would not be freed until the next reboot. Not nice. To avoid that, on such slightly losing systems, we need to call lstat to determine the type of each entry, and that represents extra overhead that -- it turns out -- we can avoid on GNU-libc-based systems, since there, unlink will never remove a directory. */#if ROOT_CAN_UNLINK_DIRS /* If we don't already know whether FILENAME is a directory, find out now. Then, if it's a non-directory, we can use unlink on it. */ if (is_dir == T_UNKNOWN) {# if HAVE_STRUCT_DIRENT_D_TYPE if (dp) is_dir = DT_IS_DIR (dp) ? T_YES : T_NO; else# endif { struct stat sbuf; if (lstat (filename, &sbuf)) { if (errno == ENOENT && x->ignore_missing_files) return RM_OK; error (0, errno, _("cannot lstat %s"), quote (full_filename (filename))); return RM_ERROR; } is_dir = S_ISDIR (sbuf.st_mode) ? T_YES : T_NO; } } if (is_dir == T_NO) { /* At this point, barring race conditions, FILENAME is known to be a non-directory, so it's ok to try to unlink it. */ DO_UNLINK (filename, x); /* unlink failed with some other error code. report it. */ error (0, errno, _("cannot remove %s"), quote (full_filename (filename))); return RM_ERROR; } if (! x->recursive) { error (0, EISDIR, _("cannot remove directory %s"), quote (full_filename (filename))); return RM_ERROR; } if (is_empty_directory == T_YES) { DO_RMDIR (filename, x); /* Don't diagnose any failure here. It'll be detected when the caller tries another way. */ }#else /* is_empty_directory is set iff it's ok to use rmdir. Note that it's set only in interactive mode -- in which case it's an optimization that arranges so that the user is asked just once whether to remove the directory. */ if (is_empty_directory == T_YES) DO_RMDIR (filename, x); /* If we happen to know that FILENAME is a directory, return now and let the caller remove it -- this saves the overhead of a failed unlink call. If FILENAME is a command-line argument, then dp is NULL, so we'll first try to unlink it. Using unlink here is ok, because it cannot remove a directory. */ if ((dp && DT_IS_DIR (dp)) || is_dir == T_YES) return RM_NONEMPTY_DIR; DO_UNLINK (filename, x); /* Accept either EISDIR or EPERM as an indication that FILENAME may be a directory. POSIX says that unlink must set errno to EPERM when it fails to remove a directory, while Linux-2.4.18 sets it to EISDIR. */ if ((errno != EISDIR && errno != EPERM) || ! x->recursive) { /* some other error code. Report it and fail. Likewise, if we're trying to remove a directory without the --recursive option. */ error (0, errno, _("cannot remove %s"), quote (full_filename (filename))); return RM_ERROR; }#endif return RM_NONEMPTY_DIR;}/* Remove entries in `.', the current working directory (cwd). Upon finding a directory that is both non-empty and that can be chdir'd into, return zero and set *SUBDIR and fill in SUBDIR_SB, where SUBDIR is the malloc'd name of the subdirectory if the chdir succeeded, NULL otherwise (e.g., if opendir failed or if there was no subdirectory). Likewise, SUBDIR_SB is the result of calling lstat on SUBDIR. Return RM_OK if all entries are removed. Remove RM_ERROR if any entry cannot be removed. Otherwise, return RM_USER_DECLINED if the user declines to remove at least one entry. Remove as much as possible, continuing even if we fail to remove some entries. */static enum RM_statusremove_cwd_entries (char **subdir, struct stat *subdir_sb, struct rm_options const *x){ DIR *dirp = opendir ("."); struct AD_ent *top = AD_stack_top (); enum RM_status status = top->status; assert (VALID_STATUS (status)); *subdir = NULL; if (dirp == NULL) { if (errno != ENOENT || !x->ignore_missing_files) { error (0, errno, _("cannot open directory %s"), quote (full_filename ("."))); return RM_ERROR; } } while (1) { struct dirent *dp = readdir (dirp); enum RM_status tmp_status; const char *f; if (dp == NULL) break; f = dp->d_name; if (DOT_OR_DOTDOT (f)) continue; /* Skip files we've already tried/failed to remove. */ if ( ! AD_is_removable (f)) continue; /* Pass dp->d_type info to remove_entry so the non-glibc case can decide whether to use unlink or chdir. Systems without the d_type member will have to endure the performance hit of first calling lstat F. */ tmp_status = remove_entry (f, x, dp); switch (tmp_status) { case RM_OK: /* do nothing */ break; case RM_ERROR: case RM_USER_DECLINED: AD_mark_as_unremovable (f); UPDATE_STATUS (status, tmp_status); break; case RM_NONEMPTY_DIR: /* Record dev/ino of F so that we can compare that with dev/ino of `.' after the chdir. This dev/ino pair is also used in cycle detection. */ if (lstat (f, subdir_sb)) error (EXIT_FAILURE, errno, _("cannot lstat %s"), quote (full_filename (f))); if (chdir (f)) { error (0, errno, _("cannot chdir from %s to %s"), quote_n (0, full_filename (".")), quote_n (1, f)); AD_mark_as_unremovable (f); status = RM_ERROR; break; } cycle_check (subdir_sb); *subdir = xstrdup (f); break; } /* Record status for this directory. */ UPDATE_STATUS (top->status, status); if (*subdir) break; } closedir (dirp); return status;}/* Remove the hierarchy rooted at DIR. Do that by changing into DIR, then removing its contents, then returning to the original working directory and removing DIR itself. Don't use recursion. Be careful when using chdir ".." that we return to the same directory from which we came, if necessary. Return 1 for success, 0 if some file cannot be removed or if a chdir fails. If the working directory cannot be restored, exit immediately. */static enum RM_statusremove_dir (char const *dir, struct saved_cwd **cwd_state, struct rm_options const *x){ enum RM_status status; struct stat dir_sb; /* Save any errno (from caller's failed remove_entry call), in case DIR is not a directory, so that we can give a reasonable diagnostic. */ int saved_errno = errno; if (*cwd_state == NULL) { *cwd_state = XMALLOC (struct saved_cwd, 1); if (save_cwd (*cwd_state)) return RM_ERROR; AD_push_initial (*cwd_state); } /* There is a race condition in that an attacker could replace the nonempty directory, DIR, with a symlink between the preceding call to rmdir (in our caller) and the chdir below. However, the following lstat, along with the `stat (".",...' and dev/ino comparison in AD_push ensure that we detect it and fail. */ if (lstat (dir, &dir_sb)) { error (0, errno, _("cannot lstat %s"), quote (full_filename (dir))); return RM_ERROR; } if (chdir (dir)) { if (! S_ISDIR (dir_sb.st_mode)) { /* This happens on Linux-2.4.18 when a non-privileged user tries to delete a file that is owned by another user in a directory like /tmp that has the S_ISVTX flag set. */ assert (saved_errno == EPERM); error (0, saved_errno, _("cannot remove %s"), quote (full_filename (dir))); } else { error (0, errno, _("cannot chdir from %s to %s"), quote_n (0, full_filename (".")), quote_n (1, dir)); } return RM_ERROR; } AD_push (dir, &dir_sb); status = RM_OK; while (1) { char *subdir = NULL; struct stat subdir_sb; enum RM_status tmp_status = remove_cwd_entries (&subdir, &subdir_sb, x); if (tmp_status != RM_OK) { UPDATE_STATUS (status, tmp_status); AD_mark_current_as_unremovable (); } if (subdir) { AD_push (subdir, &subdir_sb); free (subdir); continue; } /* Execution reaches this point when we've removed the last removable entry from the current directory. */ { char *d = AD_pop_and_chdir (); /* Try to remove D only if remove_cwd_entries succeeded. */ if (tmp_status == RM_OK) { /* This does a little more work than necessary when it actually prompts the user. E.g., we already know that D is a directory and that it's almost certainly empty, yet we lstat it. But that's no big deal since we're interactive. */ Ternary is_dir; Ternary is_empty; enum RM_status s = prompt (d, x, PA_REMOVE_DIR, &is_dir, &is_empty); if (s != RM_OK) { free (d); return s; } if (rmdir (d) == 0) { if (x->verbose) printf (_("removed directory: %s\n"), quote (full_filename (d))); } else { error (0, errno, _("cannot remove directory %s"), quote (full_filename (d))); AD_mark_as_unremovable (d); status = RM_ERROR; UPDATE_STATUS (AD_stack_top()->status, status); } } free (d); if (AD_stack_height () == 1) break; } } return status;}/* Remove the file or directory specified by FILENAME. Return RM_OK if it is removed, and RM_ERROR or RM_USER_DECLINED if not. On input, the first time this function is called, CWD_STATE should be the address of a NULL pointer. Do not modify it for any subsequent calls. On output, it is either that same NULL pointer or the address of a malloc'd `struct saved_cwd' that may be freed. */static enum RM_statusrm_1 (char const *filename, struct rm_options const *x, struct saved_cwd **cwd_state){ char *base = base_name (filename); enum RM_status status; if (DOT_OR_DOTDOT (base)) { error (0, 0, _("cannot remove `.' or `..'")); return RM_ERROR; } status = remove_entry (filename, x, NULL); if (status != RM_NONEMPTY_DIR) return status; return remove_dir (filename, cwd_state, x);}/* Remove all files and/or directories specified by N_FILES and FILE. Apply the options in X. */enum RM_statusrm (size_t n_files, char const *const *file, struct rm_options const *x){ struct saved_cwd *cwd_state = NULL; enum RM_status status = RM_OK; size_t i; obstack_init (&dir_stack); obstack_init (&len_stack); obstack_init (&Active_dir); for (i = 0; i < n_files; i++) { enum RM_status s = rm_1 (file[i], x, &cwd_state); assert (VALID_STATUS (s)); UPDATE_STATUS (status, s); } obstack_free (&dir_stack, NULL); obstack_free (&len_stack, NULL); obstack_free (&Active_dir, NULL); XFREE (cwd_state); return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -