From 18c069479dc7fb90d72817e1c316bb4c62a16edd Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Fri, 13 Jan 2017 16:43:57 -0800 Subject: [PATCH] some saftey precautions for windows - change all "path" to "filepath" - fix rename since syscall.Rename on windows cannot rename a directory Signed-off-by: Jess Frazelle --- manager_test.go | 6 +++--- result.go | 3 +-- result_test.go | 14 +++++++------- util.go | 14 +++++++++----- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/manager_test.go b/manager_test.go index 752bef2..1f7539b 100644 --- a/manager_test.go +++ b/manager_test.go @@ -62,7 +62,7 @@ func mkNaiveSM(t *testing.T) (*SourceMgr, func()) { func init() { _, filename, _, _ := runtime.Caller(1) - bd = path.Dir(filename) + bd = filepath.Dir(filename) } func TestSourceManagerInit(t *testing.T) { @@ -83,7 +83,7 @@ func TestSourceManagerInit(t *testing.T) { t.Errorf("Should have gotten CouldNotCreateLockError error type, but got %T", te) } - if _, err = os.Stat(path.Join(cpath, "sm.lock")); err != nil { + if _, err = os.Stat(filepath.Join(cpath, "sm.lock")); err != nil { t.Errorf("Global cache lock file not created correctly") } @@ -93,7 +93,7 @@ func TestSourceManagerInit(t *testing.T) { t.Errorf("removeAll failed: %s", err) } - if _, err = os.Stat(path.Join(cpath, "sm.lock")); !os.IsNotExist(err) { + if _, err = os.Stat(filepath.Join(cpath, "sm.lock")); !os.IsNotExist(err) { t.Errorf("Global cache lock file not cleared correctly on Release()") t.FailNow() } diff --git a/result.go b/result.go index e38f08d..0bbdf09 100644 --- a/result.go +++ b/result.go @@ -3,7 +3,6 @@ package gps import ( "fmt" "os" - "path" "path/filepath" ) @@ -46,7 +45,7 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, sv bool) error { // TODO(sdboyer) parallelize for _, p := range l.Projects() { - to := path.Join(basedir, string(p.Ident().ProjectRoot)) + to := filepath.Join(basedir, string(p.Ident().ProjectRoot)) err := os.MkdirAll(to, 0777) if err != nil { diff --git a/result_test.go b/result_test.go index d0fd972..09af5a2 100644 --- a/result_test.go +++ b/result_test.go @@ -2,7 +2,7 @@ package gps import ( "os" - "path" + "path/filepath" "testing" ) @@ -45,19 +45,19 @@ func TestWriteDepTree(t *testing.T) { r := basicResult - tmp := path.Join(os.TempDir(), "vsolvtest") + tmp := filepath.Join(os.TempDir(), "vsolvtest") os.RemoveAll(tmp) sm, clean := mkNaiveSM(t) defer clean() // nil lock/result should err immediately - err := WriteDepTree(path.Join(tmp, "export"), nil, sm, true) + err := WriteDepTree(filepath.Join(tmp, "export"), nil, sm, true) if err == nil { t.Errorf("Should error if nil lock is passed to WriteDepTree") } - err = WriteDepTree(path.Join(tmp, "export"), r, sm, true) + err = WriteDepTree(filepath.Join(tmp, "export"), r, sm, true) if err != nil { t.Errorf("Unexpected error while creating vendor tree: %s", err) } @@ -70,10 +70,10 @@ func BenchmarkCreateVendorTree(b *testing.B) { b.SetParallelism(1) r := basicResult - tmp := path.Join(os.TempDir(), "vsolvtest") + tmp := filepath.Join(os.TempDir(), "vsolvtest") clean := true - sm, err := NewSourceManager(naiveAnalyzer{}, path.Join(tmp, "cache")) + sm, err := NewSourceManager(naiveAnalyzer{}, filepath.Join(tmp, "cache")) if err != nil { b.Errorf("NewSourceManager errored unexpectedly: %q", err) clean = false @@ -91,7 +91,7 @@ func BenchmarkCreateVendorTree(b *testing.B) { if clean { b.ResetTimer() b.StopTimer() - exp := path.Join(tmp, "export") + exp := filepath.Join(tmp, "export") for i := 0; i < b.N; i++ { // Order the loop this way to make it easy to disable final cleanup, to // ease manual inspection diff --git a/util.go b/util.go index 45d3dff..c549ad9 100644 --- a/util.go +++ b/util.go @@ -19,6 +19,14 @@ func renameWithFallback(src, dest string) error { return err } + // Windows cannot use syscall.Rename to rename a directory + if runtime.GOOS == "windows" && fi.IsDir() { + if err := copyDir(src, dest); err != nil { + return err + } + return os.RemoveAll(src) + } + err = os.Rename(src, dest) if err == nil { return nil @@ -48,11 +56,7 @@ func renameWithFallback(src, dest string) error { // 0x11 (ERROR_NOT_SAME_DEVICE) is the windows error. // See https://msdn.microsoft.com/en-us/library/cc231199.aspx if ok && noerr == 0x11 { - if fi.IsDir() { - cerr = copyDir(src, dest) - } else { - cerr = copyFile(src, dest) - } + cerr = copyFile(src, dest) } } else { return terr