wal: use path/filepath instead of path

Use the path/filepath package instead of the path package. The
path package assumes slash-separated paths, which doesn't work
on Windows. But path/filepath manipulates filename paths in a way
that's compatible across OSes.
release-3.2
Tess Rinearson 2017-03-15 17:18:55 -07:00
parent 5856c8bce9
commit 39c733ebe7
4 changed files with 31 additions and 31 deletions

View File

@ -17,7 +17,7 @@ package wal
import ( import (
"fmt" "fmt"
"os" "os"
"path" "path/filepath"
"github.com/coreos/etcd/pkg/fileutil" "github.com/coreos/etcd/pkg/fileutil"
) )
@ -65,7 +65,7 @@ func (fp *filePipeline) Close() error {
func (fp *filePipeline) alloc() (f *fileutil.LockedFile, err error) { func (fp *filePipeline) alloc() (f *fileutil.LockedFile, err error) {
// count % 2 so this file isn't the same as the one last published // count % 2 so this file isn't the same as the one last published
fpath := path.Join(fp.dir, fmt.Sprintf("%d.tmp", fp.count%2)) fpath := filepath.Join(fp.dir, fmt.Sprintf("%d.tmp", fp.count%2))
if f, err = fileutil.LockFile(fpath, os.O_CREATE|os.O_WRONLY, fileutil.PrivateFileMode); err != nil { if f, err = fileutil.LockFile(fpath, os.O_CREATE|os.O_WRONLY, fileutil.PrivateFileMode); err != nil {
return nil, err return nil, err
} }

View File

@ -17,7 +17,7 @@ package wal
import ( import (
"io" "io"
"os" "os"
"path" "path/filepath"
"github.com/coreos/etcd/pkg/fileutil" "github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/wal/walpb" "github.com/coreos/etcd/wal/walpb"
@ -94,6 +94,6 @@ func openLast(dirpath string) (*fileutil.LockedFile, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
last := path.Join(dirpath, names[len(names)-1]) last := filepath.Join(dirpath, names[len(names)-1])
return fileutil.LockFile(last, os.O_RDWR, fileutil.PrivateFileMode) return fileutil.LockFile(last, os.O_RDWR, fileutil.PrivateFileMode)
} }

View File

@ -21,7 +21,7 @@ import (
"hash/crc32" "hash/crc32"
"io" "io"
"os" "os"
"path" "path/filepath"
"sync" "sync"
"time" "time"
@ -97,7 +97,7 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
} }
// keep temporary wal directory so WAL initialization appears atomic // keep temporary wal directory so WAL initialization appears atomic
tmpdirpath := path.Clean(dirpath) + ".tmp" tmpdirpath := filepath.Clean(dirpath) + ".tmp"
if fileutil.Exist(tmpdirpath) { if fileutil.Exist(tmpdirpath) {
if err := os.RemoveAll(tmpdirpath); err != nil { if err := os.RemoveAll(tmpdirpath); err != nil {
return nil, err return nil, err
@ -107,7 +107,7 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
return nil, err return nil, err
} }
p := path.Join(tmpdirpath, walName(0, 0)) p := filepath.Join(tmpdirpath, walName(0, 0))
f, err := fileutil.LockFile(p, os.O_WRONLY|os.O_CREATE, fileutil.PrivateFileMode) f, err := fileutil.LockFile(p, os.O_WRONLY|os.O_CREATE, fileutil.PrivateFileMode)
if err != nil { if err != nil {
return nil, err return nil, err
@ -143,7 +143,7 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
} }
// directory was renamed; sync parent dir to persist rename // directory was renamed; sync parent dir to persist rename
pdir, perr := fileutil.OpenDir(path.Dir(w.dir)) pdir, perr := fileutil.OpenDir(filepath.Dir(w.dir))
if perr != nil { if perr != nil {
return nil, perr return nil, perr
} }
@ -196,7 +196,7 @@ func openAtIndex(dirpath string, snap walpb.Snapshot, write bool) (*WAL, error)
rs := make([]io.Reader, 0) rs := make([]io.Reader, 0)
ls := make([]*fileutil.LockedFile, 0) ls := make([]*fileutil.LockedFile, 0)
for _, name := range names[nameIndex:] { for _, name := range names[nameIndex:] {
p := path.Join(dirpath, name) p := filepath.Join(dirpath, name)
if write { if write {
l, err := fileutil.TryLockFile(p, os.O_RDWR, fileutil.PrivateFileMode) l, err := fileutil.TryLockFile(p, os.O_RDWR, fileutil.PrivateFileMode)
if err != nil { if err != nil {
@ -232,7 +232,7 @@ func openAtIndex(dirpath string, snap walpb.Snapshot, write bool) (*WAL, error)
// write reuses the file descriptors from read; don't close so // write reuses the file descriptors from read; don't close so
// WAL can append without dropping the file lock // WAL can append without dropping the file lock
w.readClose = nil w.readClose = nil
if _, _, err := parseWalName(path.Base(w.tail().Name())); err != nil { if _, _, err := parseWalName(filepath.Base(w.tail().Name())); err != nil {
closer() closer()
return nil, err return nil, err
} }
@ -372,7 +372,7 @@ func (w *WAL) cut() error {
return err return err
} }
fpath := path.Join(w.dir, walName(w.seq()+1, w.enti+1)) fpath := filepath.Join(w.dir, walName(w.seq()+1, w.enti+1))
// create a temp wal file with name sequence + 1, or truncate the existing one // create a temp wal file with name sequence + 1, or truncate the existing one
newTail, err := w.fp.Open() newTail, err := w.fp.Open()
@ -464,7 +464,7 @@ func (w *WAL) ReleaseLockTo(index uint64) error {
found := false found := false
for i, l := range w.locks { for i, l := range w.locks {
_, lockIndex, err := parseWalName(path.Base(l.Name())) _, lockIndex, err := parseWalName(filepath.Base(l.Name()))
if err != nil { if err != nil {
return err return err
} }
@ -611,7 +611,7 @@ func (w *WAL) seq() uint64 {
if t == nil { if t == nil {
return 0 return 0
} }
seq, _, err := parseWalName(path.Base(t.Name())) seq, _, err := parseWalName(filepath.Base(t.Name()))
if err != nil { if err != nil {
plog.Fatalf("bad wal name %s (%v)", t.Name(), err) plog.Fatalf("bad wal name %s (%v)", t.Name(), err)
} }

View File

@ -19,7 +19,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path/filepath"
"reflect" "reflect"
"testing" "testing"
@ -40,7 +40,7 @@ func TestNew(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err = %v, want nil", err) t.Fatalf("err = %v, want nil", err)
} }
if g := path.Base(w.tail().Name()); g != walName(0, 0) { if g := filepath.Base(w.tail().Name()); g != walName(0, 0) {
t.Errorf("name = %+v, want %+v", g, walName(0, 0)) t.Errorf("name = %+v, want %+v", g, walName(0, 0))
} }
defer w.Close() defer w.Close()
@ -51,7 +51,7 @@ func TestNew(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
gd := make([]byte, off) gd := make([]byte, off)
f, err := os.Open(path.Join(p, path.Base(w.tail().Name()))) f, err := os.Open(filepath.Join(p, filepath.Base(w.tail().Name())))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -90,7 +90,7 @@ func TestNewForInitedDir(t *testing.T) {
} }
defer os.RemoveAll(p) defer os.RemoveAll(p)
os.Create(path.Join(p, walName(0, 0))) os.Create(filepath.Join(p, walName(0, 0)))
if _, err = Create(p, nil); err == nil || err != os.ErrExist { if _, err = Create(p, nil); err == nil || err != os.ErrExist {
t.Errorf("err = %v, want %v", err, os.ErrExist) t.Errorf("err = %v, want %v", err, os.ErrExist)
} }
@ -103,7 +103,7 @@ func TestOpenAtIndex(t *testing.T) {
} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
f, err := os.Create(path.Join(dir, walName(0, 0))) f, err := os.Create(filepath.Join(dir, walName(0, 0)))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -113,7 +113,7 @@ func TestOpenAtIndex(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err = %v, want nil", err) t.Fatalf("err = %v, want nil", err)
} }
if g := path.Base(w.tail().Name()); g != walName(0, 0) { if g := filepath.Base(w.tail().Name()); g != walName(0, 0) {
t.Errorf("name = %+v, want %+v", g, walName(0, 0)) t.Errorf("name = %+v, want %+v", g, walName(0, 0))
} }
if w.seq() != 0 { if w.seq() != 0 {
@ -122,7 +122,7 @@ func TestOpenAtIndex(t *testing.T) {
w.Close() w.Close()
wname := walName(2, 10) wname := walName(2, 10)
f, err = os.Create(path.Join(dir, wname)) f, err = os.Create(filepath.Join(dir, wname))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -132,7 +132,7 @@ func TestOpenAtIndex(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err = %v, want nil", err) t.Fatalf("err = %v, want nil", err)
} }
if g := path.Base(w.tail().Name()); g != wname { if g := filepath.Base(w.tail().Name()); g != wname {
t.Errorf("name = %+v, want %+v", g, wname) t.Errorf("name = %+v, want %+v", g, wname)
} }
if w.seq() != 2 { if w.seq() != 2 {
@ -172,7 +172,7 @@ func TestCut(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
wname := walName(1, 1) wname := walName(1, 1)
if g := path.Base(w.tail().Name()); g != wname { if g := filepath.Base(w.tail().Name()); g != wname {
t.Errorf("name = %s, want %s", g, wname) t.Errorf("name = %s, want %s", g, wname)
} }
@ -188,14 +188,14 @@ func TestCut(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
wname = walName(2, 2) wname = walName(2, 2)
if g := path.Base(w.tail().Name()); g != wname { if g := filepath.Base(w.tail().Name()); g != wname {
t.Errorf("name = %s, want %s", g, wname) t.Errorf("name = %s, want %s", g, wname)
} }
// check the state in the last WAL // check the state in the last WAL
// We do check before closing the WAL to ensure that Cut syncs the data // We do check before closing the WAL to ensure that Cut syncs the data
// into the disk. // into the disk.
f, err := os.Open(path.Join(p, wname)) f, err := os.Open(filepath.Join(p, wname))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -254,7 +254,7 @@ func TestSaveWithCut(t *testing.T) {
} }
defer neww.Close() defer neww.Close()
wname := walName(1, index) wname := walName(1, index)
if g := path.Base(neww.tail().Name()); g != wname { if g := filepath.Base(neww.tail().Name()); g != wname {
t.Errorf("name = %s, want %s", g, wname) t.Errorf("name = %s, want %s", g, wname)
} }
@ -416,7 +416,7 @@ func TestRecoverAfterCut(t *testing.T) {
} }
md.Close() md.Close()
if err := os.Remove(path.Join(p, walName(4, 4))); err != nil { if err := os.Remove(filepath.Join(p, walName(4, 4))); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -574,7 +574,7 @@ func TestReleaseLockTo(t *testing.T) {
} }
for i, l := range w.locks { for i, l := range w.locks {
var lockIndex uint64 var lockIndex uint64
_, lockIndex, err = parseWalName(path.Base(l.Name())) _, lockIndex, err = parseWalName(filepath.Base(l.Name()))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -592,7 +592,7 @@ func TestReleaseLockTo(t *testing.T) {
if len(w.locks) != 1 { if len(w.locks) != 1 {
t.Errorf("len(w.locks) = %d, want %d", len(w.locks), 1) t.Errorf("len(w.locks) = %d, want %d", len(w.locks), 1)
} }
_, lockIndex, err := parseWalName(path.Base(w.locks[0].Name())) _, lockIndex, err := parseWalName(filepath.Base(w.locks[0].Name()))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -677,11 +677,11 @@ func TestRestartCreateWal(t *testing.T) {
defer os.RemoveAll(p) defer os.RemoveAll(p)
// make temporary directory so it looks like initialization is interrupted // make temporary directory so it looks like initialization is interrupted
tmpdir := path.Clean(p) + ".tmp" tmpdir := filepath.Clean(p) + ".tmp"
if err = os.Mkdir(tmpdir, fileutil.PrivateDirMode); err != nil { if err = os.Mkdir(tmpdir, fileutil.PrivateDirMode); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if _, err = os.OpenFile(path.Join(tmpdir, "test"), os.O_WRONLY|os.O_CREATE, fileutil.PrivateFileMode); err != nil { if _, err = os.OpenFile(filepath.Join(tmpdir, "test"), os.O_WRONLY|os.O_CREATE, fileutil.PrivateFileMode); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -737,7 +737,7 @@ func TestOpenOnTornWrite(t *testing.T) {
} }
} }
fn := path.Join(p, path.Base(w.tail().Name())) fn := filepath.Join(p, filepath.Base(w.tail().Name()))
w.Close() w.Close()
// clobber some entry with 0's to simulate a torn write // clobber some entry with 0's to simulate a torn write