KEMBAR78
Treat -Target as literal in New-Item by GameMicrowave · Pull Request #25186 · PowerShell/PowerShell · GitHub
Skip to content

Conversation

@GameMicrowave
Copy link
Contributor

@GameMicrowave GameMicrowave commented Mar 16, 2025

PR Summary

Fix #6232
Fix #13136

Fix New-Item fails to create file links or creates links pointing to incorrect locations when the target path contains wildcard characters.

Make the -Target and -Value parameters of New-Item treat the path as a literal.

PR Context

When using the New-Item command to create file links, if the target path contains wildcard characters, they are incorrectly interpreted. The following scenarios occur:

  • When creating a hard link, if the link path is an absolute path, the creation will always fail.
  • When creating a symbolic link:
    • If the wildcard characters in the link path are not escaped with a backtick, the creation will fail.
    • If the wildcard characters in the link path are escaped with a backtick, a link pointing to an incorrect path will be created.
  • When creating a Junction, the situation is similar to the above.

Example:

New-Item lk21.txt -ItemType HardLink -Target 'D:\[test].txt'
# New-Item: Could not find item D:\[test].txt.

# The `Get-Item` command can correctly retrieve the file.
Get-Item -LP 'D:\[test].txt'
#    Directory: D:\
# Mode                 LastWriteTime         Length Name
# ----                 -------------         ------ ----
# -a---           2025/3/15    14:26              9 [test].txt

Now, the path is correctly parsed to create the link.

PR Checklist

Fix Path explain error
Fixed char '`' can not escape
Fixed wildcard conversion of hard links
Fix wildcard conversion of hard links
@GameMicrowave
Copy link
Contributor Author

请阅读以下贡献者许可协议 (CLA)。如果您同意 CLA,请回复以下信息。

@microsoft-github-policy-service agree [company="{your company}"]

选项:

  • (默认 - 未指定公司)我对我的提交内容拥有唯一的知识产权,并且我不会在为我的雇主工作期间提交提交内容。
@microsoft-github-policy-service agree
  • (当给出公司时)我在为我的雇主工作期间提交了提交内容(或者我的雇主根据合同或适用法律对我的提交内容拥有知识产权)。我已获得雇主的许可,可以代表我的雇主提交内容并签订本协议。通过在下面签名,定义的术语“您”包括我和我的雇主。
@microsoft-github-policy-service agree company="Microsoft"

贡献者许可协议

@microsoft-github-policy-service agree

@iSazonov
Copy link
Collaborator

@GameMicrowave There is #16306 which must be fixed first since it is public API.

// if it is a wildcard char, escape it
//
if (IsWildcardChar(ch) && !charsNotToEscape.Contains(ch))
if ((IsWildcardChar(ch) || ch == '`') && !charsNotToEscape.Contains(ch))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's discuss in related issue.

@iSazonov
Copy link
Collaborator

@GameMicrowave Please resolve merge conflicts.

@GameMicrowave
Copy link
Contributor Author

@GameMicrowave Please resolve merge conflicts.

yes, i resolved it😊

@GameMicrowave GameMicrowave changed the title Fix New-Item file link creation failure and WildcardPattern.Escape result issue. Fix New-Item file link creation failure Mar 25, 2025
@iSazonov
Copy link
Collaborator

@GameMicrowave I tried the build with examples from related issues and they don't work. Please add tests if you want to continue working on this issue.

fix relative path issue
Add Test of file link creation
@GameMicrowave
Copy link
Contributor Author

@GameMicrowave I tried the build with examples from related issues and they don't work. Please add tests if you want to continue working on this issue.

Alright, I've written a test script. In the process, I discovered another bug, which I've now fixed.


New-Item $hardLink -Target ([WildcardPattern]::Escape((Get-Item -PSPath $testFile))) -Type HardLink # create hardlink with absolute path
New-Item $symbolicLink -Target ([WildcardPattern]::Escape($testFile)) -Type SymbolicLink
New-Item $junction -Target ([WildcardPattern]::Escape((Get-Item -PSPath $testFolder))) -Type Junction
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please see PowerShell Committee conclusion in #6232 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I make the -Target and -Value parameters of New-Item treat the path as a literal.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Apr 25, 2025
}
}

Describe "If the target path contains wildcard characters, the operation should succeed in creating a file link." -Tags @("CI", 'RequireAdminOnWindows') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

All file link tests are in FileSystem.Tests.ps1 line 555. Please move new tests to the file.

@iSazonov iSazonov requested a review from SteveL-MSFT April 29, 2025 10:12
$symLinkToNothing = Join-Path $TestPath "sym-link-to-nowhere"
$dirSymLinkToDir = Join-Path $TestPath "symd-link-to-dir"
$junctionToDir = Join-Path $TestPath "junction-to-dir"
$hardLinkToFile = Join-Path $TestPath "[hard-to-file].txt"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we haven't explicit tests please add new comment before the line that we intentionally use wildcard chars in follow names for testing.

if file name contains wildcard character,the new-item should succeed on creation file link
@iSazonov iSazonov self-assigned this May 1, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label May 1, 2025
@GameMicrowave GameMicrowave marked this pull request as draft May 1, 2025 18:18
@GameMicrowave GameMicrowave marked this pull request as ready for review May 1, 2025 18:45
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label May 9, 2025
@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label May 12, 2025
@iSazonov iSazonov changed the title Fix New-Item file link creation failure Treat -Target as literal in New-Item May 12, 2025
@iSazonov iSazonov merged commit fcc1833 into PowerShell:master May 12, 2025
39 checks passed
@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented May 12, 2025

📣 Hey @@GameMicrowave, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@iSazonov
Copy link
Collaborator

@GameMicrowave Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

3 participants