1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:54:40 -07:00

os, syscall: windows really isn't posix compliant, fix os.IsExist()

R=golang-dev, rsc, bradfitz, alex.brainman
CC=golang-dev
https://golang.org/cl/5754083
This commit is contained in:
Shenghou Ma 2012-03-13 12:50:04 +11:00 committed by Alex Brainman
parent d2d7de974c
commit 0238cec021
4 changed files with 69 additions and 1 deletions

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin freebsd linux netbsd openbsd windows
// +build darwin freebsd linux netbsd openbsd
package os

31
src/pkg/os/error_test.go Normal file
View File

@ -0,0 +1,31 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os_test
import (
"io/ioutil"
"os"
"testing"
)
func TestErrIsExist(t *testing.T) {
f, err := ioutil.TempFile("", "_Go_ErrIsExist")
if err != nil {
t.Fatalf("open ErrIsExist tempfile: %s", err)
return
}
defer os.Remove(f.Name())
defer f.Close()
f2, err := os.OpenFile(f.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err == nil {
f2.Close()
t.Fatal("Open should have failed")
return
}
if !os.IsExist(err) {
t.Fatalf("os.IsExist does not work as expected for %#v", err)
return
}
}

View File

@ -0,0 +1,35 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os
import "syscall"
// IsExist returns whether the error is known to report that a file already exists.
// It is satisfied by ErrExist as well as some syscall errors.
func IsExist(err error) bool {
if pe, ok := err.(*PathError); ok {
err = pe.Err
}
return err == syscall.EEXIST || err == syscall.ERROR_ALREADY_EXISTS ||
err == syscall.ERROR_FILE_EXISTS || err == ErrExist
}
// IsNotExist returns whether the error is known to report that a file does not exist.
// It is satisfied by ErrNotExist as well as some syscall errors.
func IsNotExist(err error) bool {
if pe, ok := err.(*PathError); ok {
err = pe.Err
}
return err == syscall.ENOENT || err == ErrNotExist
}
// IsPermission returns whether the error is known to report that permission is denied.
// It is satisfied by ErrPermission as well as some syscall errors.
func IsPermission(err error) bool {
if pe, ok := err.(*PathError); ok {
err = pe.Err
}
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
}

View File

@ -10,11 +10,13 @@ const (
ERROR_PATH_NOT_FOUND Errno = 3
ERROR_ACCESS_DENIED Errno = 5
ERROR_NO_MORE_FILES Errno = 18
ERROR_FILE_EXISTS Errno = 80
ERROR_BROKEN_PIPE Errno = 109
ERROR_BUFFER_OVERFLOW Errno = 111
ERROR_INSUFFICIENT_BUFFER Errno = 122
ERROR_MOD_NOT_FOUND Errno = 126
ERROR_PROC_NOT_FOUND Errno = 127
ERROR_ALREADY_EXISTS Errno = 183
ERROR_ENVVAR_NOT_FOUND Errno = 203
ERROR_OPERATION_ABORTED Errno = 995
ERROR_IO_PENDING Errno = 997