diff options
author | Ian Lance Taylor <iant@golang.org> | 2017-01-14 00:05:42 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2017-01-14 00:05:42 +0000 |
commit | c2047754c300b68c05d65faa8dc2925fe67b71b4 (patch) | |
tree | e183ae81a1f48a02945cb6de463a70c5be1b06f6 /libgo/go/os/file.go | |
parent | 829afb8f05602bb31c9c597b24df7377fed4f059 (diff) | |
download | gcc-c2047754c300b68c05d65faa8dc2925fe67b71b4.zip gcc-c2047754c300b68c05d65faa8dc2925fe67b71b4.tar.gz gcc-c2047754c300b68c05d65faa8dc2925fe67b71b4.tar.bz2 |
libgo: update to Go 1.8 release candidate 1
Compiler changes:
* Change map assignment to use mapassign and assign value directly.
* Change string iteration to use decoderune, faster for ASCII strings.
* Change makeslice to take int, and use makeslice64 for larger values.
* Add new noverflow field to hmap struct used for maps.
Unresolved problems, to be fixed later:
* Commented out test in go/types/sizes_test.go that doesn't compile.
* Commented out reflect.TestStructOf test for padding after zero-sized field.
Reviewed-on: https://go-review.googlesource.com/35231
gotools/:
Updates for Go 1.8rc1.
* Makefile.am (go_cmd_go_files): Add bug.go.
(s-zdefaultcc): Write defaultPkgConfig.
* Makefile.in: Rebuild.
From-SVN: r244456
Diffstat (limited to 'libgo/go/os/file.go')
-rw-r--r-- | libgo/go/os/file.go | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/libgo/go/os/file.go b/libgo/go/os/file.go index e546441..d45a00b 100644 --- a/libgo/go/os/file.go +++ b/libgo/go/os/file.go @@ -92,11 +92,11 @@ func (e *LinkError) Error() string { } // Read reads up to len(b) bytes from the File. -// It returns the number of bytes read and an error, if any. -// EOF is signaled by a zero count with err set to io.EOF. +// It returns the number of bytes read and any error encountered. +// At end of file, Read returns 0, io.EOF. func (f *File) Read(b []byte) (n int, err error) { - if f == nil { - return 0, ErrInvalid + if err := f.checkValid("read"); err != nil { + return 0, err } n, e := f.read(b) if n == 0 && len(b) > 0 && e == nil { @@ -113,8 +113,8 @@ func (f *File) Read(b []byte) (n int, err error) { // ReadAt always returns a non-nil error when n < len(b). // At end of file, that error is io.EOF. func (f *File) ReadAt(b []byte, off int64) (n int, err error) { - if f == nil { - return 0, ErrInvalid + if err := f.checkValid("read"); err != nil { + return 0, err } for len(b) > 0 { m, e := f.pread(b, off) @@ -136,8 +136,8 @@ func (f *File) ReadAt(b []byte, off int64) (n int, err error) { // It returns the number of bytes written and an error, if any. // Write returns a non-nil error when n != len(b). func (f *File) Write(b []byte) (n int, err error) { - if f == nil { - return 0, ErrInvalid + if err := f.checkValid("write"); err != nil { + return 0, err } n, e := f.write(b) if n < 0 { @@ -159,8 +159,8 @@ func (f *File) Write(b []byte) (n int, err error) { // It returns the number of bytes written and an error, if any. // WriteAt returns a non-nil error when n != len(b). func (f *File) WriteAt(b []byte, off int64) (n int, err error) { - if f == nil { - return 0, ErrInvalid + if err := f.checkValid("write"); err != nil { + return 0, err } for len(b) > 0 { m, e := f.pwrite(b, off) @@ -181,8 +181,8 @@ func (f *File) WriteAt(b []byte, off int64) (n int, err error) { // It returns the new offset and an error, if any. // The behavior of Seek on a file opened with O_APPEND is not specified. func (f *File) Seek(offset int64, whence int) (ret int64, err error) { - if f == nil { - return 0, ErrInvalid + if err := f.checkValid("seek"); err != nil { + return 0, err } r, e := f.seek(offset, whence) if e == nil && f.dirinfo != nil && r != 0 { @@ -197,16 +197,13 @@ func (f *File) Seek(offset int64, whence int) (ret int64, err error) { // WriteString is like Write, but writes the contents of string s rather than // a slice of bytes. func (f *File) WriteString(s string) (n int, err error) { - if f == nil { - return 0, ErrInvalid - } return f.Write([]byte(s)) } // Mkdir creates a new directory with the specified name and permission bits. // If there is an error, it will be of type *PathError. func Mkdir(name string, perm FileMode) error { - e := syscall.Mkdir(name, syscallMode(perm)) + e := syscall.Mkdir(fixLongPath(name), syscallMode(perm)) if e != nil { return &PathError{"mkdir", name, e} @@ -233,8 +230,8 @@ func Chdir(dir string) error { // which must be a directory. // If there is an error, it will be of type *PathError. func (f *File) Chdir() error { - if f == nil { - return ErrInvalid + if err := f.checkValid("chdir"); err != nil { + return err } if e := syscall.Fchdir(f.fd); e != nil { return &PathError{"chdir", f.name, e} @@ -263,7 +260,7 @@ func Create(name string) (*File, error) { var lstat = Lstat // Rename renames (moves) oldpath to newpath. -// If newpath already exists, Rename replaces it. +// If newpath already exists and is not a directory, Rename replaces it. // OS-specific restrictions may apply when oldpath and newpath are in different directories. // If there is an error, it will be of type *LinkError. func Rename(oldpath, newpath string) error { @@ -278,3 +275,15 @@ func fixCount(n int, err error) (int, error) { } return n, err } + +// checkValid checks whether f is valid for use. +// If not, it returns an appropriate error, perhaps incorporating the operation name op. +func (f *File) checkValid(op string) error { + if f == nil { + return ErrInvalid + } + if f.fd == badFd { + return &PathError{op, f.name, ErrClosed} + } + return nil +} |