Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mingw_rename: do support directory renames #1839

Closed

Conversation

dscho
Copy link
Member

@dscho dscho commented Dec 17, 2024

This is not quite a critical bug fix for Git because (unlike Git for Windows) it attempts _wrename() first. If that succeeds, we'll not bother with the POSIX semantics.

However, Git for Windows knows how to deal with symbolic links, and _wrename() does not work for them. Therefore, that _wrename() call was patched out there and we rely on the native Win32 API call to SetFileInformationByHandle() to rename files and directories.

It is that latter part that is at heart of this here bug fix: To be able to call SetFileInformationByHandle(), we need a handle, and CreateFileW() is what we use, for files, and crucially, also for directories.

So while it is not critical for Git to take this patch, it still is important because that _wrename() call can fail, even when renaming directories, and then we want the fallback to fail not because we tried to obtain a handle using incorrect flags, but only because the actual rename operation failed.

This patch is based on ps/mingw-rename.

Cc: Patrick Steinhardt ps@pks.im

In 391bcea (compat/mingw: support POSIX semantics for atomic
renames, 2024-10-27), we taught the `mingw_rename()` function to respect
POSIX semantics, but we did so only as a fallback after `_wrename()`
fails.

This hid a bug in the implementation that was not caught by Git's test
suite: The `CreateFileW()` function _can_ open handles to directories,
but not when asked to use the `FILE_ATTRIBUTE_NORMAL` flag, as that flag
only is allowed for files.

Let's fix this by using the common `FILE_FLAG_BACKUP_SEMANTICS` flag
that can be used for opening handles to directories, too.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
@dscho
Copy link
Member Author

dscho commented Dec 17, 2024

/submit

Copy link

gitgitgadget bot commented Dec 17, 2024

Submitted as pull.1839.git.1734439924842.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1839/dscho/ps/mingw-rename-followup-v1

To fetch this version to local tag pr-1839/dscho/ps/mingw-rename-followup-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1839/dscho/ps/mingw-rename-followup-v1

Copy link

gitgitgadget bot commented Dec 17, 2024

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
writes:

>     This is not quite a critical bug fix for Git because (unlike Git for
>     Windows) it attempts _wrename() first. If that succeeds, we'll not
>     bother with the POSIX semantics.
>     
>     However, Git for Windows knows how to deal with symbolic links, and
>     _wrename() does not work for them. Therefore, that _wrename() call was
>     patched out there and we rely on the native Win32 API call to
>     SetFileInformationByHandle() to rename files and directories.
>     
>     It is that latter part that is at heart of this here bug fix: To be able
>     to call SetFileInformationByHandle(), we need a handle, and
>     CreateFileW() is what we use, for files, and crucially, also for
>     directories.
>     
>     So while it is not critical for Git to take this patch, it still is
>     important because that _wrename() call can fail, even when renaming
>     directories, and then we want the fallback to fail not because we tried
>     to obtain a handle using incorrect flags, but only because the actual
>     rename operation failed.
>     
>     This patch is based on ps/mingw-rename.

Thanks.

As this will be part of GfW 2.48 anyway (I presume), let me take it
and include it in my tree to keep the divergence between our trees
small.

Copy link

gitgitgadget bot commented Dec 17, 2024

This patch series was integrated into seen via git@48a7879.

@gitgitgadget gitgitgadget bot added the seen label Dec 17, 2024
Copy link

gitgitgadget bot commented Dec 18, 2024

On the Git mailing list, Patrick Steinhardt wrote (reply to this):

On Tue, Dec 17, 2024 at 12:52:04PM +0000, Johannes Schindelin via GitGitGadget wrote:
> diff --git a/compat/mingw.c b/compat/mingw.c
> index c4320769db6..e8f491d03a7 100644
> --- a/compat/mingw.c
> +++ b/compat/mingw.c
> @@ -2273,7 +2273,7 @@ repeat:
>  
>  		old_handle = CreateFileW(wpold, DELETE,
>  					 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
> -					 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
> +					 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
>  		if (old_handle == INVALID_HANDLE_VALUE) {
>  			errno = err_win_to_posix(GetLastError());
>  			return -1;

Okay. I saw that FILE_FLAG_BACKUP_SEMANTICS was used in several other
users of `CreateFileW()` in this context, but I couldn't find good
enough information around what it is actually doing anywhere. The
explanation makes sense to me, so thanks for the follow-up fix!

Patrick

Copy link

gitgitgadget bot commented Dec 18, 2024

This branch is now known as js/mingw-rename-fix.

Copy link

gitgitgadget bot commented Dec 18, 2024

This patch series was integrated into seen via git@ec5de6a.

Copy link

gitgitgadget bot commented Dec 18, 2024

This patch series was integrated into next via git@57dc9a0.

@gitgitgadget gitgitgadget bot added the next label Dec 18, 2024
Copy link

gitgitgadget bot commented Dec 19, 2024

This patch series was integrated into seen via git@e8df208.

Copy link

gitgitgadget bot commented Dec 20, 2024

There was a status update in the "New Topics" section about the branch js/mingw-rename-fix on the Git mailing list:

Update the way rename() emulation on Windows handle directories to
correct an earlier attempt to do the same.

Will merge to 'master'.
source: <pull.1839.git.1734439924842.gitgitgadget@gmail.com>

Copy link

gitgitgadget bot commented Dec 21, 2024

This patch series was integrated into seen via git@f8ffcc1.

Copy link

gitgitgadget bot commented Dec 22, 2024

This patch series was integrated into seen via git@7f30e58.

Copy link

gitgitgadget bot commented Dec 22, 2024

This patch series was integrated into seen via git@5cfceef.

Copy link

gitgitgadget bot commented Dec 23, 2024

This patch series was integrated into seen via git@c4cc685.

Copy link

gitgitgadget bot commented Dec 23, 2024

This patch series was integrated into master via git@c4cc685.

Copy link

gitgitgadget bot commented Dec 23, 2024

This patch series was integrated into next via git@c4cc685.

@gitgitgadget gitgitgadget bot added the master label Dec 23, 2024
Copy link

gitgitgadget bot commented Dec 23, 2024

Closed via c4cc685.

@gitgitgadget gitgitgadget bot closed this Dec 23, 2024
@dscho dscho deleted the ps/mingw-rename-followup branch December 23, 2024 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant