From 358063ace374e05025d124a17a0167b818b666ea Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Fri, 19 Aug 2005 16:18:42 +0000 Subject: [PATCH] * fhandler.h (dirent_states): Add dirent_isroot, dirent_saw_cygdrive, dirent_saw_dev. * dir.cc (opendir): Don't zero __flags here. Push that responsibility to opendir methods. (seekdir): Preserve dirent_isrrot in __flags. (rewinddir): Ditto. * fhandler_disk_file.cc (fhandler_disk_file::opendir): Set dirent_isroot appropriately. (fhandler_disk_file::readdir): Fill in "cygdrive" and "dev" if it is the root dir and they are missing. * fhandler_process.cc (fhandler_process::opendir): Set __flags here. * fhandler_virtual.cc (fhandler_virtual::opendir): Set __flags here. --- winsup/cygwin/ChangeLog | 15 ++++++++ winsup/cygwin/dir.cc | 6 +-- winsup/cygwin/fhandler.h | 11 ++++-- winsup/cygwin/fhandler_disk_file.cc | 57 +++++++++++++++++++++++------ winsup/cygwin/fhandler_process.cc | 8 +++- winsup/cygwin/fhandler_virtual.cc | 1 + 6 files changed, 77 insertions(+), 21 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 19092a3e7..a19029bb0 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,18 @@ +2005-08-19 Christopher Faylor + + * fhandler.h (dirent_states): Add dirent_isroot, dirent_saw_cygdrive, + dirent_saw_dev. + * dir.cc (opendir): Don't zero __flags here. Push that responsibility + to opendir methods. + (seekdir): Preserve dirent_isrrot in __flags. + (rewinddir): Ditto. + * fhandler_disk_file.cc (fhandler_disk_file::opendir): Set + dirent_isroot appropriately. + (fhandler_disk_file::readdir): Fill in "cygdrive" and "dev" if it is + the root dir and they are missing. + * fhandler_process.cc (fhandler_process::opendir): Set __flags here. + * fhandler_virtual.cc (fhandler_virtual::opendir): Set __flags here. + 2005-08-19 Christopher Faylor * winsup.h (create_pipe): Declare new function. diff --git a/winsup/cygwin/dir.cc b/winsup/cygwin/dir.cc index 254906a6e..894691d8f 100644 --- a/winsup/cygwin/dir.cc +++ b/winsup/cygwin/dir.cc @@ -59,7 +59,7 @@ opendir (const char *name) } if (res) - res->__flags = 0; + /* nothing */; else if (fh) delete fh; return res; @@ -169,7 +169,7 @@ seekdir64 (DIR *dir, _off64_t loc) if (dir->__d_cookie != __DIRENT_COOKIE) return; - dir->__flags = 0; + dir->__flags &= dirent_isroot; return ((fhandler_base *) dir->__fh)->seekdir (dir, loc); } @@ -190,7 +190,7 @@ rewinddir (DIR *dir) if (dir->__d_cookie != __DIRENT_COOKIE) return; - dir->__flags = 0; + dir->__flags &= dirent_isroot; return ((fhandler_base *) dir->__fh)->rewinddir (dir); } diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index ce0bef7d7..d8694c809 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -40,10 +40,13 @@ struct __acl32; enum dirent_states { - dirent_ok = 0, - dirent_saw_dot = 1, - dirent_saw_dot_dot = 2, - dirent_saw_eof = 4 + dirent_ok = 0x0000, + dirent_saw_dot = 0x0001, + dirent_saw_dot_dot = 0x0002, + dirent_saw_eof = 0x0004, + dirent_isroot = 0x0008, + dirent_saw_cygdrive = 0x0010, + dirent_saw_dev = 0x0020 }; enum conn_state diff --git a/winsup/cygwin/fhandler_disk_file.cc b/winsup/cygwin/fhandler_disk_file.cc index 3b3388c6f..e0ef7079a 100644 --- a/winsup/cygwin/fhandler_disk_file.cc +++ b/winsup/cygwin/fhandler_disk_file.cc @@ -1287,6 +1287,7 @@ fhandler_disk_file::opendir () DIR *dir; DIR *res = NULL; size_t len; + path_conv rootdir ("/"); if (!pc.isdir ()) set_errno (ENOTDIR); @@ -1332,7 +1333,7 @@ fhandler_disk_file::opendir () dir->__d_dirhash = get_namehash (); res = dir; - + dir->__flags = strcasematch (pc, rootdir) ? dirent_isroot : 0; } syscall_printf ("%p = opendir (%s)", res, get_name ()); @@ -1370,15 +1371,36 @@ fhandler_disk_file::readdir (DIR *dir) return res; else if (!FindNextFileA (dir->__handle, &buf)) { - DWORD lasterr = GetLastError (); - FindClose (dir->__handle); - dir->__handle = INVALID_HANDLE_VALUE; - /* POSIX says you shouldn't set errno when readdir can't - find any more files; so, if another error we leave it set. */ - if (lasterr != ERROR_NO_MORE_FILES) - seterrno_from_win_error (__FILE__, __LINE__, lasterr); - syscall_printf ("%p = readdir (%p)", res, dir); - return res; + bool added = false; + if (!(dir->__flags & dirent_isroot)) + /* nothing */; + else if (!(dir->__flags & dirent_saw_dev)) + { + strcpy (buf.cFileName, "dev"); + added = true; + } + else if (!(dir->__flags & dirent_saw_cygdrive) + && mount_table->cygdrive_len > 1) + { + strcpy (buf.cFileName, mount_table->cygdrive + 1); + buf.cFileName[mount_table->cygdrive_len - 2] = '\0'; + added = true; + } + + if (added) + buf.dwFileAttributes = 0; + else + { + DWORD lasterr = GetLastError (); + FindClose (dir->__handle); + dir->__handle = INVALID_HANDLE_VALUE; + /* POSIX says you shouldn't set errno when readdir can't + find any more files; so, if another error we leave it set. */ + if (lasterr != ERROR_NO_MORE_FILES) + seterrno_from_win_error (__FILE__, __LINE__, lasterr); + syscall_printf ("%p = readdir (%p)", res, dir); + return res; + } } /* Check for Windows shortcut. If it's a Cygwin or U/WIN @@ -1403,11 +1425,22 @@ fhandler_disk_file::readdir (DIR *dir) fnunmunge (dir->__d_dirent->d_name, buf.cFileName); else strcpy (dir->__d_dirent->d_name, buf.cFileName); + if (!(dir->__flags && dirent_isroot)) + /* nothing */; + else + { + if (strcasematch (dir->__d_dirent->d_name, "dev")) + dir->__flags |= dirent_saw_dev; + if (strlen (dir->__d_dirent->d_name) == mount_table->cygdrive_len - 2 + && strncasematch (dir->__d_dirent->d_name, mount_table->cygdrive + 1, + mount_table->cygdrive_len - 2)) + dir->__flags |= dirent_saw_cygdrive; + } dir->__d_position++; res = dir->__d_dirent; - syscall_printf ("%p = readdir (%p) (%s)", - &dir->__d_dirent, dir, buf.cFileName); + syscall_printf ("%p = readdir (%p) (%s)", &dir->__d_dirent, dir, + buf.cFileName); return res; } diff --git a/winsup/cygwin/fhandler_process.cc b/winsup/cygwin/fhandler_process.cc index 127c91e51..7cc53302f 100644 --- a/winsup/cygwin/fhandler_process.cc +++ b/winsup/cygwin/fhandler_process.cc @@ -200,8 +200,12 @@ DIR * fhandler_process::opendir () { DIR *dir = fhandler_virtual::opendir (); - if (dir && fileid == PROCESS_FD) - fill_filebuf (); + if (dir) + { + if (fileid == PROCESS_FD) + fill_filebuf (); + dir->__flags = 0; + } return dir; } diff --git a/winsup/cygwin/fhandler_virtual.cc b/winsup/cygwin/fhandler_virtual.cc index a052c4bcd..9afc1d760 100644 --- a/winsup/cygwin/fhandler_virtual.cc +++ b/winsup/cygwin/fhandler_virtual.cc @@ -84,6 +84,7 @@ fhandler_virtual::opendir () dir->__d_dirhash = get_namehash (); dir->__flags = dirent_saw_dot | dirent_saw_dot_dot; res = dir; + res->__flags = 0; } }