Files
coreutils/src/remove.h
T
Paul Eggert eb7841426c rm: fix diagnostics on I/O error
I ran into this problem when attempting to recursively
remove a directory in a filesystem on flaky hardware.
Although the underlying readdir syscall failed with errno == EIO,
rm issued no diagnostic about the I/O error.

Without this patch I see this behavior:

  $ rm -fr baddir
  rm: cannot remove 'baddir': Directory not empty
  $ rm -ir baddir
  rm: descend into directory 'baddir'? y
  rm: remove directory 'baddir'? y
  rm: cannot remove 'baddir': Directory not empty

With this patch I see the following behavior, which
lets the user know about the I/O error when rm tries
to read baddir's directory entries:

  $ rm -fr baddir
  rm: cannot remove 'baddir': Input/output error
  $ rm -ir baddir
  rm: cannot remove 'baddir': Input/output error

* src/remove.c (Ternary): Remove.  All uses removed.
(get_dir_status): New static function.
(prompt): Last arg is now directory status, not ternary.
Return RM_USER_ACCEPTED if user explicitly accepted.
All uses changed.
Report any significant error in directory status right away.
(prompt, rm_fts): Use get_dir_status to get directory status lazily.
(excise): Treat any FTS_DNR errno as being more descriptive, not
just EPERM and EACCESS.  For example, EIO is more descriptive.
(rm_fts): Distinguish more clearly between explicit and implied
user OK.
* src/remove.h (RM_USER_ACCEPTED): New constant.
(VALID_STATUS): Treat it as valid.
* src/system.h (is_empty_dir): Remove, replacing with ...
(directory_status): ... this more-general function.
All uses changed.  Avoid undefined behavior of looking at
a non-null readdir pointer after corresponding closedir.
* tests/rm/rm-readdir-fail.sh: Adjust test of internals
to match current behavior.
2022-09-24 16:34:31 -07:00

104 lines
3.2 KiB
C

/* Remove directory entries.
Copyright (C) 1998-2022 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#ifndef REMOVE_H
# define REMOVE_H
# include "dev-ino.h"
enum rm_interactive
{
/* Start with any number larger than 1, so that any legacy tests
against values of 0 or 1 will fail. */
RMI_ALWAYS = 3,
RMI_SOMETIMES,
RMI_NEVER
};
struct rm_options
{
/* If true, ignore nonexistent files. */
bool ignore_missing_files;
/* If true, query the user about whether to remove each file. */
enum rm_interactive interactive;
/* FIXME: remove */
/* If true, do not traverse into (or remove) any directory that is
on a file system (i.e., that has a different device number) other
than that of the corresponding command line argument. Note that
even without this option, rm will fail in the end, due to its
probable inability to remove the mount point. But there, the
diagnostic comes too late -- after removing all contents. */
bool one_file_system;
/* If true, recursively remove directories. */
bool recursive;
/* If true, remove empty directories. */
bool remove_empty_directories;
/* Pointer to the device and inode numbers of '/', when --recursive
and preserving '/'. Otherwise NULL. */
struct dev_ino *root_dev_ino;
/* If true, do not traverse into (or remove) any directory that is
the root of a file system. I.e., a separate device. */
bool preserve_all_root;
/* If nonzero, stdin is a tty. */
bool stdin_tty;
/* If true, display the name of each file removed. */
bool verbose;
/* If true, treat the failure by the rm function to restore the
current working directory as a fatal error. I.e., if this field
is true and the rm function cannot restore cwd, it must exit with
a nonzero status. Some applications require that the rm function
restore cwd (e.g., mv) and some others do not (e.g., rm,
in many cases). */
bool require_restore_cwd;
};
enum RM_status
{
/* These must be listed in order of increasing seriousness. */
RM_OK = 2,
RM_USER_ACCEPTED,
RM_USER_DECLINED,
RM_ERROR,
RM_NONEMPTY_DIR
};
# define VALID_STATUS(S) \
((S) == RM_OK || (S) == RM_USER_ACCEPTED || (S) == RM_USER_DECLINED \
|| (S) == RM_ERROR)
# define UPDATE_STATUS(S, New_value) \
do \
{ \
if ((New_value) == RM_ERROR \
|| ((New_value) == RM_USER_DECLINED && (S) == RM_OK)) \
(S) = (New_value); \
} \
while (0)
extern enum RM_status rm (char *const *file, struct rm_options const *x);
#endif