os: make sure Remove returns correct error on windows

R=golang-dev, bsiegert, rsc
CC=golang-dev
https://golang.org/cl/5493078
This commit is contained in:
Alex Brainman 2011-12-20 11:52:20 +11:00
parent 448d89d67a
commit 796a2c19ea
3 changed files with 59 additions and 33 deletions

View file

@ -210,6 +210,36 @@ func Truncate(name string, size int64) error {
return nil
}
// Remove removes the named file or directory.
func Remove(name string) error {
// System call interface forces us to know
// whether name is a file or directory.
// Try both: it is cheaper on average than
// doing a Stat plus the right one.
e := syscall.Unlink(name)
if e == nil {
return nil
}
e1 := syscall.Rmdir(name)
if e1 == nil {
return nil
}
// Both failed: figure out which error to return.
// OS X and Linux differ on whether unlink(dir)
// returns EISDIR, so can't use that. However,
// both agree that rmdir(file) returns ENOTDIR,
// so we can use that to decide which error is real.
// Rmdir might also return ENOTDIR if given a bad
// file path, like /etc/passwd/foo, but in that case,
// both errors will be ENOTDIR, so it's okay to
// use the error from unlink.
if e1 != syscall.ENOTDIR {
e = e1
}
return &PathError{"remove", name, e}
}
// basename removes trailing slashes and the leading directory name from path name
func basename(name string) string {
i := len(name) - 1