fts: fts_close now fails also when closing a dir file descriptor fails
authorJim Meyering <meyering@redhat.com>
Tue, 1 Sep 2009 09:18:07 +0000 (11:18 +0200)
committerJim Meyering <meyering@redhat.com>
Tue, 1 Sep 2009 09:28:59 +0000 (11:28 +0200)
* lib/fts.c (fts_close): Detect close failure, not just fchdir failure,
and propagate to caller, along with errno.

ChangeLog
lib/fts.c

index 5ff10df..89cf0ee 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2009-09-01  Jim Meyering  <meyering@redhat.com>
 
+       fts: fts_close now fails also when closing a dir file descriptor fails
+       * lib/fts.c (fts_close): Detect close failure, not just fchdir failure,
+       and propagate to caller, along with errno.
+
        announce-gen: correct formatting in --help output
        * build-aux/announce-gen (usage): Move the one-line description in
        --help output "up", to where it belongs, just after Usage:.
index bbcd1ff..a30e38a 100644 (file)
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -606,14 +606,20 @@ fts_close (FTS *sp)
        if (ISSET(FTS_CWDFD))
          {
            if (0 <= sp->fts_cwd_fd)
-             close (sp->fts_cwd_fd);
+             if (close (sp->fts_cwd_fd))
+               saved_errno = errno;
          }
        else if (!ISSET(FTS_NOCHDIR))
          {
            /* Return to original directory, save errno if necessary. */
            if (fchdir(sp->fts_rfd))
              saved_errno = errno;
-           close(sp->fts_rfd);
+
+           /* If close fails, record errno only if saved_errno is zero,
+              so that we report the probably-more-meaningful fchdir errno.  */
+           if (close (sp->fts_rfd))
+             if (saved_errno == 0)
+               saved_errno = errno;
          }
 
        fd_ring_clear (&sp->fts_fd_ring);