From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id E973E3858410; Tue, 18 Apr 2023 16:19:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E973E3858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681834753; bh=iiSolWWYGsBhtf5WEJje3vQKbhD6iQfd5mAcbvnyn9o=; h=From:To:Subject:Date:From; b=kEpjbKQIu6xJlFeVwyJ7274WtI/Zfx/TLbuYJ+VyGZmLjW7ns/QkmuOAhw8xuhYmA IAdPxlnRwhw9LYpIVGOIs/Z8twpL2IfYNrqeq75xCl1krc2lAQhu84bfLez+p/4P3Z LfnAlQzqh+9x2eRs+kaUXkQJ7YfVrhwK8A2soNEw= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_4-branch] Cygwin: align renameat2 to Linux behaviour X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/cygwin-3_4-branch X-Git-Oldrev: 4b8222983f914a7950987802e73fc069dffc3058 X-Git-Newrev: 7b3b2c201a271c5dcddcd3060f58d841478d47d9 Message-Id: <20230418161913.E973E3858410@sourceware.org> Date: Tue, 18 Apr 2023 16:19:13 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D7b3b2c201a2= 71c5dcddcd3060f58d841478d47d9 commit 7b3b2c201a271c5dcddcd3060f58d841478d47d9 Author: Corinna Vinschen AuthorDate: Tue Apr 18 18:18:27 2023 +0200 Commit: Corinna Vinschen CommitDate: Tue Apr 18 18:18:39 2023 +0200 Cygwin: align renameat2 to Linux behaviour =20 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. =20 Follow suit, given this is a Linux-only function anyway. =20 Fixes: f665b1cef30f ("cygwin: Implement renameat2") Reported-by: Bruno Haible Signed-off-by: Corinna Vinschen 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 =20 - 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 a4e4af626db1..5adc614ec30f 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -2122,6 +2122,14 @@ nt_path_has_executable_suffix (PUNICODE_STRING upath) return false; } =20 +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 @@ -2300,7 +2308,7 @@ rename2 (const char *oldpath, const char *newpath, un= signed int at2flags) newpc.get_nt_native_path (), FALSE)) { - res =3D 0; + res =3D set_same_file_return (noreplace); __leave; } newpc.file_attributes (INVALID_FILE_ATTRIBUTES); @@ -2315,7 +2323,7 @@ rename2 (const char *oldpath, const char *newpath, un= signed int at2flags) if (newpc.get_nt_native_path ()->Length =3D=3D oldpc.get_nt_native_path ()->Length) { - res =3D 0; + res =3D set_same_file_return (noreplace); __leave; } if (*(PWCHAR) ((PBYTE) newpc.get_nt_native_path ()->Buffer @@ -2335,7 +2343,7 @@ rename2 (const char *oldpath, const char *newpath, un= signed int at2flags) newpc.get_nt_native_path (), oldpc.objcaseinsensitive ())) { - res =3D 0; + res =3D set_same_file_return (noreplace); __leave; } } @@ -2364,7 +2372,7 @@ rename2 (const char *oldpath, const char *newpath, un= signed int at2flags) newpc.get_nt_native_path (), oldpc.objcaseinsensitive ())) { - res =3D 0; + res =3D set_same_file_return (noreplace); __leave; } } @@ -2582,7 +2590,7 @@ skip_pre_W10_checks: { debug_printf ("%s and %s are the same file", oldpath, newpath); NtClose (nfh); - res =3D 0; + res =3D set_same_file_return (noreplace); __leave; } NtClose (nfh);