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
481 changes: 465 additions & 16 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ internal static IEnumerable<ExtendedTypeDefinition> GetFormatData()

private static IEnumerable<FormatViewDefinition> ViewsOf_FileSystemTypes(CustomControl[] sharedControls)
{
#if UNIX
if (ExperimentalFeature.IsEnabled("PSUnixFileStat"))
{
yield return new FormatViewDefinition("childrenWithUnixStat",
TableControl.Create()
.GroupByProperty("PSParentPath", customControl: sharedControls[0])
.AddHeader(Alignment.Left, label: "UnixMode", width: 10)
.AddHeader(Alignment.Left, label: "User", width: 16)
.AddHeader(Alignment.Left, label: "Group", width: 16)
.AddHeader(Alignment.Right, label: "LastWriteTime", width: 18)
.AddHeader(Alignment.Right, label: "Size", width: 14)
.AddHeader(Alignment.Left, label: "Name")
.StartRowDefinition(wrap: true)
.AddPropertyColumn("UnixMode")
.AddPropertyColumn("User")
.AddPropertyColumn("Group")
.AddScriptBlockColumn(scriptBlock: @"'{0:d} {0:HH}:{0:mm}' -f $_.LastWriteTime")
.AddPropertyColumn("Size")
.AddPropertyColumn("NameString")
.EndRowDefinition()
.EndTable());
}
#endif

yield return new FormatViewDefinition("children",
TableControl.Create()
.GroupByProperty("PSParentPath", customControl: sharedControls[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0-preview3.19551.4" />
<!-- the following package(s) are from the powershell org -->
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0-preview.2" />
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.0.0-preview.3" />
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.0.0-preview.4" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ static ExperimentalFeature()
new ExperimentalFeature(
name: "PSPipelineChainOperators",
description: "Allow use of && and || as operators between pipeline invocations"),
#if UNIX
new ExperimentalFeature(
name: "PSUnixFileStat",
description: "Provide unix permission information for files and directories"),
#endif
new ExperimentalFeature(
name: "PSNullConditionalOperators",
description: "Support the null conditional member access operators in PowerShell language")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9198,6 +9198,53 @@ private void Process_Types_Ps1Xml(string filePath, ConcurrentBag<string> errors)

#endregion System.Version#IncludeLabel

#if UNIX
#region UnixStat


if (ExperimentalFeature.IsEnabled("PSUnixFileStat"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could cache this, especially since it's an engine experimental feature, so it can't be toggled once PS has started

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caching not required for perf reasons. I believe changes to all engine experimental features need a restart of PS.

{
typeName = @"System.IO.FileSystemInfo";
typeMembers = _extendedMembers.GetOrAdd(typeName, GetValueFactoryBasedOnInitCapacity(capacity: 1));

// Where we have a method to invoke below, first check to be sure that the object is present
// to avoid null reference issues
newMembers.Add(@"UnixMode");
AddMember(
errors,
typeName,
new PSScriptProperty(@"UnixMode", GetScriptBlock(@"if ($this.UnixStat) { $this.UnixStat.GetModeString() }")),
typeMembers,
isOverride: false);

newMembers.Add(@"User");
AddMember(
errors,
typeName,
new PSScriptProperty(@"User", GetScriptBlock(@" if ($this.UnixStat) { $this.UnixStat.GetUserName() } ")),
typeMembers,
isOverride: false);

newMembers.Add(@"Group");
AddMember(
errors,
typeName,
new PSScriptProperty(@"Group", GetScriptBlock(@" if ($this.UnixStat) { $this.UnixStat.GetGroupName() } ")),
typeMembers,
isOverride: false);

newMembers.Add(@"Size");
AddMember(
errors,
typeName,
new PSScriptProperty(@"Size", GetScriptBlock(@"$this.UnixStat.Size")),
typeMembers,
isOverride: false);
}

#endregion
#endif

// Update binder version for newly added members.
foreach (string memberName in newMembers)
{
Expand Down
21 changes: 21 additions & 0 deletions src/System.Management.Automation/namespaces/ProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,27 @@ private PSObject WrapOutputInPSObject(

result.AddOrSetProperty("PSChildName", childName);
providerBaseTracer.WriteLine("Attaching {0} = {1}", "PSChildName", childName);
#if UNIX

// Add a commonstat structure to file system objects
if (ExperimentalFeature.IsEnabled("PSUnixFileStat") && ProviderInfo.ImplementingType == typeof(Microsoft.PowerShell.Commands.FileSystemProvider))
{
try
{
// Use LStat because if you get a link, you want the information about the
// link, not the file.
var commonStat = Platform.Unix.GetLStat(path);
result.AddOrSetProperty("UnixStat", commonStat);
}
catch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to catch a specific exception here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, any problem means that we can't add the property, so don't. I'll add a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment

{
// If there is *any* problem in retrieving the stat information
// set the property to null. There is no specific exception which
// would result in different behavior.
result.AddOrSetProperty("UnixStat", value: null);
}
}
#endif
}

// PSDriveInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "UnixFileSystem additions" -Tag "CI" {
# if PSUnixFileStat is converted from an experimental feature, these tests will need to be changed
BeforeAll {
$experimentalFeatureName = "PSUnixFileStat"
$skipTest = -not $EnabledExperimentalFeatures.Contains($experimentalFeatureName)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider setting PSDefaultParameterValue for Skip

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

$PSDefaultParameterValues.Add('It:Skip', $skipTest)
}
AfterAll {
$PSDefaultParameterValues.Remove('It:Skip')
}
Context "Basic Validation" {

It "Should be an experimental feature on non-Windows systems" {
$feature = Get-ExperimentalFeature -Name $experimentalFeatureName
if ( $IsWindows ) {
$feature | Should -BeNullOrEmpty
}
else {
$feature.Name | Should -Be $experimentalFeatureName
}
}

It "Should include a UnixStat property" {
$i = Get-Item ${TestDrive}
$i.UnixStat | Should -Not -BeNullOrEmpty
}

It "The UnixStat property should be the correct type" {
$expected = "System.Management.Automation.Platform+Unix+CommonStat"
$i = (get-item /).psobject.properties['UnixStat'].TypeNameOfValue
$i | Should -Be $expected
}
}

Context "Validation of additional properties on file system objects" {
BeforeAll {
if ( $IsWindows ) {
return
}

$testDir = "${TestDrive}/TestDir"
$testFile = "${testDir}/TestFile"

$testCase = @{ Mode = '000'; Perm = '----------'; Item = "${testFile}" },
@{ Mode = '111'; Perm = '---x--x--x'; Item = "${testFile}" },
@{ Mode = '222'; Perm = '--w--w--w-'; Item = "${testFile}" },
@{ Mode = '333'; Perm = '--wx-wx-wx'; Item = "${testFile}" },
@{ Mode = '444'; Perm = '-r--r--r--'; Item = "${testFile}" },
@{ Mode = '555'; Perm = '-r-xr-xr-x'; Item = "${testFile}" },
@{ Mode = '666'; Perm = '-rw-rw-rw-'; Item = "${testFile}" },
@{ Mode = '777'; Perm = '-rwxrwxrwx'; Item = "${testFile}" },
@{ Mode = '4777'; Perm = '-rwsrwxrwx'; Item = "${testFile}" },
@{ Mode = '1777'; Perm = 'drwxrwxrwt'; Item = "${testDir}" }
}

BeforeEach {
$null = New-Item -ItemType Directory -Path "${testDir}"
$null = New-Item -ItemType File -Path "${testFile}"
}

AfterEach {
Remove-Item -Path "${testFile}" -Force
Remove-Item -Path "${testDir}" -Recurse -Force
}

It "Should present filemode '<Mode>' string correctly as '<Perm>'" -testCase $testCase {
param ($Mode, $Perm, $Item )
chmod "$Mode" "${Item}"
$i = Get-Item $Item
$i.UnixMode | Should -Be $Perm
}

It "Should retrieve the user name for the file" {
$i = Get-Item ${testFile}
$user = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[2]
$i.User | Should -Be $user
}

It "Should retrieve the group name for the file" {
$i = Get-Item ${testFile}
$group = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[3]
$i.Group | Should -Be $Group
}
}

Context "Other properties of UnixStat object" {
BeforeAll {
if ( $IsWindows ) {
return
}

$testDir = "${TestDrive}/TestDir"
$testFile = "${testDir}/TestFile"

$null = New-Item -Type Directory -Path $testDir
$null = New-Item -Type File -Path $testFile
Set-Content -Path ${testFile} -Value "abc"

$expectedFileInode,$permission,$expectedFileLinkCount,$user,$group,$expectedFileSize,$unused =
(/bin/ls -ldi ${testFile}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries")
$file = Get-Item ${testFile}

$expectedDirInode,$permission,$expectedDirLinkCount,$user,$group,$expectedDirSize,$unused =
(/bin/ls -ldi ${testDir}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries")
$Dir = Get-Item ${testDir}

$testCases =
@{ Expected = $expectedFileInode; Observed = $File.UnixStat.Inode; Title = "FileInode" },
@{ Expected = $expectedFileLinkCount; Observed = $File.UnixStat.HardlinkCount; Title = "FileHardlinkCount" },
@{ Expected = $expectedFileSize; Observed = $File.UnixStat.Size; Title = "FileSize" },
@{ Expected = $expectedDirInode; Observed = $Dir.UnixStat.Inode; Title = "DirInode" },
@{ Expected = $expectedDirLinkCount; Observed = $Dir.UnixStat.HardlinkCount; Title = "DirHardlinkCount" },
@{ Expected = $expectedDirSize; Observed = $Dir.UnixStat.Size; Title = "DirSize" }
}

It "Should have correct values in UnixStat property for '<Title>'" -TestCases $testCases {
param ( $Title, $expected, $observed )

$observed | Should -Be $expected

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Describe "Get-FormatData" -Tags "CI" {
$format.TypeNames | Should -HaveCount 2
$format.TypeNames[0] | Should -BeExactly "System.IO.DirectoryInfo"
$format.TypeNames[1] | Should -BeExactly "System.IO.FileInfo"
$format.FormatViewDefinition | Should -HaveCount 4
$format.FormatViewDefinition | Should -HaveCount ($IsWindows ? 4 : 5)
}

It "Should return nothing for format data requiring '-PowerShellVersion 5.1' and not provided" {
Expand Down
2 changes: 1 addition & 1 deletion test/powershell/engine/ETS/TypeTable.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Describe "Built-in type information tests" -Tag "CI" {
}

It "Should have correct number of built-in type items in type table" {
$types.Count | Should -BeExactly ($IsWindows ? 273 : 271)
$types.Count | Should -BeExactly ($IsWindows ? 273 : 272)
}

It "Should have expected member info for 'System.Diagnostics.ProcessModule'" {
Expand Down