From b1897d27a8be9c830f5fc30d7a91b754ad0de6fd Mon Sep 17 00:00:00 2001
From: Corinna Vinschen <corinna@vinschen.de>
Date: Wed, 19 Mar 2003 21:34:38 +0000
Subject: [PATCH] 	* fhandler_disk_file.cc
 (fhandler_disk_file::fstat_helper): Fix 	wrong usage of S_IFDIR. 
 * security.cc (get_attribute_from_acl): Ditto. 	(get_file_attribute):
 Fix wrong usage of S_IFLNK. 	(get_object_attribute): Ditto. 	(alloc_sd):
 Fix wrong usage of S_IFDIR. 	* syscalls.cc (chmod): Allow chmod'ing of
 socket files.

---
 winsup/cygwin/ChangeLog             | 10 ++++++++++
 winsup/cygwin/fhandler_disk_file.cc |  2 +-
 winsup/cygwin/security.cc           | 25 +++++++++++++------------
 winsup/cygwin/syscalls.cc           |  2 +-
 4 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 994e9c58f..54121d7dd 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,13 @@
+2003-03-19  Corinna Vinschen  <corinna@vinschen.de>
+
+	* fhandler_disk_file.cc (fhandler_disk_file::fstat_helper): Fix
+	wrong usage of S_IFDIR.
+	* security.cc (get_attribute_from_acl): Ditto.
+	(get_file_attribute): Fix wrong usage of S_IFLNK.
+	(get_object_attribute): Ditto.
+	(alloc_sd): Fix wrong usage of S_IFDIR.
+	* syscalls.cc (chmod): Allow chmod'ing of socket files.
+
 2003-03-19  Corinna Vinschen  <corinna@vinschen.de>
 
 	* include/cygwin/version.h (CYGWIN_VERSION_CHECK_FOR_USING_BIG_TYPES):
