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

Get-Item returns invalid FileName when listing alternate data streams #18018

Closed
5 tasks done
ovdeathiam opened this issue Sep 2, 2022 · 8 comments · May be fixed by #18019
Closed
5 tasks done

Get-Item returns invalid FileName when listing alternate data streams #18018

ovdeathiam opened this issue Sep 2, 2022 · 8 comments · May be fixed by #18019
Labels
In-PR Indicates that a PR is out for the issue Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more

Comments

@ovdeathiam
Copy link

ovdeathiam commented Sep 2, 2022

Prerequisites

Steps to reproduce

Whenever trying to list alternate data stream names and filenames with Get-Item the stream name is removed from FileName property value.

  1. Create a file named test.txt with stream name t
    Set-Content -Path C:\test.txt -Stream t -Value testvalue
  2. List available data stream using Get-Item
    Get-Item -Path C:\test.txt -stream t
  3. Notice that whenever the stream name is in FileName it gets truncated
Get-Item test.txt -Stream t | Format-List PSChildName, FileName, Stream

PSChildName : test.txt:t
FileName    : C:\es.x
Stream      : t

Both Windows PowerShell and Powershell Core are affected. Tested on Windows 11 as well as Windows Server 2022.

Expected behavior

PS C:\> Set-Content -Path test.txt -Stream t -Value testvalue
PS C:\> Get-Item -Path test.txt -Stream t | Format-List PSChildName, FileName, Stream

PSChildName : test.txt:t
FileName    : C:\test.txt
Stream      : t

Actual behavior

PS C:\> Set-Content test.txt -Stream t -Value testvalue
PS C:\> Get-Item test.txt -Stream t | Format-List PSChildName, FileName, Stream

PSChildName : test.txt:t
FileName    : C:\es.x
Stream      : t

Error details

No response

Environment data

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.6
PSEdition                      Core
GitCommitId                    7.2.6
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22000.832
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22000.832
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.20348.859
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.20348.859
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Visuals

No response

@ovdeathiam ovdeathiam added the Needs-Triage The issue is new and needs to be triaged by a work group. label Sep 2, 2022
@ovdeathiam
Copy link
Author

I've managed to find the reason for this behavior.

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

Class
AlternateDataStreamUtilities

Method
GetStreams

Line
data.FileName = path.Replace(data.Stream, string.Empty);

Description
The path variable holds a path to the alternate data stream i.e. C:\path\to\file.ext:streamname. Running a replace obviously replaces all instances of stream name from the path.

Probably the following change would fix the issue. That's how its handled elsewhere in FileSystemProvider.cs.

-- data.FileName = path.Replace(data.Stream, string.Empty);
-- data.FileName = data.FileName.Trim(Utils.Separators.Colon);
++ int firstColon = path.IndexOf(':');
++ data.FileName = path.Substring(0, firstColon);

@ovdeathiam ovdeathiam changed the title FileName property wrong when listing alternate data streams with Get-Item Get-Item returns invalid FileName when listing alternate data streams Sep 3, 2022
@dkaszews
Copy link
Contributor

dkaszews commented Sep 3, 2022

@ovdeathiam Won't firstColon return the colon in C:? You should use LastIndexOf instead. Alternatively, more precise solution would be data.FileName = path.EndsWith(data.Stream) ? path.Substring(0, path.Length - data.Stream.Length) : path;.

@kilasuit
Copy link
Collaborator

kilasuit commented Sep 3, 2022

Having tested both suggestions, the first did as @dkaszews suggested, however @dkaszews suggestion is oddly a single character off as shown below
image

However the actual fix is much simpler
data.FileName = path

Which I will be submitting a PR for shortly

@dkaszews
Copy link
Contributor

dkaszews commented Sep 3, 2022

@kilasuit Hah, gotta love off-by-one errors 😄 . Should be -1 there because Substring is exclusive I guess? That's why you always have to test index arithmetic.

@ghost ghost added the In-PR Indicates that a PR is out for the issue label Sep 3, 2022
@SteveL-MSFT SteveL-MSFT added Issue-Bug Issue has been identified as a bug in the product and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Nov 2, 2022
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

1 similar comment
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 15, 2023
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In-PR Indicates that a PR is out for the issue Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more
Projects
None yet
4 participants