Skip to content

Commit fe712f8

Browse files
JamesWTruheradityapatwardhan
authored andcommitted
Experimental Feature: Provide Unix stat information in filesystem output (#11042)
1 parent f32ae64 commit fe712f8

9 files changed

Lines changed: 691 additions & 19 deletions

File tree

‎src/System.Management.Automation/CoreCLR/CorePsPlatform.cs‎

Lines changed: 465 additions & 16 deletions
Large diffs are not rendered by default.

‎src/System.Management.Automation/FormatAndOutput/DefaultFormatters/FileSystem_format_ps1xml.cs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ internal static IEnumerable<ExtendedTypeDefinition> GetFormatData()
4141

4242
private static IEnumerable<FormatViewDefinition> ViewsOf_FileSystemTypes(CustomControl[] sharedControls)
4343
{
44+
#if UNIX
45+
if (ExperimentalFeature.IsEnabled("PSUnixFileStat"))
46+
{
47+
yield return new FormatViewDefinition("childrenWithUnixStat",
48+
TableControl.Create()
49+
.GroupByProperty("PSParentPath", customControl: sharedControls[0])
50+
.AddHeader(Alignment.Left, label: "UnixMode", width: 10)
51+
.AddHeader(Alignment.Left, label: "User", width: 16)
52+
.AddHeader(Alignment.Left, label: "Group", width: 16)
53+
.AddHeader(Alignment.Right, label: "LastWriteTime", width: 18)
54+
.AddHeader(Alignment.Right, label: "Size", width: 14)
55+
.AddHeader(Alignment.Left, label: "Name")
56+
.StartRowDefinition(wrap: true)
57+
.AddPropertyColumn("UnixMode")
58+
.AddPropertyColumn("User")
59+
.AddPropertyColumn("Group")
60+
.AddScriptBlockColumn(scriptBlock: @"'{0:d} {0:HH}:{0:mm}' -f $_.LastWriteTime")
61+
.AddPropertyColumn("Size")
62+
.AddPropertyColumn("NameString")
63+
.EndRowDefinition()
64+
.EndTable());
65+
}
66+
#endif
67+
4468
yield return new FormatViewDefinition("children",
4569
TableControl.Create()
4670
.GroupByProperty("PSParentPath", customControl: sharedControls[0])

‎src/System.Management.Automation/System.Management.Automation.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0-preview3.19551.4" />
2929
<!-- the following package(s) are from the powershell org -->
3030
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0-preview.2" />
31-
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.0.0-preview.3" />
31+
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.0.0-preview.4" />
3232
</ItemGroup>
3333

3434
<PropertyGroup>

‎src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ static ExperimentalFeature()
127127
new ExperimentalFeature(
128128
name: "PSPipelineChainOperators",
129129
description: "Allow use of && and || as operators between pipeline invocations"),
130+
#if UNIX
131+
new ExperimentalFeature(
132+
name: "PSUnixFileStat",
133+
description: "Provide unix permission information for files and directories"),
134+
#endif
130135
new ExperimentalFeature(
131136
name: "PSNullConditionalOperators",
132137
description: "Support the null conditional member access operators in PowerShell language")

‎src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9198,6 +9198,53 @@ private void Process_Types_Ps1Xml(string filePath, ConcurrentBag<string> errors)
91989198

91999199
#endregion System.Version#IncludeLabel
92009200

9201+
#if UNIX
9202+
#region UnixStat
9203+
9204+
9205+
if (ExperimentalFeature.IsEnabled("PSUnixFileStat"))
9206+
{
9207+
typeName = @"System.IO.FileSystemInfo";
9208+
typeMembers = _extendedMembers.GetOrAdd(typeName, GetValueFactoryBasedOnInitCapacity(capacity: 1));
9209+
9210+
// Where we have a method to invoke below, first check to be sure that the object is present
9211+
// to avoid null reference issues
9212+
newMembers.Add(@"UnixMode");
9213+
AddMember(
9214+
errors,
9215+
typeName,
9216+
new PSScriptProperty(@"UnixMode", GetScriptBlock(@"if ($this.UnixStat) { $this.UnixStat.GetModeString() }")),
9217+
typeMembers,
9218+
isOverride: false);
9219+
9220+
newMembers.Add(@"User");
9221+
AddMember(
9222+
errors,
9223+
typeName,
9224+
new PSScriptProperty(@"User", GetScriptBlock(@" if ($this.UnixStat) { $this.UnixStat.GetUserName() } ")),
9225+
typeMembers,
9226+
isOverride: false);
9227+
9228+
newMembers.Add(@"Group");
9229+
AddMember(
9230+
errors,
9231+
typeName,
9232+
new PSScriptProperty(@"Group", GetScriptBlock(@" if ($this.UnixStat) { $this.UnixStat.GetGroupName() } ")),
9233+
typeMembers,
9234+
isOverride: false);
9235+
9236+
newMembers.Add(@"Size");
9237+
AddMember(
9238+
errors,
9239+
typeName,
9240+
new PSScriptProperty(@"Size", GetScriptBlock(@"$this.UnixStat.Size")),
9241+
typeMembers,
9242+
isOverride: false);
9243+
}
9244+
9245+
#endregion
9246+
#endif
9247+
92019248
// Update binder version for newly added members.
92029249
foreach (string memberName in newMembers)
92039250
{

‎src/System.Management.Automation/namespaces/ProviderBase.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,27 @@ private PSObject WrapOutputInPSObject(
18621862

18631863
result.AddOrSetProperty("PSChildName", childName);
18641864
providerBaseTracer.WriteLine("Attaching {0} = {1}", "PSChildName", childName);
1865+
#if UNIX
1866+
1867+
// Add a commonstat structure to file system objects
1868+
if (ExperimentalFeature.IsEnabled("PSUnixFileStat") && ProviderInfo.ImplementingType == typeof(Microsoft.PowerShell.Commands.FileSystemProvider))
1869+
{
1870+
try
1871+
{
1872+
// Use LStat because if you get a link, you want the information about the
1873+
// link, not the file.
1874+
var commonStat = Platform.Unix.GetLStat(path);
1875+
result.AddOrSetProperty("UnixStat", commonStat);
1876+
}
1877+
catch
1878+
{
1879+
// If there is *any* problem in retrieving the stat information
1880+
// set the property to null. There is no specific exception which
1881+
// would result in different behavior.
1882+
result.AddOrSetProperty("UnixStat", value: null);
1883+
}
1884+
}
1885+
#endif
18651886
}
18661887

18671888
// PSDriveInfo
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
Describe "UnixFileSystem additions" -Tag "CI" {
4+
# if PSUnixFileStat is converted from an experimental feature, these tests will need to be changed
5+
BeforeAll {
6+
$experimentalFeatureName = "PSUnixFileStat"
7+
$skipTest = -not $EnabledExperimentalFeatures.Contains($experimentalFeatureName)
8+
$PSDefaultParameterValues.Add('It:Skip', $skipTest)
9+
}
10+
AfterAll {
11+
$PSDefaultParameterValues.Remove('It:Skip')
12+
}
13+
Context "Basic Validation" {
14+
15+
It "Should be an experimental feature on non-Windows systems" {
16+
$feature = Get-ExperimentalFeature -Name $experimentalFeatureName
17+
if ( $IsWindows ) {
18+
$feature | Should -BeNullOrEmpty
19+
}
20+
else {
21+
$feature.Name | Should -Be $experimentalFeatureName
22+
}
23+
}
24+
25+
It "Should include a UnixStat property" {
26+
$i = Get-Item ${TestDrive}
27+
$i.UnixStat | Should -Not -BeNullOrEmpty
28+
}
29+
30+
It "The UnixStat property should be the correct type" {
31+
$expected = "System.Management.Automation.Platform+Unix+CommonStat"
32+
$i = (get-item /).psobject.properties['UnixStat'].TypeNameOfValue
33+
$i | Should -Be $expected
34+
}
35+
}
36+
37+
Context "Validation of additional properties on file system objects" {
38+
BeforeAll {
39+
if ( $IsWindows ) {
40+
return
41+
}
42+
43+
$testDir = "${TestDrive}/TestDir"
44+
$testFile = "${testDir}/TestFile"
45+
46+
$testCase = @{ Mode = '000'; Perm = '----------'; Item = "${testFile}" },
47+
@{ Mode = '111'; Perm = '---x--x--x'; Item = "${testFile}" },
48+
@{ Mode = '222'; Perm = '--w--w--w-'; Item = "${testFile}" },
49+
@{ Mode = '333'; Perm = '--wx-wx-wx'; Item = "${testFile}" },
50+
@{ Mode = '444'; Perm = '-r--r--r--'; Item = "${testFile}" },
51+
@{ Mode = '555'; Perm = '-r-xr-xr-x'; Item = "${testFile}" },
52+
@{ Mode = '666'; Perm = '-rw-rw-rw-'; Item = "${testFile}" },
53+
@{ Mode = '777'; Perm = '-rwxrwxrwx'; Item = "${testFile}" },
54+
@{ Mode = '4777'; Perm = '-rwsrwxrwx'; Item = "${testFile}" },
55+
@{ Mode = '1777'; Perm = 'drwxrwxrwt'; Item = "${testDir}" }
56+
}
57+
58+
BeforeEach {
59+
$null = New-Item -ItemType Directory -Path "${testDir}"
60+
$null = New-Item -ItemType File -Path "${testFile}"
61+
}
62+
63+
AfterEach {
64+
Remove-Item -Path "${testFile}" -Force
65+
Remove-Item -Path "${testDir}" -Recurse -Force
66+
}
67+
68+
It "Should present filemode '<Mode>' string correctly as '<Perm>'" -testCase $testCase {
69+
param ($Mode, $Perm, $Item )
70+
chmod "$Mode" "${Item}"
71+
$i = Get-Item $Item
72+
$i.UnixMode | Should -Be $Perm
73+
}
74+
75+
It "Should retrieve the user name for the file" {
76+
$i = Get-Item ${testFile}
77+
$user = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[2]
78+
$i.User | Should -Be $user
79+
}
80+
81+
It "Should retrieve the group name for the file" {
82+
$i = Get-Item ${testFile}
83+
$group = (/bin/ls -ld $testFile).split(" ",[System.StringSplitOptions]"RemoveEmptyEntries")[3]
84+
$i.Group | Should -Be $Group
85+
}
86+
}
87+
88+
Context "Other properties of UnixStat object" {
89+
BeforeAll {
90+
if ( $IsWindows ) {
91+
return
92+
}
93+
94+
$testDir = "${TestDrive}/TestDir"
95+
$testFile = "${testDir}/TestFile"
96+
97+
$null = New-Item -Type Directory -Path $testDir
98+
$null = New-Item -Type File -Path $testFile
99+
Set-Content -Path ${testFile} -Value "abc"
100+
101+
$expectedFileInode,$permission,$expectedFileLinkCount,$user,$group,$expectedFileSize,$unused =
102+
(/bin/ls -ldi ${testFile}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries")
103+
$file = Get-Item ${testFile}
104+
105+
$expectedDirInode,$permission,$expectedDirLinkCount,$user,$group,$expectedDirSize,$unused =
106+
(/bin/ls -ldi ${testDir}).Split(" ", 7, [System.StringSplitOptions]"RemoveEmptyEntries")
107+
$Dir = Get-Item ${testDir}
108+
109+
$testCases =
110+
@{ Expected = $expectedFileInode; Observed = $File.UnixStat.Inode; Title = "FileInode" },
111+
@{ Expected = $expectedFileLinkCount; Observed = $File.UnixStat.HardlinkCount; Title = "FileHardlinkCount" },
112+
@{ Expected = $expectedFileSize; Observed = $File.UnixStat.Size; Title = "FileSize" },
113+
@{ Expected = $expectedDirInode; Observed = $Dir.UnixStat.Inode; Title = "DirInode" },
114+
@{ Expected = $expectedDirLinkCount; Observed = $Dir.UnixStat.HardlinkCount; Title = "DirHardlinkCount" },
115+
@{ Expected = $expectedDirSize; Observed = $Dir.UnixStat.Size; Title = "DirSize" }
116+
}
117+
118+
It "Should have correct values in UnixStat property for '<Title>'" -TestCases $testCases {
119+
param ( $Title, $expected, $observed )
120+
121+
$observed | Should -Be $expected
122+
123+
}
124+
}
125+
126+
}

‎test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FormatData.Tests.ps1‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Describe "Get-FormatData" -Tags "CI" {
1515
$format.TypeNames | Should -HaveCount 2
1616
$format.TypeNames[0] | Should -BeExactly "System.IO.DirectoryInfo"
1717
$format.TypeNames[1] | Should -BeExactly "System.IO.FileInfo"
18-
$format.FormatViewDefinition | Should -HaveCount 4
18+
$format.FormatViewDefinition | Should -HaveCount ($IsWindows ? 4 : 5)
1919
}
2020

2121
It "Should return nothing for format data requiring '-PowerShellVersion 5.1' and not provided" {

‎test/powershell/engine/ETS/TypeTable.Tests.ps1‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Describe "Built-in type information tests" -Tag "CI" {
1818
}
1919

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

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

0 commit comments

Comments
 (0)