diff --git a/winsup/cygwin/fhandler_disk_file.cc b/winsup/cygwin/fhandler_disk_file.cc
index 96428d1c4..66679d972 100644
--- a/winsup/cygwin/fhandler_disk_file.cc
+++ b/winsup/cygwin/fhandler_disk_file.cc
@@ -305,7 +305,7 @@ fhandler_disk_file::fstat_helper (struct __stat64 *buf, path_conv *pc,
 	buf->st_mode |= STD_WBITS;
       /* | S_IWGRP | S_IWOTH; we don't give write to group etc */
 
-      if (buf->st_mode & S_IFDIR)
+      if (S_ISDIR (buf->st_mode))
 	buf->st_mode |= S_IFDIR | STD_XBITS;
       else if (buf->st_mode & S_IFMT)
 	/* nothing */;
diff --git a/winsup/cygwin/security.cc b/winsup/cygwin/security.cc
index a4fdf8426..0c9e24cb5 100644
--- a/winsup/cygwin/security.cc
+++ b/winsup/cygwin/security.cc
@@ -1267,7 +1267,7 @@ get_attribute_from_acl (mode_t *attribute, PACL acl, PSID owner_sid,
 	    *flags |= ((!(*anti & S_IXOTH)) ? S_IXOTH : 0)
 		      | ((!(*anti & S_IXGRP)) ? S_IXGRP : 0)
 		      | ((!(*anti & S_IXUSR)) ? S_IXUSR : 0);
-	  if ((*attribute & S_IFDIR) &&
+	  if ((S_ISDIR (*attribute)) &&
 	      (ace->Mask & (FILE_WRITE_DATA | FILE_EXECUTE | FILE_DELETE_CHILD))
 	      == (FILE_WRITE_DATA | FILE_EXECUTE))
 	    *flags |= S_ISVTX;
@@ -1404,7 +1404,7 @@ get_file_attribute (int use_ntsec, const char *file,
 	  if (gidret)
 	    *gidret = ILLEGAL_GID;
 	}
-      else if (attribute && (*attribute & S_IFLNK) == S_IFLNK)
+      else if (attribute && S_ISLNK (*attribute))
 	*attribute |= S_IRWXU | S_IRWXG | S_IRWXO;
       return 0;
     }
@@ -1427,7 +1427,7 @@ get_file_attribute (int use_ntsec, const char *file,
     res = 0;
 
   /* symlinks are everything for everyone! */
-  if ((*attribute & S_IFLNK) == S_IFLNK)
+  if (S_ISLNK (*attribute))
     *attribute |= S_IRWXU | S_IRWXG | S_IRWXO;
 
   return res > 0 ? 0 : -1;
@@ -1498,7 +1498,7 @@ get_object_attribute (HANDLE handle, SE_OBJECT_TYPE object_type,
     {
       int res = get_nt_object_attribute (handle, object_type, attribute,
 					 uidret, gidret);
-      if (attribute && (*attribute & S_IFLNK) == S_IFLNK)
+      if (attribute && S_ISLNK (*attribute))
 	*attribute |= S_IRWXU | S_IRWXG | S_IRWXO;
       return res;
     }
@@ -1512,7 +1512,7 @@ get_object_attribute (HANDLE handle, SE_OBJECT_TYPE object_type,
     return 0;
 
   /* symlinks are everything for everyone! */
-  if ((*attribute & S_IFLNK) == S_IFLNK)
+  if (S_ISLNK (*attribute))
     *attribute |= S_IRWXU | S_IRWXG | S_IRWXO;
 
   return 0;
@@ -1652,8 +1652,8 @@ alloc_sd (__uid32_t uid, __gid32_t gid, int attribute,
     owner_allow |= FILE_GENERIC_WRITE;
   if (attribute & S_IXUSR)
     owner_allow |= FILE_GENERIC_EXECUTE;
-  if ((attribute & (S_IFDIR | S_IWUSR | S_IXUSR))
-      == (S_IFDIR | S_IWUSR | S_IXUSR))
+  if (S_ISDIR (attribute)
+      && (attribute & (S_IWUSR | S_IXUSR)) == (S_IWUSR | S_IXUSR))
     owner_allow |= FILE_DELETE_CHILD;
 
   /* Construct allow attribute for group. */
@@ -1665,8 +1665,9 @@ alloc_sd (__uid32_t uid, __gid32_t gid, int attribute,
     group_allow |= STANDARD_RIGHTS_WRITE | FILE_GENERIC_WRITE;
   if (attribute & S_IXGRP)
     group_allow |= FILE_GENERIC_EXECUTE;
-  if ((attribute & (S_IFDIR | S_IWGRP | S_IXGRP))
-      == (S_IFDIR | S_IWGRP | S_IXGRP) && !(attribute & S_ISVTX))
+  if (S_ISDIR (attribute)
+      && (attribute & (S_IWGRP | S_IXGRP)) == (S_IWGRP | S_IXGRP)
+      && !(attribute & S_ISVTX))
     group_allow |= FILE_DELETE_CHILD;
 
   /* Construct allow attribute for everyone. */
@@ -1678,8 +1679,8 @@ alloc_sd (__uid32_t uid, __gid32_t gid, int attribute,
     other_allow |= STANDARD_RIGHTS_WRITE | FILE_GENERIC_WRITE;
   if (attribute & S_IXOTH)
     other_allow |= FILE_GENERIC_EXECUTE;
-  if ((attribute & (S_IFDIR | S_IWOTH | S_IXOTH))
-      == (S_IFDIR | S_IWOTH | S_IXOTH)
+  if (S_ISDIR (attribute)
+      && (attribute & (S_IWOTH | S_IXOTH)) == (S_IWOTH | S_IXOTH)
       && !(attribute & S_ISVTX))
     other_allow |= FILE_DELETE_CHILD;
 
@@ -1788,7 +1789,7 @@ alloc_sd (__uid32_t uid, __gid32_t gid, int attribute,
 	}
 
   /* Construct appropriate inherit attribute for new directories */
-  if (attribute & S_IFDIR && !acl_exists )
+  if (S_ISDIR (attribute) && !acl_exists )
     {
       const DWORD inherit = SUB_CONTAINERS_AND_OBJECTS_INHERIT | INHERIT_ONLY;
 
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index c59a5fcf3..61bb08646 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -904,7 +904,7 @@ chmod (const char *path, mode_t mode)
 
   /* FIXME: This makes chmod on a device succeed always.  Someday we'll want
      to actually allow chmod to work properly on devices. */
-  if (win32_path.is_device ())
+  if (win32_path.is_device () && !win32_path.issocket ())
     {
       res = 0;
       goto done;