Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions src/System.Management.Automation/namespaces/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2485,40 +2485,35 @@ protected override void NewItem(
}

// Junctions cannot have files
if (DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo))
if (!Force && DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo))
{
string message = StringUtil.Format(FileSystemProviderStrings.DirectoryNotEmpty, path);
WriteError(new ErrorRecord(new IOException(message), "DirectoryNotEmpty", ErrorCategory.WriteError, path));
return;
}

if (Force)
try
{
try
pathDirInfo.Delete();
}
catch (Exception exception)
{
if ((exception is DirectoryNotFoundException) ||
(exception is UnauthorizedAccessException) ||
(exception is System.Security.SecurityException) ||
(exception is IOException))
{
pathDirInfo.Delete();
WriteError(new ErrorRecord(exception, "NewItemDeleteIOError", ErrorCategory.WriteError, path));
}
catch (Exception exception)
else
{
if ((exception is DirectoryNotFoundException) ||
(exception is UnauthorizedAccessException) ||
(exception is System.Security.SecurityException) ||
(exception is IOException))
{
WriteError(new ErrorRecord(exception, "NewItemDeleteIOError", ErrorCategory.WriteError, path));
}
else
{
throw;
}
throw;
}
}
}
else
{
CreateDirectory(path, false);
pathDirInfo = new DirectoryInfo(path);
}

CreateDirectory(path, streamOutput: false);
pathDirInfo = new DirectoryInfo(path);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
$nonFile = Join-Path $TestPath "not-a-file"
$fileContent = "some text"
$realDir = Join-Path $TestPath "subdir"
$realDir2 = Join-Path $TestPath "second-subdir"
$nonDir = Join-Path $TestPath "not-a-dir"
$hardLinkToFile = Join-Path $TestPath "hard-to-file.txt"
$symLinkToFile = Join-Path $TestPath "sym-link-to-file.txt"
Expand All @@ -560,6 +561,7 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"

New-Item -ItemType File -Path $realFile -Value $fileContent > $null
New-Item -ItemType Directory -Path $realDir > $null
New-Item -ItemType Directory -Path $realDir2 > $null
}

Context "New-Item and hard/symbolic links" {
Expand Down Expand Up @@ -628,6 +630,17 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
$i = New-Item -ItemType File -Path "$TestDrive\file.txt" -Force -ErrorAction Ignore
{ New-Item -ItemType HardLink -Path $i -Target $i -Force -ErrorAction Stop } | Should -Throw -ErrorId "TargetIsSameAsLink,Microsoft.PowerShell.Commands.NewItemCommand"
}

It "New-Item -Force can overwrite a junction" -Skip:(-Not $IsWindows){
$rd2 = Get-Item -Path $realDir2
New-Item -Name testfile.txt -ItemType file -Path $realDir
New-Item -ItemType Junction -Path $junctionToDir -Value $realDir > $null
Test-Path $junctionToDir | Should -BeTrue
{ New-Item -ItemType Junction -Path $junctionToDir -Value $realDir -ErrorAction Stop > $null } | Should -Throw -ErrorId "DirectoryNotEmpty,Microsoft.PowerShell.Commands.NewItemCommand"
New-Item -ItemType Junction -Path $junctionToDir -Value $realDir2 -Force > $null
$Junction = Get-Item -Path $junctionToDir
$Junction.Target | Should -BeExactly $rd2.ToString()
}
}

Context "Get-ChildItem and symbolic links" {
Expand Down