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

os: Remove/RemoveAll should remove read-only folders on Windows #26295

Open
Nnnes opened this issue Jul 9, 2018 · 12 comments
Open

os: Remove/RemoveAll should remove read-only folders on Windows #26295

Nnnes opened this issue Jul 9, 2018 · 12 comments

Comments

@Nnnes
Copy link

@Nnnes Nnnes commented Jul 9, 2018

What version of Go are you using (go version)?

go version go1.10.2 windows/amd64

Does this issue reproduce with the latest release?

Untested, but I don't see any changes that would have fixed it

What operating system and processor architecture are you using (go env)?

windows/amd64

What did you do?

package main

import (
	"os"
)

func main() {
	err := os.RemoveAll("./folder_with_icon")
	if err != nil {
		panic(err)
	}
}

folder_with_icon is an empty folder that has had a custom icon added via Windows Explorer's interface (Properties => Customize => Change Icon...).

What did you expect to see?

folder_with_icon should be deleted.

What did you see instead?

panic: remove ./folder_with_icon: Access is denied.

goroutine 1 [running]:
main.main()
        C:/[path]/test.go:10 +0x6a

Notes

This is similar to #9606.

Setting a custom folder icon on Windows sets the read-only attribute on the folder and creates a desktop.ini file with the system attribute inside the folder. Despite being read-only, the folder can be deleted both through File Explorer and by rd /s folder_with_icon by its owner, even if the owner is not an administrator account. The above Go program will not delete the folder, even if it is run with administrator privileges.

os.RemoveAll() removes the desktop.ini but does not delete the folder.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 9, 2018

By analogy with #9606 it seems that we should do this.

@iamoryanmoshe
Copy link
Contributor

@iamoryanmoshe iamoryanmoshe commented Jul 20, 2018

I'm working on it

@gopherbot
Copy link

@gopherbot gopherbot commented Jul 20, 2018

Change https://golang.org/cl/125261 mentions this issue: os: remove read-only directories

@ianlancetaylor ianlancetaylor modified the milestones: Go1.12, Go1.13 Dec 20, 2018
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@smasher164 smasher164 modified the milestones: Backlog, Go1.14 Oct 11, 2019
@gopherbot
Copy link

@gopherbot gopherbot commented Oct 26, 2019

Change https://golang.org/cl/203599 mentions this issue: os: Remove/RemoveAll removes read-only folders on Windows

@Nnnes
Copy link
Author

@Nnnes Nnnes commented Mar 13, 2020

This issue is only half fixed. As stated in the title, os.Remove() had the same problem as os.RemoveAll() for read-only directories. Using Go 1.14:

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	path := "./folder_with_icon"
	fmt.Println(count_files(path))
	err := os.Remove(path + "/desktop.ini")
	if err != nil {
		panic(err)
	}
	fmt.Println(count_files(path)) // works fine up to here
	err = os.Remove(path)
	if err != nil {
		panic(err)
	}
}

func count_files(path string) int {
	files, _ := ioutil.ReadDir(path)
	return len(files)
}

As before, folder_with_icon is a folder with a custom icon.

C:\[path]>26295.exe
1
0
panic: remove ./folder_with_icon: Access is denied.

goroutine 1 [running]:
main.main()
        C:/[path]/26295.go:19 +0x1d5
@ianlancetaylor ianlancetaylor modified the milestones: Go1.14, Go1.15 Mar 13, 2020
@bcmills
Copy link
Member

@bcmills bcmills commented Mar 17, 2020

The analogy to #9606 does not seem to hold: while Unix systems do allow read-only files to be removed from read-write directories, they do not generally allow files to be removed from within a read-only directory without first making the directory writable.

That subtlety is what led us to add the -modcacherw flag to cmd/go in 1.14 (see #31481).

@iWdGo
Copy link
Contributor

@iWdGo iWdGo commented Mar 17, 2020

The difference of behavior between Remove and RemoveAll is stated in the documentation

RemoveAll removes path and any children it contains. It removes everything it can

os.Remove will only attempt removal and return the error.
This behavior seems desirable even if support to read-only is discussed #35042

@bcmills
Copy link
Member

@bcmills bcmills commented Mar 17, 2020

@iWdGo, my point is that the proposed change here makes RemoveAll on Windows remove more than it does on Unix, but the justification given is consistency with Unix. That doesn't seem right.

If folks want to argue that the behavior on Unix should be expanded to be consistent with the documentation, that's fine by me, but then this issue would not be Windows-specific.

@iWdGo
Copy link
Contributor

@iWdGo iWdGo commented Mar 17, 2020

@bcmills The original issue was on Windows and the difference with Unix seems to start in Go1.12 as #29983 details.

The comment in _at code and what follows does not seem to consider an update of permissions in RemoveAll.

This test expects read-only directories to remain. It is skipped on Windows for the same reason. If this would change, an error would be returned when permission is insufficient although nil seems currently returned. I agree that RemoveAll on Unix could be doing more.

@bcmills
Copy link
Member

@bcmills bcmills commented Mar 17, 2020

@iWdGo, #29983 describes a situation in which files within a writable directory were not removed when that directory was itself within a read-only directory. That situation does not seem very closely related to the one described here.

@iWdGo
Copy link
Contributor

@iWdGo iWdGo commented Mar 17, 2020

@bcmills IMHO, RemoveAll on Unix should attempt a permission update but it was not in the scope of the issue as originally filed. Such a change for Unix would seem along the line of past comments.

@bcmills
Copy link
Member

@bcmills bcmills commented Apr 7, 2020

RemoveAll on Unix should attempt a permission update but it was not in the scope of the issue as originally filed. Such a change for Unix would seem along the line of past comments.

That seems fine to me, but that should be a separate issue — and implementing the behavior described in this issue should probably wait for a decision on the broader (cross-platform) RemoveAll behavior.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.15, Go1.16 May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants
You can’t perform that action at this time.