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/main] Cygwin: align renameat2 to Linux behaviour
Date: Tue, 18 Apr 2023 16:19:18 +0000 (GMT)	[thread overview]
Message-ID: <20230418161919.00072385772D@sourceware.org> (raw)

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

commit d30a5917a9c40aec18c2243dfc2848c616dca77a
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Tue Apr 18 18:18:27 2023 +0200
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Tue Apr 18 18:18:27 2023 +0200

    Cygwin: align renameat2 to Linux behaviour
    
    In contrast to rename default behaviour, Linux' renameat2 returns -1
    with errno set to EEXIST, if oldfile and newfile refer to the same
    file, and the RENAME_NOREPLACE flag is set.
    
    Follow suit, given this is a Linux-only function anyway.
    
    Fixes: f665b1cef30f ("cygwin: Implement renameat2")
    Reported-by: Bruno Haible <bruno@clisp.org>
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/release/3.4.7 |  4 ++++
 winsup/cygwin/syscalls.cc   | 18 +++++++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/release/3.4.7 b/winsup/cygwin/release/3.4.7
index 7bc7d4a1ccdb..0e6922163aa3 100644
--- a/winsup/cygwin/release/3.4.7
+++ b/winsup/cygwin/release/3.4.7
@@ -21,3 +21,7 @@ Bug Fixes
 
 - Fix error handling in readlinkat.
   Addresses: https://cygwin.com/pipermail/cygwin/2023-April/253510.html
+
+- Fix return code and errno set by renameat2, if oldfile and newfile
+  refer to the same file, and the RENAME_NOREPLACE flag is set.
+  Addresses: https://cygwin.com/pipermail/cygwin/2023-April/253514.html
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index b109720568dc..73343ecc1f07 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -2108,6 +2108,14 @@ nt_path_has_executable_suffix (PUNICODE_STRING upath)
   return false;
 }
 
+inline int
+set_same_file_return (bool noreplace)
+{
+  if (!noreplace)
+    return 0;
+  set_errno (EEXIST);
+  return -1;
+}
 /* If newpath names an existing file and the RENAME_NOREPLACE flag is
    specified, fail with EEXIST.  Exception: Don't fail if the purpose
    of the rename is just to change the case of oldpath on a
@@ -2286,7 +2294,7 @@ rename2 (const char *oldpath, const char *newpath, unsigned int at2flags)
 				     newpc.get_nt_native_path (),
 				     FALSE))
 	    {
-	      res = 0;
+	      res = set_same_file_return (noreplace);
 	      __leave;
 	    }
 	  newpc.file_attributes (INVALID_FILE_ATTRIBUTES);
@@ -2301,7 +2309,7 @@ rename2 (const char *oldpath, const char *newpath, unsigned int at2flags)
 	      if (newpc.get_nt_native_path ()->Length
 		  == oldpc.get_nt_native_path ()->Length)
 		{
-		  res = 0;
+		  res = set_same_file_return (noreplace);
 		  __leave;
 		}
 	      if (*(PWCHAR) ((PBYTE) newpc.get_nt_native_path ()->Buffer
@@ -2321,7 +2329,7 @@ rename2 (const char *oldpath, const char *newpath, unsigned int at2flags)
 					 newpc.get_nt_native_path (),
 					 oldpc.objcaseinsensitive ()))
 		{
-		  res = 0;
+		  res = set_same_file_return (noreplace);
 		  __leave;
 		}
 	    }
@@ -2350,7 +2358,7 @@ rename2 (const char *oldpath, const char *newpath, unsigned int at2flags)
 					 newpc.get_nt_native_path (),
 					 oldpc.objcaseinsensitive ()))
 		{
-		  res = 0;
+		  res = set_same_file_return (noreplace);
 		  __leave;
 		}
 	    }
@@ -2571,7 +2579,7 @@ skip_pre_W10_checks:
 	    {
 	      debug_printf ("%s and %s are the same file", oldpath, newpath);
 	      NtClose (nfh);
-	      res = 0;
+	      res = set_same_file_return (noreplace);
 	      __leave;
 	    }
 	  NtClose (nfh);

                 reply	other threads:[~2023-04-18 16:19 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=20230418161919.00072385772D@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).