8000 Treat -Target as literal in New-Item (#25186) · PowerShell/PowerShell@fcc1833 · GitHub
[go: up one dir, main page]

Skip to content

Commit fcc1833

Browse files
Treat -Target as literal in New-Item (#25186)
1 parent e7ee9da commit fcc1833

File tree

4 files changed

+16
-46
lines changed

4 files changed

+16
-46
lines changed

src/System.Management.Automation/engine/SessionStateContainer.cs

+1-31
Original file line numberDiff line numberDiff line change
@@ -3497,37 +3497,7 @@ internal void NewItem(
34973497
throw PSTraceSource.NewArgumentNullException(nameof(content), SessionStateStrings.NewLinkTargetNotSpecified, path);
34983498
}
34993499

3500-
ProviderInfo targetProvider = null;
3501-
CmdletProvider targetProviderInstance = null;
3502-
3503-
var globbedTarget = Globber.GetGlobbedProviderPathsFromMonadPath(
3504-
targetPath,
3505-
allowNonexistingPath,
3506-
context,
3507-
out targetProvider,
3508-
out targetProviderInstance);
3509-
3510-
if (!string.Equals(targetProvider.Name, "filesystem", StringComparison.OrdinalIgnoreCase))
3511-
{
3512-
throw PSTraceSource.NewNotSupportedException(SessionStateStrings.MustBeFileSystemPath);
3513-
}
3514-
3515-
if (globbedTarget.Count > 1)
3516-
{
3517-
throw PSTraceSource.NewInvalidOperationException(SessionStateStrings.PathResolvedToMultiple, targetPath);
3518-
}
3519-
3520-
if (globbedTarget.Count == 0)
3521-
{
3522-
throw PSTraceSource.NewInvalidOperationException(SessionStateStrings.PathNotFound, targetPath);
3523-
}
3524-
3525-
// If the original target was a relative path, we want to leave it as relative if it did not require
3526-
// globbing to resolve.
3527-
if (WildcardPattern.ContainsWildcardCharacters(targetPath))
3528-
{
3529-
content = globbedTarget[0];
3530-
}
3500+
content = targetPath;
35313501
}
35323502

35333503
NewItemPrivate(providerInstance, composedPath, type, content, context);

src/System.Management.Automation/namespaces/FileSystemProvider.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2271,8 +2271,7 @@ protected override void NewItem(
22712271
// for hardlinks we resolve the target to an absolute path
22722272
if (!IsAbsolutePath(strTargetPath))
22732273
{
2274-
// there is already a check before here so that strTargetPath should only resolve to 1 path
2275-
strTargetPath = SessionState.Path.GetResolvedPSPathFromPSPath(strTargetPath).FirstOrDefault()?.Path;
2274+
strTargetPath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(strTargetPath);
22762275
}
22772276

22782277
exists = GetFileSystemInfo(strTargetPath, out isDirectory) != null;

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

+13-12
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,12 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
605605
}
606606
}
607607

