Skip to content

Commit 2d83b95

Browse files
authored
Fix Move-Item to throw error when moving into itself (#24004)
1 parent ed843f0 commit 2d83b95

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5838,8 +5838,10 @@ protected override void MoveItem(
58385838
destination = MakePath(destination, dir.Name);
58395839
}
58405840

5841-
// Don't allow moving a directory into itself
5842-
if (destination.StartsWith(Path.TrimEndingDirectorySeparator(path) + Path.DirectorySeparatorChar))
5841+
// Don't allow moving a directory into itself or its sub-directory.
5842+
string pathWithoutEndingSeparator = Path.TrimEndingDirectorySeparator(path);
5843+
if (destination.StartsWith(pathWithoutEndingSeparator + Path.DirectorySeparatorChar)
5844+
|| destination.Equals(pathWithoutEndingSeparator, StringComparison.OrdinalIgnoreCase))
58435845
{
58445846
string error = StringUtil.Format(FileSystemProviderStrings.TargetCannotBeSubdirectoryOfSource, destination);
58455847
var e = new IOException(error);

src/System.Management.Automation/resources/FileSystemProviderStrings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@
334334
<value>Skip already-visited directory {0}.</value>
335335
</data>
336336
<data name="TargetCannotBeSubdirectoryOfSource" xml:space="preserve">
337-
<value>Destination path cannot be a subdirectory of the source: {0}.</value>
337+
<value>Destination path cannot be a subdirectory of the source or the source itself: {0}.</value>
338338
</data>
339339
<data name="NewItemTargetIsSameAsLink" xml:space="preserve">
340340
<value>The target and path cannot be the same.</value>

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,25 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
201201
{ Move-Item -Path $src -Destination $dest -ErrorAction Stop } | Should -Throw -ErrorId 'MoveItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand'
202202
}
203203

204+
It 'Verify Move-Item fails when destination is same as source w/wo directory separator: <source>' -TestCases @(
205+
@{ source = './Empty/' }
206+
@{ source = './Empty' }
207+
@{ source = '.\Empty\' }
208+
@{ source = '.\Empty' }
209+
) {
210+
param($source)
211+
212+
try {
213+
Push-Location $TestDrive
214+
New-Item -ItemType Directory -Path 'Empty'
215+
{ Move-Item -Path $source -ErrorAction Stop } | Should -Throw -ErrorId 'MoveItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand'
216+
}
217+
finally {
218+
Pop-Location
219+
Remove-Item 'Empty' -Force
220+
}
221+
}
222+
204223
It "Verify Move-Item throws correct error for non-existent source" {
205224
{ Move-Item -Path /does/not/exist -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand'
206225
}

0 commit comments

Comments
 (0)