https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=80f722e97cf79b6ce64b2d665e059c5b7e15d416
commit 80f722e97cf79b6ce64b2d665e059c5b7e15d416 Author: Corinna Vinschen <[email protected]> AuthorDate: Wed Mar 20 12:34:51 2024 +0100 Commit: Corinna Vinschen <[email protected]> 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 <[email protected]> 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);
