OSDN Git Service

libgo: Update to Go 1.0.3.
[pf3gnuchains/gcc-fork.git] / libgo / go / os / file_unix.go
index 9a9ed92..f677dbb 100644 (file)
@@ -8,6 +8,7 @@ package os
 
 import (
        "runtime"
+       "sync/atomic"
        "syscall"
 )
 
@@ -24,23 +25,24 @@ type file struct {
        fd      int
        name    string
        dirinfo *dirInfo // nil unless directory being read
-       nepipe  int      // number of consecutive EPIPE in Write
+       nepipe  int32    // number of consecutive EPIPE in Write
 }
 
 // Fd returns the integer Unix file descriptor referencing the open file.
-func (f *File) Fd() int {
+func (f *File) Fd() uintptr {
        if f == nil {
-               return -1
+               return ^(uintptr(0))
        }
-       return f.fd
+       return uintptr(f.fd)
 }
 
 // NewFile returns a new File with the given file descriptor and name.
-func NewFile(fd int, name string) *File {
-       if fd < 0 {
+func NewFile(fd uintptr, name string) *File {
+       fdi := int(fd)
+       if fdi < 0 {
                return nil
        }
-       f := &File{&file{fd: fd, name: name}}
+       f := &File{&file{fd: fdi, name: name}}
        runtime.SetFinalizer(f.file, (*file).close)
        return f
 }
@@ -51,6 +53,16 @@ type dirInfo struct {
        dir *syscall.DIR // from opendir
 }
 
+func epipecheck(file *File, e error) {
+       if e == syscall.EPIPE {
+               if atomic.AddInt32(&file.nepipe, 1) >= 10 {
+                       sigpipe()
+               }
+       } else {
+               atomic.StoreInt32(&file.nepipe, 0)
+       }
+}
+
 // DevNull is the name of the operating system's ``null device.''
 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
 const DevNull = "/dev/null"
@@ -59,7 +71,7 @@ const DevNull = "/dev/null"
 // or Create instead.  It opens the named file with specified flag
 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable.  If successful,
 // methods on the returned File can be used for I/O.
-// It returns the File and an error, if any.
+// If there is an error, it will be of type *PathError.
 func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
        r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
        if e != nil {
@@ -77,7 +89,7 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
                syscall.CloseOnExec(r)
        }
 
-       return NewFile(r, name), nil
+       return NewFile(uintptr(r), name), nil
 }
 
 // Close closes the File, rendering it unusable for I/O.
@@ -88,7 +100,7 @@ func (f *File) Close() error {
 
 func (file *file) close() error {
        if file == nil || file.fd < 0 {
-               return EINVAL
+               return syscall.EINVAL
        }
        var err error
        if e := syscall.Close(file.fd); e != nil {
@@ -109,7 +121,7 @@ func (file *file) close() error {
 }
 
 // Stat returns the FileInfo structure describing file.
-// It returns the FileInfo and an error, if any.
+// If there is an error, it will be of type *PathError.
 func (f *File) Stat() (fi FileInfo, err error) {
        var stat syscall.Stat_t
        err = syscall.Fstat(f.fd, &stat)
@@ -119,11 +131,8 @@ func (f *File) Stat() (fi FileInfo, err error) {
        return fileInfoFromStat(&stat, f.name), nil
 }
 
-// Stat returns a FileInfo describing the named file and an error, if any.
-// If name names a valid symbolic link, the returned FileInfo describes
-// the file pointed at by the link and has fi.FollowedSymlink set to true.
-// If name names an invalid symbolic link, the returned FileInfo describes
-// the link itself and has fi.FollowedSymlink set to false.
+// Stat returns a FileInfo describing the named file.
+// If there is an error, it will be of type *PathError.
 func Stat(name string) (fi FileInfo, err error) {
        var stat syscall.Stat_t
        err = syscall.Stat(name, &stat)
@@ -133,9 +142,10 @@ func Stat(name string) (fi FileInfo, err error) {
        return fileInfoFromStat(&stat, name), nil
 }
 
-// Lstat returns a FileInfo describing the named file and an
-// error, if any.  If the file is a symbolic link, the returned FileInfo
+// Lstat returns a FileInfo describing the named file.
+// If the file is a symbolic link, the returned FileInfo
 // describes the symbolic link.  Lstat makes no attempt to follow the link.
+// If there is an error, it will be of type *PathError.
 func Lstat(name string) (fi FileInfo, err error) {
        var stat syscall.Stat_t
        err = syscall.Lstat(name, &stat)
@@ -158,7 +168,7 @@ func (f *File) readdir(n int) (fi []FileInfo, err error) {
                if err == nil {
                        fi[i] = fip
                } else {
-                       fi[i] = &FileStat{name: filename}
+                       fi[i] = &fileStat{name: filename}
                }
        }
        return fi, err
@@ -180,7 +190,21 @@ func (f *File) pread(b []byte, off int64) (n int, err error) {
 // write writes len(b) bytes to the File.
 // It returns the number of bytes written and an error, if any.
 func (f *File) write(b []byte) (n int, err error) {
-       return syscall.Write(f.fd, b)
+       for {
+               m, err := syscall.Write(f.fd, b)
+               n += m
+
+               // If the syscall wrote some data but not all (short write)
+               // or it returned EINTR, then assume it stopped early for
+               // reasons that are uninteresting to the caller, and try again.
+               if 0 < m && m < len(b) || err == syscall.EINTR {
+                       b = b[m:]
+                       continue
+               }
+
+               return n, err
+       }
+       panic("not reached")
 }
 
 // pwrite writes len(b) bytes to the File starting at byte offset off.
@@ -199,6 +223,7 @@ func (f *File) seek(offset int64, whence int) (ret int64, err error) {
 
 // Truncate changes the size of the named file.
 // If the file is a symbolic link, it changes the size of the link's target.
+// If there is an error, it will be of type *PathError.
 func Truncate(name string, size int64) error {
        if e := syscall.Truncate(name, size); e != nil {
                return &PathError{"truncate", name, e}
@@ -207,6 +232,7 @@ func Truncate(name string, size int64) error {
 }
 
 // Remove removes the named file or directory.
+// If there is an error, it will be of type *PathError.
 func Remove(name string) error {
        // System call interface forces us to know
        // whether name is a file or directory.
@@ -270,7 +296,7 @@ func Pipe() (r *File, w *File, err error) {
        syscall.CloseOnExec(p[1])
        syscall.ForkLock.RUnlock()
 
-       return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
+       return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
 }
 
 // TempDir returns the default directory to use for temporary files.