Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of long paths #3960

Merged
merged 2 commits into from
Jun 8, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,8 +1153,22 @@ internal static bool PathIsUnc(string path)

internal class NativeMethods
{
[DllImport(PinvokeDllNames.GetFileAttributesDllName, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int GetFileAttributes(string lpFileName);
private static string EnsureLongPathPrefixIfNeeded(string path)
{
if (path.Length >= MAX_PATH && !path.StartsWith(@"\\?\", StringComparison.Ordinal))
return @"\\?\" + path;

return path;
}

[DllImport(PinvokeDllNames.GetFileAttributesDllName, EntryPoint = "GetFileAttributesW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetFileAttributesPrivate(string lpFileName);

internal static int GetFileAttributes(string fileName)
{
fileName = EnsureLongPathPrefixIfNeeded(fileName);
return GetFileAttributesPrivate(fileName);
}

[Flags]
internal enum FileAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,47 @@ Describe "Copy-Item can avoid copying an item onto itself" -Tags "CI", "RequireA
}
}

Describe "Handling long paths" -Tags "CI" {
BeforeAll {
$longDir = 'a' * 250
$longSubDir = 'b' * 250
$fileName = "file1.txt"
$topPath = Join-Path $TestDrive $longDir
$longDirPath = Join-Path $topPath $longSubDir
$longFilePath = Join-Path $longDirPath $fileName
$cwd = Get-Location
}
BeforeEach {
New-Item -ItemType File -Path $longFilePath -Force | Out-Null
}
AfterEach {
Remove-Item -Path $topPath -Force -Recurse -ErrorAction SilentlyContinue
Set-Location $cwd
}

It "Can remove a file via a long path" {
Remove-Item -Path $longFilePath -ErrorVariable e -ErrorAction SilentlyContinue
$e | Should BeNullOrEmpty
$longFilePath | Should Not Exist
}
It "Can rename a file via a long path" {
$newFileName = "new-file.txt"
$newPath = Join-Path $longDirPath $newFileName
Rename-Item -Path $longFilePath -NewName $newFileName
$longFilePath | Should Not Exist
$newPath | Should Exist
}
It "Can change into a directory via a long path" {
Set-Location -Path $longDirPath -ErrorVariable e -ErrorAction SilentlyContinue
$e | Should BeNullOrEmpty
$c = Get-Location
$fileName | Should Exist
}
It "Can use Test-Path to check for a file via a long path" {
Test-Path $longFilePath | Should Be $true
}
}

Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature" {
BeforeAll {
$testDir = "testDir"
Expand Down