Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: use 'os.SEEK_*' #6296

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions etcdctl/ctlv3/command/snapshot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,14 @@ func makeDB(snapdir, dbfile string) {
defer f.Close()

// get snapshot integrity hash
if _, err := f.Seek(-sha256.Size, os.SEEK_END); err != nil {
if _, err := f.Seek(-sha256.Size, io.SeekEnd); err != nil {
ExitWithError(ExitIO, err)
}
sha := make([]byte, sha256.Size)
if _, err := f.Read(sha); err != nil {
ExitWithError(ExitIO, err)
}
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
if _, err := f.Seek(0, io.SeekStart); err != nil {
ExitWithError(ExitIO, err)
}

Expand All @@ -291,7 +291,7 @@ func makeDB(snapdir, dbfile string) {
}

// truncate away integrity hash, if any.
off, serr := db.Seek(0, os.SEEK_END)
off, serr := db.Seek(0, io.SeekEnd)
if serr != nil {
ExitWithError(ExitIO, serr)
}
Expand All @@ -309,7 +309,7 @@ func makeDB(snapdir, dbfile string) {

if hasHash && !skipHashCheck {
// check for match
if _, err := db.Seek(0, os.SEEK_SET); err != nil {
if _, err := db.Seek(0, io.SeekStart); err != nil {
ExitWithError(ExitIO, err)
}
h := sha256.New()
Expand Down
7 changes: 4 additions & 3 deletions pkg/fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package fileutil

import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -101,11 +102,11 @@ func Exist(name string) bool {
// shorten the length of the file.
func ZeroToEnd(f *os.File) error {
// TODO: support FALLOC_FL_ZERO_RANGE
off, err := f.Seek(0, os.SEEK_CUR)
off, err := f.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
lenf, lerr := f.Seek(0, os.SEEK_END)
lenf, lerr := f.Seek(0, io.SeekEnd)
if lerr != nil {
return lerr
}
Expand All @@ -116,6 +117,6 @@ func ZeroToEnd(f *os.File) error {
if err = Preallocate(f, lenf, true); err != nil {
return err
}
_, err = f.Seek(off, os.SEEK_SET)
_, err = f.Seek(off, io.SeekStart)
return err
}
5 changes: 3 additions & 2 deletions pkg/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fileutil

import (
"io"
"io/ioutil"
"os"
"os/user"
Expand Down Expand Up @@ -133,13 +134,13 @@ func TestZeroToEnd(t *testing.T) {
if _, err = f.Write(b); err != nil {
t.Fatal(err)
}
if _, err = f.Seek(512, os.SEEK_SET); err != nil {
if _, err = f.Seek(512, io.SeekStart); err != nil {
t.Fatal(err)
}
if err = ZeroToEnd(f); err != nil {
t.Fatal(err)
}
off, serr := f.Seek(0, os.SEEK_CUR)
off, serr := f.Seek(0, io.SeekCurrent)
if serr != nil {
t.Fatal(serr)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/fileutil/lock_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package fileutil

import (
"io"
"os"
"syscall"
)
Expand All @@ -36,7 +37,7 @@ const (
var (
wrlck = syscall.Flock_t{
Type: syscall.F_WRLCK,
Whence: int16(os.SEEK_SET),
Whence: int16(io.SeekStart),
Start: 0,
Len: 0,
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/fileutil/preallocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package fileutil

import "os"
import (
"io"
"os"
)

// Preallocate tries to allocate the space for given
// file. This operation is only supported on linux by a
Expand All @@ -29,15 +32,15 @@ func Preallocate(f *os.File, sizeInBytes int64, extendFile bool) error {
}

func preallocExtendTrunc(f *os.File, sizeInBytes int64) error {
curOff, err := f.Seek(0, os.SEEK_CUR)
curOff, err := f.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
size, err := f.Seek(sizeInBytes, os.SEEK_END)
size, err := f.Seek(sizeInBytes, io.SeekEnd)
if err != nil {
return err
}
if _, err = f.Seek(curOff, os.SEEK_SET); err != nil {
if _, err = f.Seek(curOff, io.SeekStart); err != nil {
return err
}
if sizeInBytes > size {
Expand Down
2 changes: 1 addition & 1 deletion wal/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Repair(dirpath string) bool {
}
defer bf.Close()

if _, err = f.Seek(0, os.SEEK_SET); err != nil {
if _, err = f.Seek(0, io.SeekStart); err != nil {
plog.Errorf("could not repair %v, failed to read file", f.Name())
return false
}
Expand Down
2 changes: 1 addition & 1 deletion wal/repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func testRepair(t *testing.T, ents [][]raftpb.Entry, corrupt corruptFunc, expect
}
}

offset, err := w.tail().Seek(0, os.SEEK_CUR)
offset, err := w.tail().Seek(0, io.SeekCurrent)
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
if err != nil {
return nil, err
}
if _, err := f.Seek(0, os.SEEK_END); err != nil {
if _, err := f.Seek(0, io.SeekEnd); err != nil {
return nil, err
}
if err := fileutil.Preallocate(f.File, SegmentSizeBytes, true); err != nil {
Expand Down Expand Up @@ -292,7 +292,7 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.
// not all, will cause CRC errors on WAL open. Since the records
// were never fully synced to disk in the first place, it's safe
// to zero them out to avoid any CRC errors from new writes.
if _, err = w.tail().Seek(w.decoder.lastOffset(), os.SEEK_SET); err != nil {
if _, err = w.tail().Seek(w.decoder.lastOffset(), io.SeekStart); err != nil {
return nil, state, nil, err
}
if err = fileutil.ZeroToEnd(w.tail().File); err != nil {
Expand Down Expand Up @@ -328,7 +328,7 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.
// Then cut atomically rename temp wal file to a wal file.
func (w *WAL) cut() error {
// close old wal file; truncate to avoid wasting space if an early cut
off, serr := w.tail().Seek(0, os.SEEK_CUR)
off, serr := w.tail().Seek(0, io.SeekCurrent)
if serr != nil {
return serr
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func (w *WAL) cut() error {
return err
}

off, err = w.tail().Seek(0, os.SEEK_CUR)
off, err = w.tail().Seek(0, io.SeekCurrent)
if err != nil {
return err
}
Expand All @@ -378,7 +378,7 @@ func (w *WAL) cut() error {
if newTail, err = fileutil.LockFile(fpath, os.O_WRONLY, fileutil.PrivateFileMode); err != nil {
return err
}
if _, err = newTail.Seek(off, os.SEEK_SET); err != nil {
if _, err = newTail.Seek(off, io.SeekStart); err != nil {
return err
}

Expand Down Expand Up @@ -520,7 +520,7 @@ func (w *WAL) Save(st raftpb.HardState, ents []raftpb.Entry) error {
return err
}

curOff, err := w.tail().Seek(0, os.SEEK_CUR)
curOff, err := w.tail().Seek(0, io.SeekCurrent)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestNew(t *testing.T) {
defer w.Close()

// file is preallocated to segment size; only read data written by wal
off, err := w.tail().Seek(0, os.SEEK_CUR)
off, err := w.tail().Seek(0, io.SeekCurrent)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -556,7 +556,7 @@ func TestTailWriteNoSlackSpace(t *testing.T) {
}
}
// get rid of slack space by truncating file
off, serr := w.tail().Seek(0, os.SEEK_CUR)
off, serr := w.tail().Seek(0, io.SeekCurrent)
if serr != nil {
t.Fatal(serr)
}
Expand Down Expand Up @@ -661,7 +661,7 @@ func TestOpenOnTornWrite(t *testing.T) {
if err = w.Save(raftpb.HardState{}, es); err != nil {
t.Fatal(err)
}
if offsets[i], err = w.tail().Seek(0, os.SEEK_CUR); err != nil {
if offsets[i], err = w.tail().Seek(0, io.SeekCurrent); err != nil {
t.Fatal(err)
}
}
Expand All @@ -675,7 +675,7 @@ func TestOpenOnTornWrite(t *testing.T) {
t.Fatal(ferr)
}
defer f.Close()
_, err = f.Seek(offsets[clobberIdx], os.SEEK_SET)
_, err = f.Seek(offsets[clobberIdx], io.SeekStart)
if err != nil {
t.Fatal(err)
}
Expand Down