Skip to content

wallet: Fix detection of symlinks on Windows - #34603

Open
achow101 wants to merge 4 commits into
bitcoin:masterfrom
achow101:fix-listwallet-recursion-detect-symlinks
Open

achow101 wants to merge 4 commits into
bitcoin:masterfrom
achow101:fix-listwallet-recursion-detect-symlinks

Conversation

@achow101

Copy link
Copy Markdown
Member

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.

@DrahtBot

DrahtBot commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

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.

@achow101
achow101 force-pushed the fix-listwallet-recursion-detect-symlinks branch from 0c76d21 to 677297e Compare February 17, 2026 03:00
Comment thread src/wallet/wallet.cpp Outdated
(path_type == fs::file_type::symlink && fs::is_directory(wallet_path)) ||
(path_type == fs::file_type::regular && fs::PathFromString(name).filename() == fs::PathFromString(name)))) {
(IsSymlink(wallet_path) && fs::is_directory(wallet_path)) ||
// Windows cross compile does not detect symlinks, so we need to explicilty check

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread src/util/fs_helpers.cpp
if (file_attrs == INVALID_FILE_ATTRIBUTES) {
throw fs::filesystem_error("Unable to get file attributes", fs::PathToString(path), std::make_error_code(std::errc::invalid_argument));
}
return (file_attrs & FILE_ATTRIBUTE_REPARSE_POINT) != 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f507a31

Should we additionally check against a Reparse Point Tag value?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread test/functional/wallet_multiwallet.py Outdated
Comment on lines -152 to -154
finally:
# Need to ensure access is restored for cleanup
os.chmod(wallet_dir('no_access'), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/util/fs_helpers.cpp
}
#endif

bool IsSymlink(const fs::path& path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wallet/db.cpp
if (IsSymlink(it->path())) {
LogWarning("Not recursively searching symlink/reparse point at '%s'", fs::PathToString(it->path()));
it.disable_recursion_pending();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wallet/wallet.cpp Resolved
Comment thread src/wallet/wallet.cpp Resolved
hodlinator added a commit to hodlinator/bitcoin that referenced this pull request Feb 19, 2026
test_scanning_sub_dir():
- Remove try/finally - we don't need to clean up after a failed test (done in this commit to maintain indentation).

Regarding symlinks: bitcoin#31410 (comment)

Kept some symlink creation which didn't disrupt Windows cross builds to make for a smaller diff and less cumbersome code. There is some hope of eventually getting better symlink support via bitcoin#34603.

Co-authored-by: Ava Chow <[email protected]>
@achow101
achow101 force-pushed the fix-listwallet-recursion-detect-symlinks branch from 677297e to fda778d Compare February 24, 2026 23:12
@DrahtBot DrahtBot mentioned this pull request Mar 4, 2026
@achow101
achow101 force-pushed the fix-listwallet-recursion-detect-symlinks branch from fda778d to 9333d26 Compare March 13, 2026 23:05
Comment thread src/wallet/wallet.cpp Resolved
@achow101
achow101 force-pushed the fix-listwallet-recursion-detect-symlinks branch from 9333d26 to 0f3fcdf Compare March 23, 2026 22:05
@achow101
achow101 marked this pull request as ready for review March 23, 2026 22:05
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Apr 13, 2026
When searching the wallets directory for wallets, we want to avoid
recursing symlinks. Although recursive_directory_iterator already
doesn't recurse symlinks, since GCC doesn't implement symlink detection
on Windows, we need to use IsSymlink to detect symlinks on Windows in
order to avoid recursing them.

Github-Pull: bitcoin#34603
Rebased-From: 6faf3d892cc3c0bc85950100637430a894a1a8e6
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Apr 13, 2026
Since GCC lacks symlink detection, we need to use IsSymlink to detect
symlinks on Windows. Additionally, symlinks to files are reported as
being regular files rather symlnks, so we need to explicitly check with
IsSymlink that a regular file is not actually a symlink.

Github-Pull: bitcoin#34603
Rebased-From: 62675e4b7db0e83b1562ba51829d84c5d48e0a65
@achow101
achow101 force-pushed the fix-listwallet-recursion-detect-symlinks branch from 0f3fcdf to 36a6c83 Compare May 19, 2026 21:36
@achow101
achow101 force-pushed the fix-listwallet-recursion-detect-symlinks branch from 36a6c83 to b127cde Compare May 26, 2026 18:14

@sedited sedited left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

The changes seem fine to me, but I would suggest adding a note in the description about hebasto's comment #34603 (comment) about why we're gating WIN32 in general.

@sedited

sedited commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Ping for review @hebasto.

@molnard molnard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cACK

I tested PR tip b127cde against base 9c15022 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.

@@ -105,21 +100,20 @@ def run_test(self):
self.test_lock_file_closed(node)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

GCC lacks detection of symlinks on Windows. IsSymlink implements this
detection on Windows, and utilizes std::filesystem::is_symlink for other
systems.
When searching the wallets directory for wallets, we want to avoid
recursing symlinks. Although recursive_directory_iterator already
doesn't recurse symlinks, since GCC doesn't implement symlink detection
on Windows, we need to use IsSymlink to detect symlinks on Windows in
order to avoid recursing them.
Since GCC lacks symlink detection, we need to use IsSymlink to detect
symlinks on Windows. Additionally, symlinks to files are reported as
being regular files rather symlnks, so we need to explicitly check with
IsSymlink that a regular file is not actually a symlink.
@achow101
achow101 force-pushed the fix-listwallet-recursion-detect-symlinks branch from b127cde to d8f14e3 Compare November 24, 2026 19:25
@DrahtBot

Copy link
Copy Markdown
Contributor

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

paulscode added a commit to paulscode/knots-sha256 that referenced this pull request Nov 30, 2026
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.

8 participants