wallet: Fix detection of symlinks on Windows #34603

Open
achow101 wants to merge 4 commits from fix-listwallet-recursion-detect-symlinks into master AGit
achow101 commented 2026-02-17 00:34:28 +00:00 (Migrated from github.com)

This PR implements symlink detection for Windows, since GCC doesn't support symlinks on Windows. This is used by the wallet's ListDatabases in order to detect symlinks and avoid recursing them. It's also used in wallet path validation since that is also checking whether paths are symlinks, and we should properly detect symlinks on Windows.

Under the hood, the IsSymlink function uses std::filesystem::is_symlink for non-Windows systems. On Windows, it checks a file's attributes to determine if it is a reparse point. Strictly speaking, this will also catch files that aren't symlinks, such as directories that are Onedrive mounts. I think it's fine to not be recursively searching such directories.

This is based on #34418 for the multiwallet test and CI changes that it enables. The last commit also removes the skipping of the symlink checks so that we are testing that the symlink handling behavior on Windows matches that on Linux and MacOS.

This PR implements symlink detection for Windows, since GCC [doesn't support symlinks on Windows](https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/src/filesystem/ops-common.h;h=ba377905a2e90f7baf30c900b090f1f732397e08;hb=refs/heads/releases/gcc-13#l124). This is used by the wallet's `ListDatabases` in order to detect symlinks and avoid recursing them. It's also used in wallet path validation since that is also checking whether paths are symlinks, and we should properly detect symlinks on Windows. Under the hood, the `IsSymlink` function uses `std::filesystem::is_symlink` for non-Windows systems. On Windows, it checks a file's attributes to determine if it is a [reparse point](https://learn.microsoft.com/en-us/windows/win32/fileio/reparse-points). Strictly speaking, this will also catch files that aren't symlinks, such as directories that are Onedrive mounts. I think it's fine to not be recursively searching such directories. This is based on #34418 for the multiwallet test and CI changes that it enables. The last commit also removes the skipping of the symlink checks so that we are testing that the symlink handling behavior on Windows matches that on Linux and MacOS.
DrahtBot commented 2026-02-17 00:35:07 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK sedited, molnard

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34909 (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/34603 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/34603.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/34603#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/34603#pullrequestreview-4444925610), [molnard](https://github.com/bitcoin/bitcoin/pull/34603#pullrequestreview-5280988411) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#34909](https://github.com/bitcoin/bitcoin/pull/34909) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34909.svg"></sub> (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
fanquake (Migrated from github.com) reviewed 2026-02-17 09:41:59 +00:00
fanquake (Migrated from github.com) commented 2026-02-17 09:41:59 +00:00

Do symlinks work fine with LLVM/Clang? cc @hebasto (#31507).

Do symlinks work fine with LLVM/Clang? cc @hebasto (#31507).
hebasto (Migrated from github.com) reviewed 2026-02-18 15:00:31 +00:00
hebasto (Migrated from github.com) commented 2026-02-18 15:00:31 +00:00

When building on Windows either with MSVC or clang-cl, std::filesystem::is_symlink successfully detects file and folder symbolic links, but fails to detect junctions.

Since the in-code comment mentions "Windows cross compile", were you actually asking about the LLVM MinGW toolchain?

When building on Windows either with MSVC or clang-cl, `std::filesystem::is_symlink` successfully detects file and folder symbolic links, but fails to detect [junctions](https://learn.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions#junctions). Since the in-code comment mentions "Windows cross compile", were you actually asking about the [LLVM MinGW](https://github.com/mstorsjo/llvm-mingw) toolchain?
hebasto (Migrated from github.com) reviewed 2026-02-18 15:19:24 +00:00
hebasto (Migrated from github.com) commented 2026-02-18 15:19:24 +00:00

f507a31eec5219c3621b9d5551f9d04c2a409ded

Should we additionally check against a Reparse Point Tag value?

f507a31eec5219c3621b9d5551f9d04c2a409ded Should we additionally check against a [Reparse Point Tag](https://learn.microsoft.com/en-us/windows/win32/fileio/reparse-point-tags) value?
luke-jr (Migrated from github.com) reviewed 2026-02-18 17:37:19 +00:00
luke-jr (Migrated from github.com) commented 2026-02-18 17:37:19 +00:00

Why is this removed? It's actually kind of annoying to have files that need to be chmod'd before deletion

Why is this removed? It's actually kind of annoying to have files that need to be chmod'd before deletion
achow101 (Migrated from github.com) reviewed 2026-02-18 18:14:40 +00:00
achow101 (Migrated from github.com) commented 2026-02-18 18:14:40 +00:00

It isn't, the chmod testing is significantly changed. This is from #34418 and review comments about it should be left there.

It isn't, the chmod testing is significantly changed. This is from #34418 and review comments about it should be left there.
achow101 (Migrated from github.com) reviewed 2026-02-18 18:16:09 +00:00
achow101 (Migrated from github.com) commented 2026-02-18 18:16:09 +00:00

I considered that, but didn't think any of the possible tag values were things that we actually want to support recursively scanning inside of.

I considered that, but didn't think any of the possible tag values were things that we actually want to support recursively scanning inside of.
luke-jr (Migrated from github.com) reviewed 2026-02-18 18:50:00 +00:00
luke-jr (Migrated from github.com) commented 2026-02-18 18:50:00 +00:00

Might it be better to just override fs::is_symlink on Windows?

Might it be better to just override fs::is_symlink on Windows?
hebasto (Migrated from github.com) reviewed 2026-02-18 18:54:21 +00:00
hebasto (Migrated from github.com) commented 2026-02-18 18:54:21 +00:00

Since the IsSymlink fucntion returns true for entities other than symlinks, we should probably add a clarifying comment.

Since the `IsSymlink` fucntion returns `true` for entities other than symlinks, we should probably add a clarifying comment.
luke-jr (Migrated from github.com) reviewed 2026-02-18 19:14:11 +00:00
luke-jr (Migrated from github.com) commented 2026-02-18 19:14:11 +00:00

Is it intentional that we still check the symlink itself for a wallet?

If so, should we not do so even if IsSymlink throws?

Is it intentional that we still check the symlink itself for a wallet? If so, should we not do so even if IsSymlink throws?
luke-jr (Migrated from github.com) reviewed 2026-02-18 19:18:45 +00:00
Lines 2932-2935
luke-jr (Migrated from github.com) commented 2026-02-18 19:18:45 +00:00

This calls IsSymlink twice in some cases.

This calls `IsSymlink` twice in some cases.
achow101 (Migrated from github.com) reviewed 2026-02-18 19:18:48 +00:00
achow101 (Migrated from github.com) commented 2026-02-18 19:18:48 +00:00

I considered that, but we aren't already using fs::is_symlink so I don't think it would give us any benefit. Also, since IsSymlink returns true for more than just symlinks, I thought it wouldn't be appropriate to be override the stdlib function.

I considered that, but we aren't already using `fs::is_symlink` so I don't think it would give us any benefit. Also, since `IsSymlink` returns `true` for more than just symlinks, I thought it wouldn't be appropriate to be override the stdlib function.
luke-jr (Migrated from github.com) reviewed 2026-02-18 19:19:15 +00:00
luke-jr (Migrated from github.com) commented 2026-02-18 19:19:16 +00:00

Does this need to change? We already check for path_type == fs::file_type::directory

Does this need to change? We already check for path_type == fs::file_type::directory
achow101 (Migrated from github.com) reviewed 2026-02-18 19:21:54 +00:00
achow101 (Migrated from github.com) commented 2026-02-18 19:21:54 +00:00

Is it intentional that we still check the symlink itself for a wallet?

Yes

If so, should we not do so even if IsSymlink throws?

status() and symlink_status() used below can also throw, so this is the same behavior.

> Is it intentional that we still check the symlink itself for a wallet? Yes > If so, should we not do so even if IsSymlink throws? `status()` and `symlink_status()` used below can also throw, so this is the same behavior.
achow101 (Migrated from github.com) reviewed 2026-02-24 23:06:32 +00:00
achow101 (Migrated from github.com) commented 2026-02-24 23:06:32 +00:00

The docstring states that it returns true for reparse points on windows.

The docstring states that it returns true for reparse points on windows.
achow101 (Migrated from github.com) reviewed 2026-02-24 23:12:56 +00:00
achow101 (Migrated from github.com) commented 2026-02-24 23:12:56 +00:00

Hmm, good point. Removed

Hmm, good point. Removed
achow101 (Migrated from github.com) reviewed 2026-02-24 23:13:12 +00:00
Lines 2932-2935
achow101 (Migrated from github.com) commented 2026-02-24 23:13:12 +00:00
No longer relevant with https://github.com/bitcoin/bitcoin/pull/34603#discussion_r2824027663
maflcko (Migrated from github.com) reviewed 2026-03-16 09:14:12 +00:00
maflcko (Migrated from github.com) commented 2026-03-16 09:14:12 +00:00

llm nits:

windows -> Windows [Proper noun; "Windows" should be capitalized in documentation comment] (src/util/fs_helpers.h)
explicilty -> explicitly [Spelling error in comment] (this line)

llm nits: windows -> Windows [Proper noun; "Windows" should be capitalized in documentation comment] (src/util/fs_helpers.h) explicilty -> explicitly [Spelling error in comment] (this line)
achow101 (Migrated from github.com) reviewed 2026-03-23 22:05:39 +00:00
achow101 (Migrated from github.com) commented 2026-03-23 22:05:39 +00:00

Fixed

Fixed
achow101 commented 2026-03-23 22:05:52 +00:00 (Migrated from github.com)

Forgot to undraft this.

Forgot to undraft this.
sedited (Migrated from github.com) approved these changes 2026-06-07 11:00:01 +00:00
sedited (Migrated from github.com) left a comment

utACK

The changes seem fine to me, but I would suggest adding a note in the description about hebasto's comment https://github.com/bitcoin/bitcoin/pull/34603#discussion_r2822782155 about why we're gating WIN32 in general.

utACK The changes seem fine to me, but I would suggest adding a note in the description about hebasto's comment https://github.com/bitcoin/bitcoin/pull/34603#discussion_r2822782155 about why we're gating WIN32 in general.
sedited commented 2026-09-22 07:56:19 +00:00 (Migrated from github.com)

Ping for review @hebasto.

Ping for review @hebasto.
molnard (Migrated from github.com) reviewed 2026-09-22 17:17:46 +00:00
molnard (Migrated from github.com) left a comment

cACK

I tested PR tip b127cde3f4 against base 9c15022260 on Windows 11 x64 (25H2), NTFS, with MinGW/GCC 15.2.0, MSVC 19.51.36257, and clang-cl 23.1.1 using Microsoft's STL. wallet_multiwallet.py passes on the PR with all three compilers.

I also added local junction checks. For a junction pointing to an outside wallet directory containing another wallet in a nested directory:

  • MinGW: the BASE lists both junction_wallet and junction_wallet\nested. The PR lists only junction_wallet, confirming the fix.
  • MSVC and clang-cl: BOTH revisions already list only junction_wallet.

The full multiwallet test with the local junction checks also passes on the PR with all three compilers.

Could we add this nested-wallet junction case to wallet_multiwallet.py? It checks that the wallet at the junction remains discoverable while discovery does not descend into its subdirectories.

cACK I tested PR tip b127cde3f4ae99d186d3de62f84bb3c9b7c48b0a against base 9c150222604478431669b66b2caa0d75e8324d4c on Windows 11 x64 (25H2), NTFS, with MinGW/GCC 15.2.0, MSVC 19.51.36257, and clang-cl 23.1.1 using Microsoft's STL. `wallet_multiwallet.py` passes on the PR with all three compilers. I also added local junction checks. For a junction pointing to an outside wallet directory containing another wallet in a nested directory: - MinGW: the BASE lists both `junction_wallet` and `junction_wallet\nested`. The PR lists only `junction_wallet`, confirming the fix. - MSVC and clang-cl: BOTH revisions already list only `junction_wallet`. The full multiwallet test with the local junction checks also passes on the PR with all three compilers. Could we add this nested-wallet junction case to `wallet_multiwallet.py`? It checks that the wallet at the junction remains discoverable while discovery does not descend into its subdirectories.
molnard (Migrated from github.com) commented 2026-09-22 16:44:05 +00:00

This is how I created the junction/s for the test.

def create_junction(self, link, target):
    subprocess.run(
        'cmd.exe /d /v:off /c mklink /J "%JUNCTION_LINK%" "%JUNCTION_TARGET%"',
        env={**os.environ, 'JUNCTION_LINK': link, 'JUNCTION_TARGET': target},
        capture_output=True,
        check=True,
    )
    # Verify that this is a junction, not an ordinary directory symlink.
    assert_equal(os.lstat(link).st_reparse_tag, stat.IO_REPARSE_TAG_MOUNT_POINT)

Attached the patch for the exact code: junction-regression.patch

This is how I created the junction/s for the test. ```py def create_junction(self, link, target): subprocess.run( 'cmd.exe /d /v:off /c mklink /J "%JUNCTION_LINK%" "%JUNCTION_TARGET%"', env={**os.environ, 'JUNCTION_LINK': link, 'JUNCTION_TARGET': target}, capture_output=True, check=True, ) # Verify that this is a junction, not an ordinary directory symlink. assert_equal(os.lstat(link).st_reparse_tag, stat.IO_REPARSE_TAG_MOUNT_POINT) ``` Attached the patch for the exact code: [junction-regression.patch](https://github.com/user-attachments/files/32528168/junction-regression.patch)
First-time contributor

Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive linked from the Review report for their assessments.

🟠 Minor · ListDatabases lists Windows file symlinks that GetWalletPath rejects

On GCC-built Windows binaries, a top-level BDB file symlink remains advertised for migration although GetWalletPath now rejects it; no current in-tree caller or fixture was found that creates this input.

Location: src/wallet/db.cpp:34 (head).

🔵 Suggestion · GetWalletPath broadens file rejection from symlinks to all Windows reparse points

GetWalletPath now excludes non-link Windows reparse-point files from legacy compatibility, although the stated rationale addresses directory traversal; practical incidence remains unconfirmed because visibility of the affected non-link tags has not been established.

Location: src/wallet/walletutil.cpp:70 (head).

🔵 Suggestion · Routine wallet-directory scans warn about supported symlink layouts

ListDatabases now generates warning-level messages for supported directory-symlink wallets during routine RPC and GUI scans, although non-recursion is their intended handling rather than a problem requiring administrator action.

Location: src/wallet/db.cpp:35 (head).

🔵 Suggestion · IsSymlink discards the Windows error and unnecessarily converts the exception path

When GetFileAttributesW fails, IsSymlink replaces the native error with invalid_argument and rebuilds the exception path through a locale-dependent narrow-string constructor, obscuring diagnosis.

Location: src/util/fs_helpers.cpp:357 (head).

Review coverage was incomplete.


Reviewed: d8f14e32ca8e · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:301f8fac66b8cbe87114981d6600f15187695a2edf245458a232c3958e920add:3329223fb40bbbe44124b0523f0fa08f23650d06836368f60e45a38d2db02971 --> <!-- ralph-base:74f3ed9021e853aa14a402ed0ceef78544c5b4a7 --> <!-- ralph-head:d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b --> <!-- ralph-case:4f972eca2e5ffb71cbafd44a3b504e61928f16069af6aa274386d3df9c34919f --> Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive linked from the Review report for their assessments. **🟠 Minor · `ListDatabases` lists Windows file symlinks that `GetWalletPath` rejects** On GCC-built Windows binaries, a top-level BDB file symlink remains advertised for migration although `GetWalletPath` now rejects it; no current in-tree caller or fixture was found that creates this input. Location: [`src/wallet/db.cpp:34`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b/src/wallet/db.cpp#L34>) (head). **🔵 Suggestion · `GetWalletPath` broadens file rejection from symlinks to all Windows reparse points** `GetWalletPath` now excludes non-link Windows reparse-point files from legacy compatibility, although the stated rationale addresses directory traversal; practical incidence remains unconfirmed because visibility of the affected non-link tags has not been established. Location: [`src/wallet/walletutil.cpp:70`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b/src/wallet/walletutil.cpp#L70>) (head). **🔵 Suggestion · Routine wallet-directory scans warn about supported symlink layouts** `ListDatabases` now generates warning-level messages for supported directory-symlink wallets during routine RPC and GUI scans, although non-recursion is their intended handling rather than a problem requiring administrator action. Location: [`src/wallet/db.cpp:35`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b/src/wallet/db.cpp#L35>) (head). **🔵 Suggestion · `IsSymlink` discards the Windows error and unnecessarily converts the exception path** When `GetFileAttributesW` fails, `IsSymlink` replaces the native error with `invalid_argument` and rebuilds the exception path through a locale-dependent narrow-string constructor, obscuring diagnosis. Location: [`src/util/fs_helpers.cpp:357`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b/src/util/fs_helpers.cpp#L357>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`d8f14e32ca8e`](<https://git.fish.foo/bitcoin/bitcoin/commit/d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b>) · [Review report](<https://review.fish.foo/report/34603-d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b-3060-4.html>) · [Previous reports](<https://review.fish.foo/traces/34603/>) [permalink](<https://review.fish.foo/report/34603-d8f14e32ca8ec39d3e301457ae0cf2dda9b96d0b-3060-4.html>)
DrahtBot commented 2026-11-24 15:58:16 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-11-25 22:04:17 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!34603
No description provided.