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

Can no longer get file attributes from exclusively locked files #26216

Closed
iSazonov opened this issue May 18, 2018 · 7 comments
Closed

Can no longer get file attributes from exclusively locked files #26216

iSazonov opened this issue May 18, 2018 · 7 comments
Assignees
Milestone

Comments

@iSazonov
Copy link
Contributor

Problem

IO.File.GetAttributes() doesn't return attributes for exclusively locked file like c:\pagefile.sys
IO.File.Exist() doesn't found such file (return false).

Repo on PowerShell

#########
[IO.File]::GetAttributes("c:\pagefile.sys")

Exception calling "GetAttributes" with "1" argument(s): "The process cannot access the file 'c:\pagefile.sys' because it is being used by another process."
At line:1 char:1
+ [IO.File]::GetAttributes("c:\pagefile.sys")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException


$error[0].exception.InnerException | fl -Force

Message        : The process cannot access the file 'c:\pagefile.sys' because it is being used by another process.
Data           : {}
InnerException :
TargetSite     : System.IO.FileAttributes GetAttributes(System.String)
StackTrace     :    at System.IO.FileSystem.GetAttributes(String fullPath)
                    at CallSite.Target(Closure , CallSite , Type , String )
HelpLink       :
Source         : System.IO.FileSystem
HResult        : -2147024864


#########
[IO.File]::Exists("c:\pagefile.sys")

false

In PowerShell repo we have a workaround:

  1. https://github.com/PowerShell/PowerShell/blob/5cbf1b43de52fe9025bf756990b1cc6eb4e694e9/src/System.Management.Automation/namespaces/FileSystemProvider.cs#L6994-L7013
  2. https://github.com/PowerShell/PowerShell/blob/5cbf1b43de52fe9025bf756990b1cc6eb4e694e9/src/System.Management.Automation/engine/Utils.cs#L989-L1029

Without the workaround PowerShell doesn't show the file:

dir c:\pagefile.sys -Hidden

but cmd.exe show the file:

C:\Users\sie>dir c:\pagefile.sys /AH
 Volume in drive C has no label.
 Volume Serial Number is 824F-BBDF

 Directory of c:\

04/18/2018  10:48 AM    11,811,160,064 pagefile.sys
               1 File(s) 11,811,160,064 bytes
               0 Dir(s)  236,808,941,568 bytes free

Suggestion

I believe the issue should be fixed in CoreFX here
https://github.com/dotnet/corefx/blob/f25eb288a449010574a6e95fe298f3ad880ada1e/src/System.IO.FileSystem/src/System/IO/FileSystem.Windows.cs#L203-L223

ERROR_SHARING_VIOLATION should be added.

@danmoseley
Copy link
Member

Is this the cause of https://github.com/dotnet/corefx/issues/29784? Ie that's a dupe?

@JeremyKuhne

@danmoseley
Copy link
Member

Seems to be a regression since 2.0.

@danmoseley
Copy link
Member

Previously we would try again with FindFirstFile for any failure of GetFileAttributes other than 3 specific codes:
https://github.com/dotnet/corefx/blob/release/2.0.0/src/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs#L257

Now we only try again for ERROR_ACCESS_DENIED

@danmoseley danmoseley changed the title IO.File.GetAttributes() and IO.File.Exist() don't work correctly with exclusively locked files Can no longer get file attributes from exclusively locked files May 18, 2018
@JeremyKuhne JeremyKuhne self-assigned this May 18, 2018
@JeremyKuhne
Copy link
Member

I'm creating tests for this scenario now and a fix.

@JeremyKuhne
Copy link
Member

The original change was done nearly 11 months ago: dotnet/corefx@0cdb9f3

@danmoseley
Copy link
Member

@JeremyKuhne could you please make a 2.1 port and mark no merge..

@JeremyKuhne
Copy link
Member

could you please make a 2.1 port and mark no merge..

Yep, I should have this up today.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.x milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants