From c329021c545f0369f84d261904d2506ccc337b36 Mon Sep 17 00:00:00 2001 From: Olivia Date: Mon, 17 Oct 2022 19:03:34 +1300 Subject: [PATCH 01/10] Refactor New-Item for Junction to allow force on Dir with Childitems --- .../namespaces/FileSystemProvider.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 1db69c2a125..44fae8b80c2 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2484,14 +2484,6 @@ protected override void NewItem( return; } - // Junctions cannot have files - if (DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo)) - { - string message = StringUtil.Format(FileSystemProviderStrings.DirectoryNotEmpty, path); - WriteError(new ErrorRecord(new IOException(message), "DirectoryNotEmpty", ErrorCategory.WriteError, path)); - return; - } - if (Force) { try @@ -2515,7 +2507,15 @@ protected override void NewItem( } } else - { + { + // Junctions cannot have files + if (DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo)) + { + string message = StringUtil.Format(FileSystemProviderStrings.DirectoryNotEmpty, path); + WriteError(new ErrorRecord(new IOException(message), "DirectoryNotEmpty", ErrorCategory.WriteError, path)); + return; + } + CreateDirectory(path, false); pathDirInfo = new DirectoryInfo(path); } From 63a9afff96938752ce22bbada04fa23b45dcb425 Mon Sep 17 00:00:00 2001 From: Olivia Date: Mon, 17 Oct 2022 19:43:59 +1300 Subject: [PATCH 02/10] Put on my glasses before moving code --- .../namespaces/FileSystemProvider.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 44fae8b80c2..ad58d4385f6 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2505,17 +2505,19 @@ protected override void NewItem( } } } + else + { + // Junctions cannot have files + if (DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo)) + { + string message = StringUtil.Format(FileSystemProviderStrings.DirectoryNotEmpty, path); + WriteError(new ErrorRecord(new IOException(message), "DirectoryNotEmpty", ErrorCategory.WriteError, path)); + return; + } + } } else { - // Junctions cannot have files - if (DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo)) - { - string message = StringUtil.Format(FileSystemProviderStrings.DirectoryNotEmpty, path); - WriteError(new ErrorRecord(new IOException(message), "DirectoryNotEmpty", ErrorCategory.WriteError, path)); - return; - } - CreateDirectory(path, false); pathDirInfo = new DirectoryInfo(path); } From 81d66c4fad92752046cee1f74cc4dae84e123e3b Mon Sep 17 00:00:00 2001 From: Olivia Date: Mon, 17 Oct 2022 21:30:29 +1300 Subject: [PATCH 03/10] Recreate path after delete. --- .../namespaces/FileSystemProvider.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index ad58d4385f6..832489ed47f 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2489,6 +2489,8 @@ protected override void NewItem( try { pathDirInfo.Delete(); + CreateDirectory(path, false); + pathDirInfo = new DirectoryInfo(path); } catch (Exception exception) { @@ -2517,7 +2519,7 @@ protected override void NewItem( } } else - { + { CreateDirectory(path, false); pathDirInfo = new DirectoryInfo(path); } From 9ee044eb5eb00860f3ed162d4cdd012035a7e462 Mon Sep 17 00:00:00 2001 From: Olivia Date: Tue, 18 Oct 2022 21:09:13 +1300 Subject: [PATCH 04/10] Add target check to test (WIP) --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 0f5222b3db5..d945fba1876 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -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" @@ -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" { From dda7688882d10e6da11d1a8f2d6b10c414f083b5 Mon Sep 17 00:00:00 2001 From: Olivia Date: Wed, 19 Oct 2022 17:35:25 +1300 Subject: [PATCH 05/10] add setup to get error to throw --- .../FileSystem.Tests.ps1 | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index d945fba1876..f9139e8a8c5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -630,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 > $null -ErrorAction Stop } | Should -Throw -ErrorId "DirectoryNotEmpty,Microsoft.PowerShell.Commands.NewItemCommand" + New-Item -ItemType Junction -Path $junctionToDir -Value $realDir2 > $null -Force + $Junction = Get-Item -Path $junctionToDir + $Junction.Target | Should -BeExactly $rd2.ToString() + } } Context "Get-ChildItem and symbolic links" { From 8fe15e55cf61e3adda2623ab5f019e3f7189403c Mon Sep 17 00:00:00 2001 From: Olivia Date: Wed, 19 Oct 2022 18:07:51 +1300 Subject: [PATCH 06/10] Move lines out of try/catch to avoid race condition --- .../namespaces/FileSystemProvider.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 832489ed47f..67d5972e77c 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2489,8 +2489,6 @@ protected override void NewItem( try { pathDirInfo.Delete(); - CreateDirectory(path, false); - pathDirInfo = new DirectoryInfo(path); } catch (Exception exception) { @@ -2506,7 +2504,9 @@ protected override void NewItem( throw; } } - } + CreateDirectory(path, false); + pathDirInfo = new DirectoryInfo(path); + } else { // Junctions cannot have files From 575d4447944b434add048febcb8626625b9b5cf3 Mon Sep 17 00:00:00 2001 From: Olivia Date: Wed, 19 Oct 2022 18:10:06 +1300 Subject: [PATCH 07/10] caught fleeing brace --- .../namespaces/FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 67d5972e77c..2a9d5285d08 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2506,7 +2506,7 @@ protected override void NewItem( } CreateDirectory(path, false); pathDirInfo = new DirectoryInfo(path); - } + } else { // Junctions cannot have files From c6288ef70adc89f7d069223fab9d9e408452841c Mon Sep 17 00:00:00 2001 From: Olivia Date: Wed, 19 Oct 2022 18:26:49 +1300 Subject: [PATCH 08/10] Update src/System.Management.Automation/namespaces/FileSystemProvider.cs Co-authored-by: Ilya --- .../namespaces/FileSystemProvider.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 2a9d5285d08..e73cf5a7de0 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2504,6 +2504,7 @@ protected override void NewItem( throw; } } + CreateDirectory(path, false); pathDirInfo = new DirectoryInfo(path); } From 53a65d55c4ccef30f24d17ada2fdd4d7d839f371 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 1 May 2023 22:47:42 -0700 Subject: [PATCH 09/10] Apply a fix to the change --- .../namespaces/FileSystemProvider.cs | 54 ++++++++----------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index e73cf5a7de0..b8d8e6526af 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2484,46 +2484,36 @@ protected override void NewItem( return; } - if (Force) + // Junctions cannot have files + if (!Force && DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo)) { - try - { - pathDirInfo.Delete(); - } - catch (Exception exception) - { - 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; - } - } + string message = StringUtil.Format(FileSystemProviderStrings.DirectoryNotEmpty, path); + WriteError(new ErrorRecord(new IOException(message), "DirectoryNotEmpty", ErrorCategory.WriteError, path)); + return; + } - CreateDirectory(path, false); - pathDirInfo = new DirectoryInfo(path); + try + { + pathDirInfo.Delete(); } - else + catch (Exception exception) { - // Junctions cannot have files - if (DirectoryInfoHasChildItems((DirectoryInfo)pathDirInfo)) + if ((exception is DirectoryNotFoundException) || + (exception is UnauthorizedAccessException) || + (exception is System.Security.SecurityException) || + (exception is IOException)) { - string message = StringUtil.Format(FileSystemProviderStrings.DirectoryNotEmpty, path); - WriteError(new ErrorRecord(new IOException(message), "DirectoryNotEmpty", ErrorCategory.WriteError, path)); - return; + WriteError(new ErrorRecord(exception, "NewItemDeleteIOError", ErrorCategory.WriteError, path)); + } + else + { + throw; } } } - else - { - CreateDirectory(path, false); - pathDirInfo = new DirectoryInfo(path); - } + + CreateDirectory(path, streamOutput: false); + pathDirInfo = new DirectoryInfo(path); try { From 869cb6cb6e8bc6ec5bc845197f7138b67c6974b5 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 1 May 2023 22:57:32 -0700 Subject: [PATCH 10/10] Minor change to the test --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index f9139e8a8c5..5849d2ea9fb 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -636,8 +636,8 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows" 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 > $null -ErrorAction Stop } | Should -Throw -ErrorId "DirectoryNotEmpty,Microsoft.PowerShell.Commands.NewItemCommand" - New-Item -ItemType Junction -Path $junctionToDir -Value $realDir2 > $null -Force + { 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() }