[newlib-cygwin/main] Cygwin: open(2): reset sparseness on O_TRUNCated files

Corinna Vinschen corinna@sourceware.org
Tue Nov 28 10:03:10 GMT 2023


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=7f9caa4a3698827010d9b478407d21dd58b8bca5

commit 7f9caa4a3698827010d9b478407d21dd58b8bca5
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Mon Nov 27 21:14:49 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Tue Nov 28 10:55:33 2023 +0100

    Cygwin: open(2): reset sparseness on O_TRUNCated files
    
    open(2) implements O_TRUNC by just reducing the size of the file
    to 0, to make sure EAs stay available.
    
    Turns out, file sparseness is not removed this way either, so add
    code to do just that.
    
    Fixes: 603ef545bdbd ("* fhandler.cc (fhandler_base::open): Never open files with  FILE_OVERWITE/FILE_OVERWRITE_IF.")
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/fhandler/base.cc | 9 +++++++++
 winsup/cygwin/release/3.4.10   | 3 +++
 2 files changed, 12 insertions(+)

diff --git a/winsup/cygwin/fhandler/base.cc b/winsup/cygwin/fhandler/base.cc
index d859870cb40d..b9336625419a 100644
--- a/winsup/cygwin/fhandler/base.cc
+++ b/winsup/cygwin/fhandler/base.cc
@@ -776,6 +776,15 @@ fhandler_base::open (int flags, mode_t mode)
 	  NtClose (fh);
 	  goto done;
 	}
+      /* Drop sparseness */
+      if (pc.file_attributes () & FILE_ATTRIBUTE_SPARSE_FILE)
+	{
+	  FILE_SET_SPARSE_BUFFER fssb = { SetSparse: FALSE };
+	  status = NtFsControlFile (fh, NULL, NULL, NULL, &io,
+				    FSCTL_SET_SPARSE, &fssb, sizeof fssb, NULL, 0);
+	  if (NT_SUCCESS (status))
+	    pc.file_attributes (pc.file_attributes () & ~FILE_ATTRIBUTE_SPARSE_FILE);
+	}
     }
 
   set_handle (fh);
diff --git a/winsup/cygwin/release/3.4.10 b/winsup/cygwin/release/3.4.10
index 02f6885837b4..632ffcd39493 100644
--- a/winsup/cygwin/release/3.4.10
+++ b/winsup/cygwin/release/3.4.10
@@ -21,3 +21,6 @@ Bug Fixes
 
 - Fix posix_fallocate(3) return value in case of being called on
   other than regular files.
+
+- Reset sparseness in case open(2) has been called with O_CREAT|O_TRUNC on
+  sparse files.


More information about the Cygwin-cvs mailing list