Skip to content

Commit 9f43827

Browse files
committed
lib: Address https://v.io/i/1220
- reverts https://v.io/c/20483 - changes v23test to not share a bin dir across test main's (bin dir is still shared across tests within a given test main, but that's safe since "go test" will not run such tests concurrently by default) - changes v23test.StartRootMountTable and v23test.StartSyncbase to use gosh.Shell.FuncCmd (refactoring syncbased and mounttabled as needed to make this possible) A side effect of this change is that tests should run much faster, because for the common case of tests that previously used StartRootMountTable and/or StartSyncbase, but didn't directly call BuildGoPkg, we'll no longer build Go packages during test execution. MultiPart: 2/5 Change-Id: Iba81f1db56ca21f2d887e26496d7a87ad288b076
1 parent 8fe1276 commit 9f43827

File tree

3 files changed

+313
-227
lines changed

3 files changed

+313
-227
lines changed

gosh/shell.go

+35-12
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ func (sh *Shell) Wait() {
150150
sh.handleError(sh.wait())
151151
}
152152

153-
// Move moves a file.
153+
// Move moves a file from 'oldpath' to 'newpath'. It first attempts os.Rename;
154+
// if that fails, it copies 'oldpath' to 'newpath', then deletes 'oldpath'.
155+
// Requires that 'newpath' does not exist, and that the parent directory of
156+
// 'newpath' does exist. Currently only supports moving an individual file;
157+
// moving a directory is not yet supported.
154158
func (sh *Shell) Move(oldpath, newpath string) {
155159
sh.Ok()
156160
sh.handleError(sh.move(oldpath, newpath))
@@ -340,7 +344,7 @@ func (sh *Shell) wait() error {
340344
return res
341345
}
342346

343-
func copyFile(from, to string) error {
347+
func copyFile(to, from string) error {
344348
fi, err := os.Stat(from)
345349
if err != nil {
346350
return err
@@ -363,18 +367,33 @@ func copyFile(from, to string) error {
363367
}
364368

365369
func (sh *Shell) move(oldpath, newpath string) error {
366-
var err error
367-
if err = os.Rename(oldpath, newpath); err != nil {
368-
// Concurrent, same-directory rename operations sometimes fail on certain
369-
// filesystems, so we retry once after a random backoff.
370-
time.Sleep(time.Duration(rand.Int63n(1000)) * time.Millisecond)
371-
err = os.Rename(oldpath, newpath)
372-
}
373-
// If the error was a LinkError, try copying the file over.
374-
if _, ok := err.(*os.LinkError); !ok {
370+
fi, err := os.Stat(oldpath)
371+
if err != nil {
375372
return err
376373
}
377-
if err := copyFile(oldpath, newpath); err != nil {
374+
if fi.Mode().IsDir() {
375+
return errors.New("gosh: moving a directory is not yet supported")
376+
}
377+
if _, err := os.Stat(newpath); !os.IsNotExist(err) {
378+
return errors.New("gosh: destination file must not exist")
379+
}
380+
if _, err := os.Stat(filepath.Dir(newpath)); err != nil {
381+
if os.IsNotExist(err) {
382+
return errors.New("gosh: destination file's parent directory must exist")
383+
}
384+
return err
385+
}
386+
if err := os.Rename(oldpath, newpath); err == nil {
387+
return nil
388+
}
389+
// Concurrent, same-directory rename operations sometimes fail on certain
390+
// systems, so we retry once after a random backoff.
391+
time.Sleep(time.Duration(rand.Int63n(1000)) * time.Millisecond)
392+
if err := os.Rename(oldpath, newpath); err == nil {
393+
return nil
394+
}
395+
// Try copying the file over.
396+
if err := copyFile(newpath, oldpath); err != nil {
378397
return err
379398
}
380399
return os.Remove(oldpath)
@@ -540,6 +559,9 @@ var calledInitMain = false
540559
// parent process, it returns immediately with no effect. In a child process for
541560
// a Shell.FuncCmd command, it runs the specified function, then exits.
542561
func InitMain() {
562+
if calledInitMain {
563+
panic("gosh: already called gosh.InitMain")
564+
}
543565
calledInitMain = true
544566
s := os.Getenv(envInvocation)
545567
if s == "" {
@@ -629,5 +651,6 @@ func buildGoPkg(sh *Shell, binDir, pkg string, flags ...string) (string, error)
629651
if err := sh.move(tempBinPath, binPath); err != nil {
630652
return "", err
631653
}
654+
sh.tb.Logf("Built executable: %s\n", binPath)
632655
return binPath, nil
633656
}

0 commit comments

Comments
 (0)