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

fix: use MoveFileEx for rename on win #2546

Merged
merged 1 commit into from
Sep 19, 2023
Merged

Conversation

digama0
Copy link
Collaborator

@digama0 digama0 commented Sep 15, 2023

As discovered on Zulip, the C++ standard std::rename function does not overwrite the target on windows, unlike on linux and macos. https://doc.rust-lang.org/std/fs/fn.rename.html mentions that it uses MoveFileExW(from, to, MOVEFILE_REPLACE_EXISTING) on windows to address this issue, so we implement the same fix here.

@digama0 digama0 force-pushed the rename_win branch 3 times, most recently from 6ad7289 to 30bc506 Compare September 15, 2023 10:44
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Sep 15, 2023
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 15, 2023
@leanprover-community-mathlib4-bot
Copy link
Collaborator

leanprover-community-mathlib4-bot commented Sep 15, 2023

@eyelash
Copy link
Contributor

eyelash commented Sep 15, 2023

Does this work correctly for paths that contain (non-ASCII) Unicode characters?

@semorrison
Copy link
Collaborator

Could we add a test for the basic functionality? Might as well answer @eyelash's question at the same time.

Probably we should also copy the "platform specific behaviour" section from https://doc.rust-lang.org/std/fs/fn.rename.html, and/or include that link.

@digama0
Copy link
Collaborator Author

digama0 commented Sep 17, 2023

It is supposed to support non-ASCII, I don't see why it wouldn't, but it needs testing on a windows machine and I don't have one.

@eyelash
Copy link
Contributor

eyelash commented Sep 17, 2023

It is supposed to support non-ASCII, I don't see why it wouldn't

The reason I was asking was because of these lines:

std::wstring wfrom(string_cstr(from), string_cstr(from) + string_size(from));
std::wstring wto(string_cstr(to), string_cstr(to) + string_size(to));

I'm assuming that string_cstr returns a UTF-8 string and MoveFileExW takes a UTF-16 string and I don't see any conversion from UTF-8 to UTF-16 here.

By the way just calling the standard library with UTF-8 strings on Windows often does not work for non-ASCII strings so there might be more problems in this file related to Unicode on Windows. The following page provides some more information: https://learn.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page

@digama0
Copy link
Collaborator Author

digama0 commented Sep 17, 2023

I'm assuming that string_cstr returns a UTF-8 string and MoveFileExW takes a UTF-16 string and I don't see any conversion from UTF-8 to UTF-16 here.

The conversion is the wstring constructor, or at least it's supposed to... Like I said I have no ability to test this so someone else will have to tell me if it works.

@semorrison
Copy link
Collaborator

A test file would run on Windows during CI! :-)

semorrison added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 18, 2023
@semorrison semorrison added the missing tests This PR requires the addition of tests before it can be merged. label Sep 18, 2023
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Sep 18, 2023
@digama0
Copy link
Collaborator Author

digama0 commented Sep 18, 2023

I'm not sure I want to add tests which actually hit the filesystem...

@Kha
Copy link
Member

Kha commented Sep 18, 2023

Please use the MoveFileEx base function like we do everywhere else. We do use the UTF-8 codepage... sometimes #2554.

I'm not sure I want to add tests which actually hit the filesystem...

We already have IO_test.lean

@Kha Kha added the awaiting-author Waiting for PR author to address issues label Sep 18, 2023
@digama0
Copy link
Collaborator Author

digama0 commented Sep 19, 2023

Please use the MoveFileEx base function like we do everywhere else. We do use the UTF-8 codepage... sometimes #2554.

I'd be happy to use what is used elsewhere, I'm 100% cargo culting this code and don't do windows programming normally. But a grep for MoveFileEx comes up empty, do you have other examples?

@digama0 digama0 changed the title fix: use MoveFileExW for rename on win fix: use MoveFileEx for rename on win Sep 19, 2023
@digama0 digama0 added awaiting-review Waiting for someone to review the PR and removed awaiting-author Waiting for PR author to address issues labels Sep 19, 2023
@Kha
Copy link
Member

Kha commented Sep 19, 2023

Bad wording, I just meant that we never use the *W variants and instead trust in the codepage

@digama0 digama0 removed the missing tests This PR requires the addition of tests before it can be merged. label Sep 19, 2023
src/runtime/io.cpp Outdated Show resolved Hide resolved
@Kha
Copy link
Member

Kha commented Sep 19, 2023

Thanks!

auto-merge was automatically disabled September 19, 2023 17:27

Head branch was pushed to by a user without write access

@Kha Kha enabled auto-merge (rebase) September 19, 2023 17:28
@Kha Kha merged commit f0af71a into leanprover:master Sep 19, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review Waiting for someone to review the PR builds-mathlib CI has verified that Mathlib builds against this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants