From: Jim Meyering Date: Thu, 29 Aug 2002 09:42:43 +0000 (+0000) Subject: (remove_cwd_entries): Detect and diagnose readdir X-Git-Tag: v4.5.1~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a9e0503a26a2076caa48422cbcc6ae02aeb3721;p=thirdparty%2Fcoreutils.git (remove_cwd_entries): Detect and diagnose readdir failures. On some systems (at least EMC Celerra and Solaris5.8), this appears to be necessary. --- diff --git a/src/remove.c b/src/remove.c index f09184e35c..15ea10d2fe 100644 --- a/src/remove.c +++ b/src/remove.c @@ -810,12 +810,27 @@ remove_cwd_entries (char **subdir, struct stat *subdir_sb, while (1) { - struct dirent *dp = readdir (dirp); + struct dirent *dp; enum RM_status tmp_status; const char *f; - if (dp == NULL) - break; + /* Set errno to zero so we can distinguish between a readdir failure + and when readdir simply finds that there are no more entries. */ + errno = 0; + if ((dp = readdir (dirp)) == NULL) + { + if (errno) + { + /* Save/restore errno across closedir call. */ + int e = errno; + CLOSEDIR (dirp); + errno = e; + + /* Arrange to give a diagnostic after exiting this loop. */ + dirp = NULL; + } + break; + } f = dp->d_name; if (DOT_OR_DOTDOT (f)) @@ -871,8 +886,10 @@ remove_cwd_entries (char **subdir, struct stat *subdir_sb, break; } - if (CLOSEDIR (dirp) != 0) + if (dirp == NULL || CLOSEDIR (dirp) != 0) { + /* Note that this diagnostic serves for both readdir + and closedir failures. */ error (0, errno, _("reading directory %s"), quote (full_filename ("."))); status = RM_ERROR; }