changeset 31380:980059c3b129

unlink: Also remove files with read-only file attribute on Windows (bug #63265). * liboctave/wrappers/unistd-wrappers.c (octave_unlink_wrapper): "_wunlink" fails on Windows when trying to unlink files that have their read-only file attribute set. Try to un-set that attribute before unlinking. * etc/NEWS.8.md: Add note about new behavior of file system functions that depend on "unlink".
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 26 Oct 2022 20:06:47 +0200
parents 84fa33608593
children cad5406329be
files etc/NEWS.8.md liboctave/wrappers/unistd-wrappers.c
diffstat 2 files changed, 11 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/etc/NEWS.8.md	Mon Oct 31 16:25:46 2022 -0400
+++ b/etc/NEWS.8.md	Wed Oct 26 20:06:47 2022 +0200
@@ -72,6 +72,9 @@
 - `integral` can now accept the 'ArrayValued' option in combination with
   'RelTol' and 'WayPoints'.
 
+- File system operations that remove files (e.g., `unlink` or `rmdir`) now also
+  remove files that have their read-only file attribute set on Windows.
+
 - The default state for certain graphics properties has been made
   consistent with Matlab.
 
--- a/liboctave/wrappers/unistd-wrappers.c	Mon Oct 31 16:25:46 2022 -0400
+++ b/liboctave/wrappers/unistd-wrappers.c	Wed Oct 26 20:06:47 2022 +0200
@@ -380,8 +380,16 @@
 {
 #if defined (OCTAVE_USE_WINDOWS_API)
   wchar_t *wnm = u8_to_wchar (nm);
+
+  // _wunlink fails on files with the read-only flag set. Try to un-set it.
+  DWORD file_attributes = GetFileAttributesW (wnm);
+  if (file_attributes != INVALID_FILE_ATTRIBUTES
+      && file_attributes & FILE_ATTRIBUTE_READONLY)
+    SetFileAttributesW (wnm, file_attributes & ~FILE_ATTRIBUTE_READONLY);
+
   int status = _wunlink (wnm);
   free ((void *) wnm);
+
   return status;
 #else
   return unlink (nm);