-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Comments
Tagging subscribers to this area: @dotnet/area-system-io 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.
|
I'll throw up a fix. |
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? |
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. |
@stephentoub thoughts on above? |
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". |
Seems fine then. |
@danmoseley Should we backport the fix? It was relatively low risk. Thank you for fixing it BTW. |
Reponed since customer is asking for this in 6.0. |
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.
The text was updated successfully, but these errors were encountered: