Commit 71d08324 authored by Ian Lance Taylor's avatar Ian Lance Taylor

time: unify three readFile implementations into one

Undoes this part of https://golang.org/cl/5447061 by using the
OS-specific open and close functions, and adding a read function.

Change-Id: If37ef43eb5df8554fc03f3922bbc2f785129bb9c
Reviewed-on: https://go-review.googlesource.com/66271
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarJoe Tsai <joetsai@google.com>
parent 7340d139
......@@ -16,36 +16,6 @@ func interrupt() {
// cannot predict pid, don't want to kill group
}
// readFile reads and returns the content of the named file.
// It is a trivial implementation of ioutil.ReadFile, reimplemented
// here to avoid depending on io/ioutil or os.
// It returns an error if name exceeds maxFileSize bytes.
func readFile(name string) ([]byte, error) {
f, err := syscall.Open(name, syscall.O_RDONLY)
if err != nil {
return nil, err
}
defer syscall.Close(f)
var (
buf [4096]byte
ret []byte
n int
)
for {
n, err = syscall.Read(f, buf[:])
if n > 0 {
ret = append(ret, buf[:n]...)
}
if n == 0 || err != nil {
break
}
if len(ret) > maxFileSize {
return nil, fileSizeError(name)
}
}
return ret, err
}
func open(name string) (uintptr, error) {
fd, err := syscall.Open(name, syscall.O_RDONLY)
if err != nil {
......@@ -54,6 +24,10 @@ func open(name string) (uintptr, error) {
return uintptr(fd), nil
}
func read(fd uintptr, buf []byte) (int, error) {
return syscall.Read(int(fd), buf)
}
func closefd(fd uintptr) {
syscall.Close(int(fd))
}
......
......@@ -16,36 +16,6 @@ func interrupt() {
syscall.Kill(syscall.Getpid(), syscall.SIGCHLD)
}
// readFile reads and returns the content of the named file.
// It is a trivial implementation of ioutil.ReadFile, reimplemented
// here to avoid depending on io/ioutil or os.
// It returns an error if name exceeds maxFileSize bytes.
func readFile(name string) ([]byte, error) {
f, err := syscall.Open(name, syscall.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer syscall.Close(f)
var (
buf [4096]byte
ret []byte
n int
)
for {
n, err = syscall.Read(f, buf[:])
if n > 0 {
ret = append(ret, buf[:n]...)
}
if n == 0 || err != nil {
break
}
if len(ret) > maxFileSize {
return nil, fileSizeError(name)
}
}
return ret, err
}
func open(name string) (uintptr, error) {
fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
if err != nil {
......@@ -54,6 +24,10 @@ func open(name string) (uintptr, error) {
return uintptr(fd), nil
}
func read(fd uintptr, buf []byte) (int, error) {
return syscall.Read(int(fd), buf)
}
func closefd(fd uintptr) {
syscall.Close(int(fd))
}
......
......@@ -13,36 +13,6 @@ import (
func interrupt() {
}
// readFile reads and returns the content of the named file.
// It is a trivial implementation of ioutil.ReadFile, reimplemented
// here to avoid depending on io/ioutil or os.
// It returns an error if name exceeds maxFileSize bytes.
func readFile(name string) ([]byte, error) {
f, err := syscall.Open(name, syscall.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer syscall.Close(f)
var (
buf [4096]byte
ret []byte
n int
)
for {
n, err = syscall.Read(f, buf[:])
if n > 0 {
ret = append(ret, buf[:n]...)
}
if n == 0 || err != nil {
break
}
if len(ret) > maxFileSize {
return nil, fileSizeError(name)
}
}
return ret, err
}
func open(name string) (uintptr, error) {
fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
if err != nil {
......@@ -51,6 +21,10 @@ func open(name string) (uintptr, error) {
return uintptr(fd), nil
}
func read(fd uintptr, buf []byte) (int, error) {
return syscall.Read(syscall.Handle(fd), buf)
}
func closefd(fd uintptr) {
syscall.Close(syscall.Handle(fd))
}
......
......@@ -403,3 +403,33 @@ func loadLocation(name string, sources []string) (z *Location, firstErr error) {
}
return nil, errors.New("unknown time zone " + name)
}
// readFile reads and returns the content of the named file.
// It is a trivial implementation of ioutil.ReadFile, reimplemented
// here to avoid depending on io/ioutil or os.
// It returns an error if name exceeds maxFileSize bytes.
func readFile(name string) ([]byte, error) {
f, err := open(name)
if err != nil {
return nil, err
}
defer closefd(f)
var (
buf [4096]byte
ret []byte
n int
)
for {
n, err = read(f, buf[:])
if n > 0 {
ret = append(ret, buf[:n]...)
}
if n == 0 || err != nil {
break
}
if len(ret) > maxFileSize {
return nil, fileSizeError(name)
}
}
return ret, err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment