public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/cygwin-3_4-branch] Cygwin: open(2): reset sparseness on O_TRUNCated files
Date: Tue, 28 Nov 2023 10:02:45 +0000 (GMT)	[thread overview]
Message-ID: <20231128100245.75BEB3847736@sourceware.org> (raw)

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

commit 89a1e4a573d40b233011402f42630b970ff71291
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:52 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 58b63da8de88..05e51536ff64 100644
--- a/winsup/cygwin/fhandler/base.cc
+++ b/winsup/cygwin/fhandler/base.cc
@@ -773,6 +773,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.

                 reply	other threads:[~2023-11-28 10:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231128100245.75BEB3847736@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).