-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
os: permit Rename to read-only file on Windows #38287
Comments
This probably seems to be side effect in 16f0f9c but I think this is not regression.
|
Before go1.13 (before 16f0f9c), ioutil.WriteFile does not respect permission bits on Windows. So the file was always 0666 eventhough if 0444 was specified. This is related on #26295 |
I confirmed below fix this. diff --git a/src/os/file_windows.go b/src/os/file_windows.go
index 96f934d039..581d382f75 100644
--- a/src/os/file_windows.go
+++ b/src/os/file_windows.go
@@ -313,10 +313,29 @@ func Remove(name string) error {
func rename(oldname, newname string) error {
e := windows.Rename(fixLongPath(oldname), fixLongPath(newname))
- if e != nil {
- return &LinkError{"rename", oldname, newname, e}
+ if e == nil {
+ return nil
}
- return nil
+
+ p, e1 := syscall.UTF16PtrFromString(fixLongPath(newname))
+ if e1 != nil {
+ return e1
+ }
+
+ a, e1 := syscall.GetFileAttributes(p)
+ if e1 != nil {
+ e = e1
+ } else {
+ if a&syscall.FILE_ATTRIBUTE_READONLY != 0 {
+ if e1 = syscall.SetFileAttributes(p, a&^syscall.FILE_ATTRIBUTE_READONLY); e1 == nil {
+ if e = windows.Rename(fixLongPath(oldname), fixLongPath(newname)); e == nil {
+ return nil
+ }
+ }
+ }
+ }
+
+ return &LinkError{"rename", oldname, newname, e}
}
// Pipe returns a connected pair of Files; reads from r return bytes written to w. |
Yeah, the bug is in WriteFile, not Rename. Ugh. |
Ah, I'm sorry of my short explaining. I'm thinking 16f0f9c was not regression but os.Rename can be improved like os.Remove. |
Change https://golang.org/cl/227457 mentions this issue: |
On Unix, rename to a read-only file is permitted. The directory must be writable. So the question is whether Any other opinions? |
I agree with iant. |
This bug does not affect me. But the fix will affect me - os.Rename will have to do more to implement this logic. We always adjust windows code to match whatever Unix syscalls implements. Adding @zx2c4 since he is the author of 16f0f9c Thank you. Alex |
On Windows 10, in Go 1.13, it used to be possible to rename over a read only file (like it is in Unix). In Go 1.14 it is not. I'm not seeing anything specific about this in the release notes, was this an intentional change?
The text was updated successfully, but these errors were encountered: