From d04604664a580f2805fe4111592467bb656d71f6 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sat, 25 Jul 1998 15:27:55 +0000 Subject: [PATCH] (same_file): New function (remove_dir): Use it to give a better diagnostic when rmdir fails because it can't remove the current directory. --- src/remove.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/remove.c b/src/remove.c index 6d5a61b33a..995e931859 100644 --- a/src/remove.c +++ b/src/remove.c @@ -412,6 +412,16 @@ fspec_filetype_mode (const struct File_spec *fs) return fs->mode; } +static int +same_file (const char *file_1, const char *file_2) +{ + struct stat sb1, sb2; + return (lstat (file_1, &sb1) == 0 + && lstat (file_2, &sb2) == 0 + && SAME_INODE (sb1, sb2)); +} + + /* Recursively remove all of the entries in the current directory. Return an indication of the success of the operation. */ @@ -735,8 +745,24 @@ remove_dir (struct File_spec *fs, int need_save_cwd, const struct rm_options *x) if (rmdir (dir_name) && (errno != ENOENT || !x->ignore_missing_files)) { - error (0, errno, _("cannot remove directory `%s'"), - full_filename (dir_name)); + int saved_errno = errno; + +#ifndef EINVAL +# define EINVAL 0 +#endif + /* See if rmdir just failed because DIR_NAME is the current directory. + If so, give a better diagnostic than `rm: cannot remove directory + `...': Invalid argument' */ + if (errno == EINVAL && same_file (".", dir_name)) + { + error (0, 0, _("cannot remove current directory `%s'"), + full_filename (dir_name)); + } + else + { + error (0, saved_errno, _("cannot remove directory `%s'"), + full_filename (dir_name)); + } return RM_ERROR; } -- 2.47.3