fts: make directory fds more robust
[pspp] / lib / fts.c
index fdbe2586c6dd1d2f91ed3fa507af088ad925a1ae..ebf66fc45f9f46c5d53ebe27cd29037a6bd97df6 100644 (file)
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -69,7 +69,7 @@ static char sccsid[] = "@(#)fts.c     8.6 (Berkeley) 8/14/94";
 
 #if ! _LIBC
 # include "fcntl--.h"
-# include "openat.h"
+# include "dirent--.h"
 # include "unistd--.h"
 # include "same-inode.h"
 #endif
@@ -228,10 +228,6 @@ static void free_dir (FTS *fts) {}
 # define SIZE_MAX ((size_t) -1)
 #endif
 
-#ifndef O_DIRECTORY
-# define O_DIRECTORY 0
-#endif
-
 #define ISDOT(a)       (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
 #define STREQ(a, b)    (strcmp ((a), (b)) == 0)
 
@@ -309,7 +305,8 @@ static inline DIR *
 internal_function
 opendirat (int fd, char const *dir)
 {
-  int new_fd = openat (fd, dir, O_RDONLY);
+  int new_fd = openat (fd, dir,
+                      O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
   DIR *dirp;
 
   if (new_fd < 0)
@@ -606,20 +603,28 @@ 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);
 
+#if GNULIB_FTS
        if (sp->fts_leaf_optimization_works_ht)
          hash_free (sp->fts_leaf_optimization_works_ht);
+#endif
 
        free_dir (sp);
 
@@ -717,6 +722,7 @@ static bool dirent_inode_sort_may_be_useful (int dir_fd) { return true; }
 static bool leaf_optimization_applies (int dir_fd) { return false; }
 #endif
 
+#if GNULIB_FTS
 /* link-count-optimization entry:
    map an stat.st_dev number to a boolean: leaf_optimization_works */
 struct LCO_ent
@@ -798,6 +804,7 @@ link_count_optimize_ok (FTSENT const *p)
 
   return opt_ok;
 }
+#endif
 
 /*
  * Special case of "/" at the end of the file name so that slashes aren't
@@ -928,7 +935,9 @@ next:       tmp = p;
                                SET(FTS_STOP);
                                return (NULL);
                        }
+                       free_dir(sp);
                        fts_load(sp, p);
+                       setup_dir(sp);
                        goto check_for_dir;
                }
 
@@ -961,7 +970,10 @@ check_for_dir:
                    if (p->fts_statp->st_size == FTS_STAT_REQUIRED)
                      {
                        FTSENT *parent = p->fts_parent;
-                       if (parent->fts_n_dirs_remaining == 0
+                       if (FTS_ROOTLEVEL < p->fts_level
+                           /* ->fts_n_dirs_remaining is not valid
+                              for command-line-specified names.  */
+                           && parent->fts_n_dirs_remaining == 0
                            && ISSET(FTS_NOSTAT)
                            && ISSET(FTS_PHYSICAL)
                            && link_count_optimize_ok (parent))
@@ -971,11 +983,10 @@ check_for_dir:
                        else
                          {
                            p->fts_info = fts_stat(sp, p, false);
-                           if (S_ISDIR(p->fts_statp->st_mode))
-                             {
-                               if (parent->fts_n_dirs_remaining)
+                           if (S_ISDIR(p->fts_statp->st_mode)
+                               && p->fts_level != FTS_ROOTLEVEL
+                               && parent->fts_n_dirs_remaining)
                                  parent->fts_n_dirs_remaining--;
-                             }
                          }
                      }
                    else