This is the mail archive of the cygwin-patches mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Patch] Add dirent.d_type support to Cygwin 1.7 ?


Corinna Vinschen wrote:
The logic sounds ok to me. I just don't think we need a warning and the condition could be simplified accordingly.


New patch below. Conditionals removed as suggested by cgf.


Define of _DIRENT_HAVE_D_TYPE still there - google code search finds several projects using this instead of a ./configure check.


2008-11-27 Christian Franke <franke@computer.org>


	* dir.cc (readdir_worker): Initialize dirent.d_type and __d_unused1.
	* fhandler_disk_file.cc (fhandler_disk_file::readdir_helper):
	Set dirent.d_type based on FILE_ATTRIBUTE_*.
	* include/sys/dirent.h: Define _DIRENT_HAVE_D_TYPE.
	(struct dirent): Add d_type. Adjust __d_unused1 size to preserve layout.
	[_DIRENT_HAVE_D_TYPE]: Enable DT_* declarations.



Christian


PS: find is not as smart as expected: 'find /path -type d' calls lstat() for each entry, even if d_type != DT_UNKNOWN.
So 'find /path' is 2-3 times faster than 'find /path -type d'.


diff --git a/winsup/cygwin/dir.cc b/winsup/cygwin/dir.cc
index 30662e6..2b9125f 100644
--- a/winsup/cygwin/dir.cc
+++ b/winsup/cygwin/dir.cc
@@ -93,6 +93,9 @@ readdir_worker (DIR *dir, dirent *de)
     }
 
   de->d_ino = 0;
+  de->d_type = DT_UNKNOWN;
+  memset (&de->__d_unused1, 0, sizeof (de->__d_unused1));
+
   int res = ((fhandler_base *) dir->__fh)->readdir (dir, de);
 
   if (res == ENMFILE)
diff --git a/winsup/cygwin/fhandler_disk_file.cc b/winsup/cygwin/fhandler_disk_file.cc
index c388a13..ac7ee2e 100644
--- a/winsup/cygwin/fhandler_disk_file.cc
+++ b/winsup/cygwin/fhandler_disk_file.cc
@@ -1677,6 +1677,20 @@ fhandler_disk_file::readdir_helper (DIR *dir, dirent *de, DWORD w32_err,
       dir->__flags &= ~dirent_set_d_ino;
     }
 
+  /* Set d_type if type can be determined from file attributes.
+     FILE_ATTRIBUTE_SYSTEM ommitted to leave DT_UNKNOWN for old symlinks.
+     For new symlinks, d_type will be reset to DT_UNKNOWN below.  */
+  if (attr &&
+      !(attr & (  ~FILE_ATTRIBUTE_VALID_FLAGS
+		| FILE_ATTRIBUTE_SYSTEM
+		| FILE_ATTRIBUTE_REPARSE_POINT)))
+    {
+      if (attr & FILE_ATTRIBUTE_DIRECTORY)
+	de->d_type = DT_DIR;
+      else
+	de->d_type = DT_REG;
+    }
+
   /* Check for directory reparse point.  These are potential volume mount
      points which have another inode than the underlying directory. */
   if ((attr & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT))
@@ -1728,7 +1742,10 @@ fhandler_disk_file::readdir_helper (DIR *dir, dirent *de, DWORD w32_err,
 	    }
 	  path_conv fpath (&fbuf, PC_SYM_NOFOLLOW);
 	  if (fpath.issymlink () || fpath.is_fs_special ())
-	    fname->Length -= 4 * sizeof (WCHAR);
+	    {
+	      fname->Length -= 4 * sizeof (WCHAR);
+	      de->d_type = DT_UNKNOWN;
+	    }
 	}
     }
 
diff --git a/winsup/cygwin/include/sys/dirent.h b/winsup/cygwin/include/sys/dirent.h
index 41bfcc1..451c802 100644
--- a/winsup/cygwin/include/sys/dirent.h
+++ b/winsup/cygwin/include/sys/dirent.h
@@ -18,11 +18,13 @@
 
 #pragma pack(push,4)
 #if defined(__INSIDE_CYGWIN__) || defined (__CYGWIN_USE_BIG_TYPES__)
+#define _DIRENT_HAVE_D_TYPE
 struct dirent
 {
   long __d_version;			/* Used internally */
   __ino64_t d_ino;
-  __uint32_t __d_unused1;
+  unsigned char d_type;
+  unsigned char __d_unused1[3];
   __uint32_t __d_internal1;
   char d_name[NAME_MAX + 1];
 };
@@ -77,7 +79,7 @@ int scandir (const char *__dir,
 	     int (*compar) (const struct dirent **, const struct dirent **));
 
 int alphasort (const struct dirent **__a, const struct dirent **__b);
-#if 0  /* these make no sense in the absence of d_type */
+#ifdef _DIRENT_HAVE_D_TYPE
 /* File types for `d_type'.  */
 enum
 {
@@ -104,6 +106,6 @@ enum
 /* Convert between stat structure types and directory types.  */
 # define IFTODT(mode)		(((mode) & 0170000) >> 12)
 # define DTTOIF(dirtype)        ((dirtype) << 12)
-#endif /* #if 0 */
+#endif /* _DIRENT_HAVE_D_TYPE */
 #endif /* _POSIX_SOURCE */
 #endif /*_SYS_DIRENT_H*/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]