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
Original file line number Diff line number Diff line change
Expand Up @@ -5851,6 +5851,15 @@ protected override void MoveItem(
destination = MakePath(destination, dir.Name);
}

// Don't allow moving a directory into itself
if (destination.StartsWith(Path.TrimEndingDirectorySeparator(path) + Path.DirectorySeparatorChar))
{
string error = StringUtil.Format(FileSystemProviderStrings.TargetCannotBeSubdirectoryOfSource, destination);
var e = new IOException(error);
WriteError(new ErrorRecord(e, "MoveItemArgumentError", ErrorCategory.InvalidArgument, destination));
return;
}

// Get the confirmation text
string action = FileSystemProviderStrings.MoveItemActionDirectory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,7 @@
<data name="AlreadyListedDirectory" xml:space="preserve">
<value>Skip already-visited directory {0}.</value>
</data>
<data name="TargetCannotBeSubdirectoryOfSource" xml:space="preserve">
<value>Destination path cannot be a subdirectory of the source: {0}.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
$e.Exception | Should -BeOfType System.IO.IOException
}

It 'Verify Move-Item fails for destination that is subdir of source with trailing: <trailingChar>' -TestCases @(
@{ trailingChar = [System.IO.Path]::DirectorySeparatorChar }
@{ trailingChar = [System.IO.Path]::AltDirectorySeparatorChar }
@{ trailingChar = '' }
) {
param($trailingChar)

$dest = Join-Path -Path $TestDrive -ChildPath dest
$null = New-item -ItemType Directory -Path $dest -Force -ErrorAction Stop
$src = "$TestDrive$trailingChar"

{ Move-Item -Path $src -Destination $dest -ErrorAction Stop } | Should -Throw -ErrorId 'MoveItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand'
}

It "Verify Move-Item throws correct error for non-existent source" {
{ Move-Item -Path /does/not/exist -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand'
}
Expand Down