public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin/main] Cygwin: opendir(3): move ENOTDIR check into main function
@ 2024-03-20 13:49 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2024-03-20 13:49 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=80f722e97cf79b6ce64b2d665e059c5b7e15d416

commit 80f722e97cf79b6ce64b2d665e059c5b7e15d416
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Wed Mar 20 12:34:51 2024 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Wed Mar 20 12:34:51 2024 +0100

    Cygwin: opendir(3): move ENOTDIR check into main function
    
    So far the check for a directory is in the fhandler::opendir
    methods. Given that path_conv::check sets FILE_ATTRIBUTE_DIRECTORY
    on virtual files either, we can move the check up into the
    opendir(3) function. This avoids calling exists() twice when
    calling opendir(3).
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/dir.cc                | 11 ++++++++---
 winsup/cygwin/fhandler/disk_file.cc |  4 +---
 winsup/cygwin/fhandler/virtual.cc   |  4 +---
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/winsup/cygwin/dir.cc b/winsup/cygwin/dir.cc
index c30ed74d32b9..df0749197dbf 100644
--- a/winsup/cygwin/dir.cc
+++ b/winsup/cygwin/dir.cc
@@ -61,13 +61,18 @@ opendir (const char *name)
       set_errno (fh->error ());
       res = NULL;
     }
-  else if (fh->exists ())
-    res = fh->opendir (-1);
-  else
+  else if (!fh->exists ())
     {
       set_errno (ENOENT);
       res = NULL;
     }
+  else if (!fh->pc.isdir ())
+    {
+      set_errno (ENOTDIR);
+      res = NULL;
+    }
+  else
+    res = fh->opendir (-1);
 
   if (!res && fh)
     delete fh;
diff --git a/winsup/cygwin/fhandler/disk_file.cc b/winsup/cygwin/fhandler/disk_file.cc
index c5b78984d72a..f1ac29d329ac 100644
--- a/winsup/cygwin/fhandler/disk_file.cc
+++ b/winsup/cygwin/fhandler/disk_file.cc
@@ -2156,9 +2156,7 @@ fhandler_disk_file::opendir (int fd)
   DIR *dir;
   DIR *res = NULL;
 
-  if (!pc.isdir ())
-    set_errno (ENOTDIR);
-  else if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
+  if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
     set_errno (ENOMEM);
   else if ((dir->__d_dirname = (char *) malloc ( sizeof (struct __DIR_cache)))
 	   == NULL)
diff --git a/winsup/cygwin/fhandler/virtual.cc b/winsup/cygwin/fhandler/virtual.cc
index 21ff4f35ead1..4a0d29489e13 100644
--- a/winsup/cygwin/fhandler/virtual.cc
+++ b/winsup/cygwin/fhandler/virtual.cc
@@ -45,9 +45,7 @@ fhandler_virtual::opendir (int fd)
   DIR *res = NULL;
   size_t len;
 
-  if (!virt_ftype_isdir (exists ()))
-    set_errno (ENOTDIR);
-  else if ((len = strlen (get_name ())) > PATH_MAX - 3)
+  if ((len = strlen (get_name ())) > PATH_MAX - 3)
     set_errno (ENAMETOOLONG);
   else if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
     set_errno (ENOMEM);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-20 13:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-20 13:49 [newlib-cygwin/main] Cygwin: opendir(3): move ENOTDIR check into main function Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).