// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// The os package provides a platform-independent interface to operating
-// system functionality. The design is Unix-like.
+// Package os provides a platform-independent interface to operating system
+// functionality. The design is Unix-like.
+// The os interface is intended to be uniform across all operating systems.
+// Features not generally available appear in the system-specific package syscall.
package os
import (
- "runtime"
"syscall"
)
-// File represents an open file descriptor.
-type File struct {
- fd int
- name string
- dirinfo *dirInfo // nil unless directory being read
- nepipe int // number of consecutive EPIPE in Write
-}
-
-// Fd returns the integer Unix file descriptor referencing the open file.
-func (file *File) Fd() int { return file.fd }
-
// Name returns the name of the file as presented to Open.
func (file *File) Name() string { return file.name }
-// NewFile returns a new File with the given file descriptor and name.
-func NewFile(fd int, name string) *File {
- if fd < 0 {
- return nil
- }
- f := &File{fd, name, nil, 0}
- runtime.SetFinalizer(f, (*File).Close)
- return f
-}
-
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
// standard output, and standard error file descriptors.
var (
if file == nil {
return 0, EINVAL
}
- n, e := syscall.Read(file.fd, b)
+ n, e := file.read(b)
if n < 0 {
n = 0
}
return 0, EINVAL
}
for len(b) > 0 {
- m, e := syscall.Pread(file.fd, b, off)
+ m, e := file.pread(b, off)
if m == 0 && !iserror(e) {
return n, EOF
}
if file == nil {
return 0, EINVAL
}
- n, e := syscall.Write(file.fd, b)
+ n, e := file.write(b)
if n < 0 {
n = 0
}
return 0, EINVAL
}
for len(b) > 0 {
- m, e := syscall.Pwrite(file.fd, b, off)
+ m, e := file.pwrite(b, off)
if iserror(e) {
err = &PathError{"write", file.name, Errno(e)}
break
// relative to the current offset, and 2 means relative to the end.
// It returns the new offset and an Error, if any.
func (file *File) Seek(offset int64, whence int) (ret int64, err Error) {
- r, e := syscall.Seek(file.fd, offset, whence)
+ r, e := file.seek(offset, whence)
if !iserror(e) && file.dirinfo != nil && r != 0 {
e = syscall.EISDIR
}
if file == nil {
return 0, EINVAL
}
- b := syscall.StringByteSlice(s)
- b = b[0 : len(b)-1]
- return file.Write(b)
+ return file.Write([]byte(s))
}
// Mkdir creates a new directory with the specified name and permission bits.