1
0
mirror of https://github.com/golang/go synced 2024-11-23 10:10:02 -07:00

all: use SeekStart, SeekCurrent, SeekEnd

CL/19862 (f79b50b8d5) recently introduced the constants
SeekStart, SeekCurrent, and SeekEnd to the io package. We should use these constants
consistently throughout the code base.

Updates #15269

Change-Id: If7fcaca7676e4a51f588528f5ced28220d9639a2
Reviewed-on: https://go-review.googlesource.com/22097
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Joe Tsai <joetsai@digital-static.net>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Joe Tsai 2016-04-05 11:22:53 -07:00 committed by Brad Fitzpatrick
parent 6db98a3c51
commit acc757f678
27 changed files with 72 additions and 64 deletions

View File

@ -1475,7 +1475,7 @@ func BenchmarkReaderWriteToOptimal(b *testing.B) {
b.Fatal("ioutil.Discard doesn't support ReaderFrom")
}
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
r.Seek(0, io.SeekStart)
srcReader.Reset(onlyReader{r})
n, err := srcReader.WriteTo(ioutil.Discard)
if err != nil {

View File

@ -108,11 +108,11 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
r.prevRune = -1
var abs int64
switch whence {
case 0:
case io.SeekStart:
abs = offset
case 1:
case io.SeekCurrent:
abs = r.i + offset
case 2:
case io.SeekEnd:
abs = int64(len(r.s)) + offset
default:
return 0, errors.New("bytes.Reader.Seek: invalid whence")

View File

@ -188,7 +188,7 @@ var UnreadRuneErrorTests = []struct {
{"Read", func(r *Reader) { r.Read([]byte{0}) }},
{"ReadByte", func(r *Reader) { r.ReadByte() }},
{"UnreadRune", func(r *Reader) { r.UnreadRune() }},
{"Seek", func(r *Reader) { r.Seek(0, 1) }},
{"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},
{"WriteTo", func(r *Reader) { r.WriteTo(&Buffer{}) }},
}

View File

@ -110,7 +110,7 @@ func readELFGoBuildID(filename string, f *os.File, data []byte) (buildid string,
// or even the first few megabytes of the file
// due to differences in note segment placement;
// in that case, extract the note data manually.
_, err = f.Seek(int64(p.Off), 0)
_, err = f.Seek(int64(p.Off), io.SeekStart)
if err != nil {
return "", err
}

View File

@ -290,9 +290,9 @@ func importPathToPrefix(s string) string {
func (r *objReader) init(f io.ReadSeeker, p *Package) {
r.f = f
r.p = p
r.offset, _ = f.Seek(0, 1)
r.limit, _ = f.Seek(0, 2)
f.Seek(r.offset, 0)
r.offset, _ = f.Seek(0, io.SeekCurrent)
r.limit, _ = f.Seek(0, io.SeekEnd)
f.Seek(r.offset, io.SeekStart)
r.b = bufio.NewReader(f)
r.pkgprefix = importPathToPrefix(p.ImportPath) + "."
}
@ -440,7 +440,7 @@ func (r *objReader) skip(n int64) {
r.readFull(r.tmp[:n])
} else {
// Seek, giving up buffered data.
_, err := r.f.Seek(r.offset+n, 0)
_, err := r.f.Seek(r.offset+n, io.SeekStart)
if err != nil {
r.error(err)
}

View File

@ -286,7 +286,7 @@ func (ar *Archive) output(entry *Entry, w io.Writer) {
log.Fatal("short file")
}
if entry.size&1 == 1 {
_, err := ar.fd.Seek(1, 1)
_, err := ar.fd.Seek(1, io.SeekCurrent)
if err != nil {
log.Fatal(err)
}
@ -299,7 +299,7 @@ func (ar *Archive) skip(entry *Entry) {
if size&1 == 1 {
size++
}
_, err := ar.fd.Seek(size, 1)
_, err := ar.fd.Seek(size, io.SeekCurrent)
if err != nil {
log.Fatal(err)
}

View File

@ -655,7 +655,7 @@ func TestCompressedSection(t *testing.T) {
// Test Open method and seeking.
buf, have, count := make([]byte, len(b)), make([]bool, len(b)), 0
sf := sec.Open()
if got, err := sf.Seek(0, 2); got != int64(len(b)) || err != nil {
if got, err := sf.Seek(0, io.SeekEnd); got != int64(len(b)) || err != nil {
t.Fatalf("want seek end %d, got %d error %v", len(b), got, err)
}
if n, err := sf.Read(buf); n != 0 || err != io.EOF {
@ -668,11 +668,11 @@ func TestCompressedSection(t *testing.T) {
target := rand.Int63n(int64(len(buf)))
var offset int64
switch whence {
case 0:
case io.SeekStart:
offset = target
case 1:
case io.SeekCurrent:
offset = target - pos
case 2:
case io.SeekEnd:
offset = target - int64(len(buf))
}
pos, err = sf.Seek(offset, whence)

View File

@ -63,11 +63,11 @@ func (r *readSeekerFromReader) Read(p []byte) (n int, err error) {
func (r *readSeekerFromReader) Seek(offset int64, whence int) (int64, error) {
var newOffset int64
switch whence {
case 0:
case io.SeekStart:
newOffset = offset
case 1:
case io.SeekCurrent:
newOffset = r.offset + offset
case 2:
case io.SeekEnd:
newOffset = r.size + offset
default:
return 0, os.ErrInvalid

View File

@ -90,7 +90,7 @@ func openExportFile(fpath string) (reader io.ReadSeeker, closer io.Closer, err e
}
// reset to offset 0 - needed on Plan 9 (see issue #11265)
// TODO: remove once issue #11265 has been resolved.
_, err = f.Seek(0, 0)
_, err = f.Seek(0, io.SeekStart)
if err != nil {
return
}
@ -168,7 +168,7 @@ func GetImporter(searchpaths []string, initmap map[*types.Package]InitData) Impo
if err != nil {
return
}
_, err = reader.Seek(0, 0)
_, err = reader.Seek(0, io.SeekStart)
if err != nil {
return
}

View File

@ -189,7 +189,7 @@ func ExampleSectionReader_Seek() {
r := strings.NewReader("some io.Reader stream to be read\n")
s := io.NewSectionReader(r, 5, 16)
if _, err := s.Seek(10, 0); err != nil {
if _, err := s.Seek(10, io.SeekStart); err != nil {
log.Fatal(err)
}

View File

@ -480,11 +480,11 @@ func (s *SectionReader) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
return 0, errWhence
case 0:
case SeekStart:
offset += s.base
case 1:
case SeekCurrent:
offset += s.off
case 2:
case SeekEnd:
offset += s.limit
}
if offset < s.base {

View File

@ -347,7 +347,7 @@ func TestSectionReader_Seek(t *testing.T) {
br := bytes.NewReader([]byte("foo"))
sr := NewSectionReader(br, 0, int64(len("foo")))
for whence := 0; whence <= 2; whence++ {
for _, whence := range []int{SeekStart, SeekCurrent, SeekEnd} {
for offset := int64(-3); offset <= 4; offset++ {
brOff, brErr := br.Seek(offset, whence)
srOff, srErr := sr.Seek(offset, whence)
@ -359,7 +359,7 @@ func TestSectionReader_Seek(t *testing.T) {
}
// And verify we can just seek past the end and get an EOF
got, err := sr.Seek(100, 0)
got, err := sr.Seek(100, SeekStart)
if err != nil || got != 100 {
t.Errorf("Seek = %v, %v; want 100, nil", got, err)
}

View File

@ -60,7 +60,7 @@ func newFileFD(f *os.File) (net *netFD, err error) {
dir := netdir + "/" + comp[n-2]
ctl = os.NewFile(uintptr(fd), dir+"/"+file)
ctl.Seek(0, 0)
ctl.Seek(0, io.SeekStart)
var buf [16]byte
n, err := ctl.Read(buf[:])
if err != nil {

View File

@ -122,7 +122,7 @@ func TestChunkReaderAllocs(t *testing.T) {
byter := bytes.NewReader(buf.Bytes())
bufr := bufio.NewReader(byter)
mallocs := testing.AllocsPerRun(100, func() {
byter.Seek(0, 0)
byter.Seek(0, io.SeekStart)
bufr.Reset(byter)
r := NewChunkedReader(bufr)
n, err := io.ReadFull(r, readBuf)

View File

@ -7,6 +7,7 @@ package net
import (
"context"
"errors"
"io"
"os"
)
@ -17,7 +18,7 @@ func query(ctx context.Context, filename, query string, bufSize int) (res []stri
}
defer file.Close()
_, err = file.Seek(0, 0)
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return
}
@ -25,7 +26,7 @@ func query(ctx context.Context, filename, query string, bufSize int) (res []stri
if err != nil {
return
}
_, err = file.Seek(0, 0)
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return
}

View File

@ -5,6 +5,7 @@
package os
import (
"io"
"runtime"
"syscall"
"time"
@ -123,7 +124,7 @@ func OpenFile(name string, flag int, perm FileMode) (*File, error) {
}
if append {
if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
if _, e = syscall.Seek(fd, 0, io.SeekEnd); e != nil {
return nil, &PathError{"seek", name, e}
}
}

View File

@ -325,11 +325,11 @@ func (f *File) read(b []byte) (n int, err error) {
func (f *File) pread(b []byte, off int64) (n int, err error) {
f.l.Lock()
defer f.l.Unlock()
curoffset, e := syscall.Seek(f.fd, 0, 1)
curoffset, e := syscall.Seek(f.fd, 0, io.SeekCurrent)
if e != nil {
return 0, e
}
defer syscall.Seek(f.fd, curoffset, 0)
defer syscall.Seek(f.fd, curoffset, io.SeekStart)
o := syscall.Overlapped{
OffsetHigh: uint32(off >> 32),
Offset: uint32(off),
@ -405,11 +405,11 @@ func (f *File) write(b []byte) (n int, err error) {
func (f *File) pwrite(b []byte, off int64) (n int, err error) {
f.l.Lock()
defer f.l.Unlock()
curoffset, e := syscall.Seek(f.fd, 0, 1)
curoffset, e := syscall.Seek(f.fd, 0, io.SeekCurrent)
if e != nil {
return 0, e
}
defer syscall.Seek(f.fd, curoffset, 0)
defer syscall.Seek(f.fd, curoffset, io.SeekStart)
o := syscall.Overlapped{
OffsetHigh: uint32(off >> 32),
Offset: uint32(off),

View File

@ -1182,14 +1182,14 @@ func TestSeek(t *testing.T) {
out int64
}
var tests = []test{
{0, 1, int64(len(data))},
{0, 0, 0},
{5, 0, 5},
{0, 2, int64(len(data))},
{0, 0, 0},
{-1, 2, int64(len(data)) - 1},
{1 << 33, 0, 1 << 33},
{1 << 33, 2, 1<<33 + int64(len(data))},
{0, io.SeekCurrent, int64(len(data))},
{0, io.SeekStart, 0},
{5, io.SeekStart, 5},
{0, io.SeekEnd, int64(len(data))},
{0, io.SeekStart, 0},
{-1, io.SeekEnd, int64(len(data)) - 1},
{1 << 33, io.SeekStart, 1 << 33},
{1 << 33, io.SeekEnd, 1<<33 + int64(len(data))},
}
for i, tt := range tests {
off, err := f.Seek(tt.in, tt.whence)
@ -1726,7 +1726,7 @@ var nilFileMethodTests = []struct {
{"ReadAt", func(f *File) error { _, err := f.ReadAt(make([]byte, 0), 0); return err }},
{"Readdir", func(f *File) error { _, err := f.Readdir(1); return err }},
{"Readdirnames", func(f *File) error { _, err := f.Readdirnames(1); return err }},
{"Seek", func(f *File) error { _, err := f.Seek(0, 0); return err }},
{"Seek", func(f *File) error { _, err := f.Seek(0, io.SeekStart); return err }},
{"Stat", func(f *File) error { _, err := f.Stat(); return err }},
{"Sync", func(f *File) error { return f.Sync() }},
{"Truncate", func(f *File) error { return f.Truncate(0) }},

View File

@ -232,7 +232,7 @@ func verifyAranges(t *testing.T, byteorder binary.ByteOrder, data io.ReadSeeker)
SegmentSize uint8
}
for {
offset, err := data.Seek(0, 1)
offset, err := data.Seek(0, io.SeekCurrent)
if err != nil {
t.Fatalf("Seek error: %v", err)
}
@ -246,7 +246,7 @@ func verifyAranges(t *testing.T, byteorder binary.ByteOrder, data io.ReadSeeker)
if lastTupleOffset%tupleSize != 0 {
t.Fatalf("Invalid arange length %d, (addr %d, seg %d)", header.UnitLength, header.AddressSize, header.SegmentSize)
}
if _, err = data.Seek(lastTupleOffset, 0); err != nil {
if _, err = data.Seek(lastTupleOffset, io.SeekStart); err != nil {
t.Fatalf("Seek error: %v", err)
}
buf := make([]byte, tupleSize)

View File

@ -107,11 +107,11 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
r.prevRune = -1
var abs int64
switch whence {
case 0:
case io.SeekStart:
abs = offset
case 1:
case io.SeekCurrent:
abs = r.i + offset
case 2:
case io.SeekEnd:
abs = int64(len(r.s)) + offset
default:
return 0, errors.New("strings.Reader.Seek: invalid whence")

View File

@ -952,7 +952,7 @@ var UnreadRuneErrorTests = []struct {
{"Read", func(r *Reader) { r.Read([]byte{0}) }},
{"ReadByte", func(r *Reader) { r.ReadByte() }},
{"UnreadRune", func(r *Reader) { r.UnreadRune() }},
{"Seek", func(r *Reader) { r.Seek(0, 1) }},
{"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},
{"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }},
}

View File

@ -10,6 +10,7 @@
package syscall
import (
"io"
"sync"
)
@ -252,15 +253,15 @@ func (f *naclFile) seek(off int64, whence int) (int64, error) {
func (f *naclFile) prw(b []byte, offset int64, rw func([]byte) (int, error)) (int, error) {
// NaCl has no pread; simulate with seek and hope for no races.
old, err := f.seek(0, 1)
old, err := f.seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
if _, err := f.seek(offset, 0); err != nil {
if _, err := f.seek(offset, io.SeekStart); err != nil {
return 0, err
}
n, err := rw(b)
f.seek(old, 0)
f.seek(old, io.SeekStart)
return n, err
}

View File

@ -15,6 +15,7 @@
package syscall
import (
"io"
"sync"
"unsafe"
)
@ -367,9 +368,9 @@ func (f *fsysFile) seek(offset int64, whence int) (int64, error) {
f.fsys.mu.Lock()
defer f.fsys.mu.Unlock()
switch whence {
case 1:
case io.SeekCurrent:
offset += f.offset
case 2:
case io.SeekEnd:
offset += f.inode.Size
}
if offset < 0 {

View File

@ -10,6 +10,7 @@ import (
"flag"
"fmt"
"internal/testenv"
"io"
"io/ioutil"
"net"
"os"
@ -244,7 +245,7 @@ func passFDChild() {
}
f.Write([]byte("Hello from child process!\n"))
f.Seek(0, 0)
f.Seek(0, io.SeekStart)
rights := syscall.UnixRights(int(f.Fd()))
dummyByte := []byte("x")
@ -344,7 +345,7 @@ func TestRlimit(t *testing.T) {
}
func TestSeekFailure(t *testing.T) {
_, err := syscall.Seek(-1, 0, 0)
_, err := syscall.Seek(-1, 0, io.SeekStart)
if err == nil {
t.Fatalf("Seek(-1, 0, 0) did not fail")
}

View File

@ -8,6 +8,7 @@ package time
import (
"errors"
"io"
"syscall"
)
@ -55,9 +56,9 @@ func closefd(fd uintptr) {
}
func preadn(fd uintptr, buf []byte, off int) error {
whence := 0
whence := io.SeekStart
if off < 0 {
whence = 2
whence = io.SeekEnd
}
if _, err := syscall.Seek(int(fd), int64(off), whence); err != nil {
return err

View File

@ -8,6 +8,7 @@ package time
import (
"errors"
"io"
"syscall"
)
@ -55,9 +56,9 @@ func closefd(fd uintptr) {
}
func preadn(fd uintptr, buf []byte, off int) error {
whence := 0
whence := io.SeekStart
if off < 0 {
whence = 2
whence = io.SeekEnd
}
if _, err := syscall.Seek(int(fd), int64(off), whence); err != nil {
return err

View File

@ -6,6 +6,7 @@ package time
import (
"errors"
"io"
"syscall"
)
@ -52,9 +53,9 @@ func closefd(fd uintptr) {
}
func preadn(fd uintptr, buf []byte, off int) error {
whence := 0
whence := io.SeekStart
if off < 0 {
whence = 2
whence = io.SeekEnd
}
if _, err := syscall.Seek(syscall.Handle(fd), int64(off), whence); err != nil {
return err