[PATCH 01/11] syscalls.cc: unlink_nt: Try FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE first

Ben Wijen ben@wijen.net
Fri Jan 15 13:45:24 GMT 2021


---
 winsup/cygwin/ntdll.h     |  3 ++-
 winsup/cygwin/syscalls.cc | 20 ++++++++++++++++----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/ntdll.h b/winsup/cygwin/ntdll.h
index d4f6aaf45..7eee383dd 100644
--- a/winsup/cygwin/ntdll.h
+++ b/winsup/cygwin/ntdll.h
@@ -497,7 +497,8 @@ enum {
   FILE_DISPOSITION_DELETE				= 0x01,
   FILE_DISPOSITION_POSIX_SEMANTICS			= 0x02,
   FILE_DISPOSITION_FORCE_IMAGE_SECTION_CHECK		= 0x04,
-  FILE_DISPOSITION_ON_CLOSE				= 0x08
+  FILE_DISPOSITION_ON_CLOSE				= 0x08,
+  FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE		= 0x10,
 };
 
 enum
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 525efecf3..ce4e9c65c 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -709,11 +709,23 @@ _unlink_nt (path_conv &pc, bool shareable)
 			   flags);
       if (!NT_SUCCESS (status))
 	goto out;
-      /* Why didn't the devs add a FILE_DELETE_IGNORE_READONLY_ATTRIBUTE
-	 flag just like they did with FILE_LINK_IGNORE_READONLY_ATTRIBUTE
-	 and FILE_LINK_IGNORE_READONLY_ATTRIBUTE???
+      /* Try FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE first
+         it was added with Redstone 5 (Win10 18_09) (as were POSIX rename semantics)
+         If it fails: fall-back to usual trickery */
+      if (wincap.has_posix_rename_semantics ())
+        {
+          fdie.Flags = FILE_DISPOSITION_DELETE | FILE_DISPOSITION_POSIX_SEMANTICS;
+          fdie.Flags|= FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE;
+          status = NtSetInformationFile (fh, &io, &fdie, sizeof fdie,
+                                         FileDispositionInformationEx);
+          if (NT_SUCCESS (status))
+            {
+              NtClose (fh);
+              goto out;
+            }
+        }
 
-         POSIX unlink semantics are nice, but they still fail if the file
+      /* POSIX unlink semantics are nice, but they still fail if the file
 	 has the R/O attribute set.  Removing the file is very much a safe
 	 bet afterwards, so, no transaction. */
       if (pc.file_attributes () & FILE_ATTRIBUTE_READONLY)
-- 
2.29.2



More information about the Cygwin-patches mailing list