Browse Source

Fix nftw when called with FTW_CHDIR and FTW_DEPTH

Change directory back to the parent before processing
the directory (after the contents have already been processed).

Signed-off-by: John Ata <john.ata@baesystems.com>
John Ata 7 years ago
parent
commit
a6cdfdfff3
1 changed files with 6 additions and 7 deletions
  1. 6 7
      libc/misc/ftw/ftw.c

+ 6 - 7
libc/misc/ftw/ftw.c

@@ -558,19 +558,14 @@ fail:
   --data->ftw.level;
   data->ftw.base = previous_base;
 
-  /* Finally, if we process depth-first report the directory.  */
-  if (result == 0 && (data->flags & FTW_DEPTH))
-    result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
-
-  if (old_dir
-      && (data->flags & FTW_CHDIR)
+  if ((data->flags & FTW_CHDIR)
       && (result == 0
 	  || ((data->flags & FTW_ACTIONRETVAL)
 	      && (result != -1 && result != FTW_STOP))))
     {
       /* Change back to the parent directory.  */
       int done = 0;
-      if (old_dir->stream != NULL)
+      if (old_dir && old_dir->stream != NULL)
 	if (__fchdir (dirfd (old_dir->stream)) == 0)
 	  done = 1;
 
@@ -587,6 +582,10 @@ fail:
 	}
     }
 
+  /* Finally, if we process depth-first report the directory.  */
+  if (result == 0 && (data->flags & FTW_DEPTH))
+    result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
+
   return result;
 }