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

File.SetLastWrite time fails on readonly files on Windows #62602

Closed
Tracked by #64596
danmoseley opened this issue Dec 9, 2021 · 9 comments · Fixed by #62638
Closed
Tracked by #64596

File.SetLastWrite time fails on readonly files on Windows #62602

danmoseley opened this issue Dec 9, 2021 · 9 comments · Fixed by #62638
Assignees
Milestone

Comments

@danmoseley
Copy link
Member

            string path = Path.GetTempFileName();

            File.SetLastWriteTime(path, new DateTime(2000, 1, 1)); // succeeds
            File.SetAttributes(path, FileAttributes.ReadOnly); 
            File.SetLastWriteTime(path, new DateTime(2001, 1, 2)); // throws UnauthorizedAccessException

This is because SetLastWrite time tries to open a file handle with GENERIC_WRITE instead of only FILE_WRITE_ATTRIBUTES. This bug exists in .NET Framework as well and has been encountered by customers eg here and now reported in a DTS issue.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Dec 9, 2021
@ghost
Copy link

ghost commented Dec 9, 2021

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details
            string path = Path.GetTempFileName();

            File.SetLastWriteTime(path, new DateTime(2000, 1, 1)); // succeeds
            File.SetAttributes(path, FileAttributes.ReadOnly); 
            File.SetLastWriteTime(path, new DateTime(2001, 1, 2)); // throws UnauthorizedAccessException

This is because SetLastWrite time tries to open a file handle with GENERIC_WRITE instead of only FILE_WRITE_ATTRIBUTES. This bug exists in .NET Framework as well and has been encountered by customers eg here and now reported in a DTS issue.

Author: danmoseley
Assignees: -
Labels:

area-System.IO

Milestone: -

@danmoseley
Copy link
Member Author

I'll throw up a fix.

@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label Dec 10, 2021
@adamsitnik adamsitnik added this to the 7.0.0 milestone Dec 10, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Dec 10, 2021
@stephentoub
Copy link
Member

stephentoub commented Dec 10, 2021

Just from a theoretical perspective, why is it incorrect to fail changing a read-only file? I get that UnauthorizedAccessException is a strange exception for that case, but isn't it a little strange to expect it to succeed?

@danmoseley
Copy link
Member Author

danmoseley commented Dec 10, 2021

  1. Windows allows it just fine. It's precisely what this flag is for - we were just passing the wrong one.
  2. Customers have asked for it, including a DTS customer today.
  3. Linux allows it. Customer pointed out Windows is inconsistent due to the flag we're passing.
dan@danmoseL:~/tmp$ ll tmp
-r--r--r-- 1 dan dan 2 Jan  1  2020 tmp
dan@danmoseL:~/tmp$ chmod 444 tmp
dan@danmoseL:~/tmp$ touch -t 202101010101 tmp
dan@danmoseL:~/tmp$ ll tmp
-r--r--r-- 1 dan dan 2 Jan  1  2021 tmp

Note that my implementation of GNU touch on Windows does fail, I didn't look at sources, but under the debugger it doesn't seem to call CreateFile (or SetFileTime) after reading the attributes. It looks like it is choosing to bail when it sees the r/o flag.

@danmoseley
Copy link
Member Author

@stephentoub thoughts on above?

@danmoseley
Copy link
Member Author

Also it occurs to me that copying a readonly file changes the timestamp on it. You can also rename or move it, and set other attributes on it. It seems that readonly really means "cannot change the content".

@stephentoub
Copy link
Member

Seems fine then.

@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Dec 10, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Dec 11, 2021
@adamsitnik
Copy link
Member

Customers have asked for it, including a DTS customer today.

@danmoseley Should we backport the fix? It was relatively low risk. Thank you for fixing it BTW.

@ericstj ericstj reopened this Dec 16, 2021
@ericstj
Copy link
Member

ericstj commented Dec 16, 2021

Reponed since customer is asking for this in 6.0.

@ericstj ericstj modified the milestones: 7.0.0, 6.0.x Dec 16, 2021
@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Dec 30, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 7, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants