8000 Fix regression in `Move-Item` to only fallback to CopyAndDelete in sp… · PowerShell/PowerShell@7c9bf37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c9bf37

Browse files
SteveL-MSFTadityapatwardhan
authored andcommitted
Fix regression in Move-Item to only fallback to CopyAndDelete in specific cases (#16029)
If Move fails, check if known case before attempting CopyAndDelete: - if an item is attempted to be renamed across filesystem mount boundaries. - if the source and destination do not have the same root path.
1 parent 6ebc95e commit 7c9bf37

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6121,7 +6121,16 @@ private void MoveDirectoryInfoUnchecked(DirectoryInfo directory, string destinat
61216121

61226122
directory.MoveTo(destinationPath);
61236123
}
6124-
catch (IOException)
6124+
#if UNIX
6125+
// This is the errno returned by the rename() syscall
6126+
// when an item is attempted to be renamed across filesystem mount boundaries.
6127+
// 0x80131620 is returned if the source and destination do not have the same root path
6128+
catch (IOException e) when (e.HResult == 18 || e.HResult == -2146232800)
6129+
#else
6130+
// 0x80070005 ACCESS_DENIED is returned when trying to move files across volumes like DFS
6131+
// 0x80131620 is returned if the source and destination do not have the same root path
6132+
catch (IOException e) when (e.HResult == -2147024891 || e.HResult == -2146232800)
6133+
#endif
61256134
{
61266135
// Rather than try to ascertain whether we can rename a directory ahead of time,
61276136
// it's both faster and more correct to try to rename it and fall back to copy/deleting it

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,23 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
170170
}
171171

172172
It "Verify Move-Item will not move to an existing file" {
173-
{ Move-Item -Path $testDir -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId 'DirectoryExist,Microsoft.PowerShell.Commands.MoveItemCommand'
174-
$error[0].Exception | Should -BeOfType System.IO.IOException
173+
if ($IsWindows) {
174+
$expectedError = 'MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand'
175+
}
176+
else {
177+
$expectedError = 'DirectoryExist,Microsoft.PowerShell.Commands.MoveItemCommand'
178+
}
179+
180+
$e = { Move-Item -Path $testDir -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId $expectedError -PassThru
181+
$e.Exception | Should -BeOfType System.IO.IOException
175182
$testDir | Should -Exist
176183
}
177184

185+
It 'Verify Move-Item fails for non-existing destination path' {
186+
$e = { Move-Item -Path $testDir -Destination TestDrive:/0/2/0 -ErrorAction Stop } | Should -Throw -ErrorId 'MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand' -PassThru
187+
$e.Exception | Should -BeOfType System.IO.IOException
188+
}
189+
178190
It "Verify Move-Item throws correct error for non-existent source" {
179191
{ Move-Item -Path /does/not/exist -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand'
180192
}

0 commit comments

Comments
 (0)
0