608-
$realFile = Join-Path $TestPath "file.txt"
608+
# Ensure that the file link can still be successfully created when the target file/directory name contains wildcards.
609+
$realFile = Join-Path $TestPath "[file].txt"
609610
$nonFile = Join-Path $TestPath "not-a-file"
610611
$fileContent = "some text"
611-
$realDir = Join-Path $TestPath "subdir"
612-
$realDir2 = Join-Path $TestPath "second-subdir"
612+
$realDir = Join-Path $TestPath "[subdir]"
613+
$realDir2 = Join-Path $TestPath "[second-subdir]"
613614
$nonDir = Join-Path $TestPath "not-a-dir"
614615
$hardLinkToFile = Join-Path $TestPath "hard-to-file.txt"
615616
$symLinkToFile = Join-Path $TestPath "sym-link-to-file.txt"
@@ -626,7 +627,7 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
626627
Context "New-Item and hard/symbolic links" {
627628
AfterEach {
628629
# clean up created links after each test
629-
Remove-Item -Exclude (Split-Path -Leaf $realFile, $realDir, $realDir2) -Recurse $TestPath/*
630+
Remove-Item -Exclude (Split-Path -Leaf ([WildcardPattern]::Escape($realFile)), ([WildcardPattern]::Escape($realDir)), ([WildcardPattern]::Escape($realDir2))) -Recurse $TestPath/*
630631
}
631632

632633
It "New-Item can create a hard link to a file" {
@@ -639,7 +640,7 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
639640
It "New-Item can create symbolic link to file" {
640641
New-Item -ItemType SymbolicLink -Path $symLinkToFile -Value $realFile > $null
641642
Test-Path $symLinkToFile | Should -BeTrue
642-
$real = Get-Item -Path $realFile
643+
$real = Get-Item -LiteralPath $realFile
643644
$link = Get-Item -Path $symLinkToFile
644645
$link.LinkType | Should -BeExactly "SymbolicLink"
645646
$link.Target | Should -BeExactly $real.ToString()
@@ -658,15 +659,15 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
658659
It "New-Item can create a symbolic link to a directory" -Skip:($IsWindows) {
659660
New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir > $null
660661
Test-Path $symLinkToDir | Should -BeTrue
661-
$real = Get-Item -Path $realDir
662+
$real = Get-Item -LiteralPath $realDir
662663
$link = Get-Item -Path $symLinkToDir
663664
$link.LinkType | Should -BeExactly "SymbolicLink"
664665
$link.Target | Should -BeExactly $real.ToString()
665666
}
666667
It "New-Item can create a directory symbolic link to a directory" -Skip:(-Not $IsWindows) {
667668
New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir > $null
668669
Test-Path $symLinkToDir | Should -BeTrue
669-
$real = Get-Item -Path $realDir
670+
$real = Get-Item -LiteralPath $realDir
670671
$link = Get-Item -Path $symLinkToDir
671672
$link | Should -BeOfType System.IO.DirectoryInfo
672673
$link.LinkType | Should -BeExactly "SymbolicLink"
@@ -677,7 +678,7 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
677678
$target = Split-Path -Leaf $realDir
678679
New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $target > $null
679680
Test-Path $symLinkToDir | Should -BeTrue
680-
$real = Get-Item -Path $realDir
681+
$real = Get-Item -LiteralPath $realDir
681682
$link = Get-Item -Path $symLinkToDir
682683
$link | Should -BeOfType System.IO.DirectoryInfo
683684
$link.LinkType | Should -BeExactly "SymbolicLink"
@@ -689,7 +690,7 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
689690
$target = ".\$(Split-Path -Leaf $realDir)"
690691
New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $target > $null
691692
Test-Path $symLinkToDir | Should -BeTrue
692-
$real = Get-Item -Path $realDir
693+
$real = Get-Item -LiteralPath $realDir
693694
$link = Get-Item -Path $symLinkToDir
694695
$link | Should -BeOfType System.IO.DirectoryInfo
695696
$link.LinkType | Should -BeExactly "SymbolicLink"
@@ -732,8 +733,8 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
732733
}
733734

734735
It "New-Item -Force can overwrite a junction" -Skip:(-Not $IsWindows){
735-
$rd2 = Get-Item -Path $realDir2
736-
New-Item -Name testfile.txt -ItemType file -Path $realDir
736+
F11C $rd2 = Get-Item -LiteralPath $realDir2
737+
New-Item -Name testfile.txt -ItemType file -Path ([WildcardPattern]::Escape($realDir))
737738
New-Item -ItemType Junction -Path $junctionToDir -Value $realDir > $null
738739
Test-Path $junctionToDir | Should -BeTrue
739740
{ New-Item -ItemType Junction -Path $junctionToDir -Value $realDir -ErrorAction Stop > $null } | Should -Throw -ErrorId "DirectoryNotEmpty,Microsoft.PowerShell.Commands.NewItemCommand"
@@ -879,7 +880,7 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
879880

880881
Remove-Item -Path $Link -ErrorAction SilentlyContinue > $null
881882
Test-Path -Path $Link | Should -BeFalse
882-
Test-Path -Path $Target | Should -BeTrue
883+
Test-Path -LiteralPath $Target | Should -BeTrue
883884
}
884885
}
885886

test/powershell/engine/Basic/GroupPolicySettings.Tests.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
Describe 'Group policy settings tests' -Tag CI,RequireAdminOnWindows {
4+
Describe 'Group policy settings tests' -Tags @('CI', 'RequireAdminOnWindows') {
55
BeforeAll {
66
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
77
if ( ! $IsWindows ) {

0 commit comments

Comments
 (0)
0