PowerToys icon indicating copy to clipboard operation
PowerToys copied to clipboard

Add function to get Win32 path from file handle

Open rknastenka opened this issue 10 months ago • 5 comments

Summary of the Pull Request

This PR fixes the issue where File Locksmith would occasionally display raw NT kernel paths (\Device\HarddiskVolume1...) instead of user-friendly Win32 paths (C:...). This was primarily caused by the limitations of manual string-based path translation when encountering long paths, network shares, or complex mount points.

  • Added a helper function get_win32_path_from_handle inside the anonymous namespace.
  • Implemented a two-stage buffer allocation to safely handle paths of any length.
  • Added sanitization logic to strip the \?\ prefix and correctly format \?\UNC\ paths for user display.

Important: I have intentionally preserved the internal variable name kernel_file_name within the handles() loop and the associated HandleInfo struct fields.

While these variables now store resolved Win32 paths, keeping the original names ensures binary and source compatibility with other modules and the UI layer that consume this data. This avoids a large-scale refactoring and ensures this PR remains focused solely on fixing the path resolution bug.

PR Checklist

  • [x] Closes: #31385
  • [ ] Communication: I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected
  • [ ] Tests: Added/updated and all pass
  • [x] Localization: All end-user-facing strings can be localized
  • [ ] Dev docs: Added/updated
  • [ ] New binaries: Added on the required places
  • [ ] Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

rknastenka avatar Jan 09 '26 00:01 rknastenka

@microsoft-github-policy-service agree

rknastenka avatar Jan 09 '26 00:01 rknastenka

@check-spelling-bot Report

:red_circle: Please review

See the :open_file_folder: files view, the :scroll:action log, or :memo: job summary for details.

Unrecognized words (1)

cursorwrap

These words are not needed and should be removed ATX cppcoreguidelines CURSORWRAP swe

Some files were automatically ignored :see_no_evil:

These sample patterns would exclude them:

^src/modules/powerrename/unittests/testdata/avif_test\.avif$
^src/modules/powerrename/unittests/testdata/heif_test\.heic$

You should consider adding them to:

.github/actions/spell-check/excludes.txt

File matching is via Perl regular expressions.

To check these files, more of their words need to be in the dictionary than not. You can use patterns.txt to exclude portions, add items to the dictionary (e.g. by adding them to allow.txt), or fix typos.

To accept these unrecognized words as correct, update file exclusions, and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:rknastenka/PowerToys.git repository on the FileLocksmith branch (:information_source: how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/c635c2f3f714eec2fcf27b643a1919b9a811ef2e/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/21261230695/attempts/1' &&
git commit -m 'Update check-spelling metadata'
Warnings :warning: (2)

See the :open_file_folder: files view, the :scroll:action log, or :memo: job summary for details.

:warning: Warnings Count
:warning: ignored-expect-variant 6
:warning: large-file 2

See :warning: Event descriptions for more information.

If the flagged items are :exploding_head: false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it, try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

github-actions[bot] avatar Jan 22 '26 19:01 github-actions[bot]

🔍 Automated PR Review Summary

Step Severity Finding
12-code-comments 🟢 low Comment phrasing is ambiguous: \

Review generated by pr-review skill. See Generated Files/prReview/44624/ for full details.

yeelam-gordon avatar Feb 14 '26 12:02 yeelam-gordon

@daverayment would you mind doing another pass on reviewing this PR?

niels9001 avatar Jul 08 '26 12:07 niels9001

@rknastenka Just a quick note about your PR description. You state:

This PR fixes the issue where File Locksmith would occasionally display raw NT kernel paths (\Device\HarddiskVolume1...) instead of user-friendly Win32 paths (C:...). This was primarily caused by the limitations of manual string-based path translation when encountering long paths, network shares, or complex mount points.

The original issue author didn't claim kernel paths were appearing in the UI. Instead, the original issue is an "Idea-Enhancement" request, not a bug report, and it was about using GetFinalPathNameByHandleW() instead of the previous manual string manipulation. The API call does extra work to ensure a canonical path is produced. AFAIK, it's all "under the hood" enhancements.

It may be best to edit the description so it's clear that this isn't a bug fix, but an enhancement to overall robustness.

daverayment avatar Jul 08 '26 23:07 daverayment

thank you a lot @daverayment for the detailed feedback, i forced pushed a new commit to address what you mentioned

rknastenka avatar Jul 15 '26 17:07 rknastenka

Thanks @rknastenka. How do you think this should best be tested? We will need to confirm that the strings are the same on both sides of the comparison, and ensure that there haven't been any regressions. There are a set of unit tests for the CLI mode of FileLocksmith - in FileLocksmith\Tests\FileLocksmithCLI.UnitTests - but I'm unfamiliar with them. I don't know what you did to confirm your first pass at this PR worked. Did you install and test with the context menu option?

daverayment avatar Jul 15 '26 23:07 daverayment

/needinfo

MuyuanMS avatar Aug 05 '26 02:08 MuyuanMS

i haven't validated this end-to-end. as i ran into build issues getting a working local setup, so what I have is a code-level implementation only

on the existing CLI tests, I looked and they won't cover this. FileLocksmithCLITests.cpp injects a MockProcessFinder, so find_processes_recursive and NtdllExtensions never execute. Those tests pass with or without this change, they cover argument parsing and output formatting only.

rknastenka avatar Aug 07 '26 15:08 rknastenka

Although I think the code is now in a decent shape, I don't think we can go ahead with this without testing, so I'm going to convert this to a draft. Perhaps we can revisit at a later date.

daverayment avatar Sep 21 '26 00:09 daverayment