X Tutup
Skip to content

Commit e7b125f

Browse files
committed
cmd/api: be more robust against OS deleting temp files
OS X in particular deletes tmp files (but not directories) pretty reliably. Ask hg whether the go.tools directory in tmp is good before using it. Fixes issue Rob and others were reporting, which I just hit myself now. R=golang-dev, r CC=golang-dev https://golang.org/cl/13084049
1 parent cb79c57 commit e7b125f

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/cmd/api/run.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ func prepGoPath() string {
110110
tmpDir := filepath.Join(cloneDir, tempBase)
111111

112112
// finalDir is where the checkout will live once it's complete.
113-
// If this exists already, we're done.
114113
finalDir := filepath.Join(cloneDir, "go.tools")
115114

116-
if fi, err := os.Stat(finalDir); err == nil && fi.IsDir() {
115+
if goToolsCheckoutGood(finalDir) {
117116
return gopath
118117
}
118+
os.RemoveAll(finalDir) // in case it's there but corrupt
119+
os.RemoveAll(tmpDir) // in case of aborted hg clone before
119120

120121
if err := os.MkdirAll(cloneDir, 0700); err != nil {
121122
log.Fatal(err)
@@ -138,3 +139,29 @@ func prepGoPath() string {
138139
}
139140
return gopath
140141
}
142+
143+
func goToolsCheckoutGood(dir string) bool {
144+
if _, err := os.Stat(dir); err != nil {
145+
return false
146+
}
147+
148+
cmd := exec.Command("hg", "id", "--id")
149+
cmd.Dir = dir
150+
out, err := cmd.Output()
151+
if err != nil {
152+
return false
153+
}
154+
id := strings.TrimSpace(string(out))
155+
if id != goToolsVersion {
156+
return false
157+
}
158+
159+
cmd = exec.Command("hg", "status")
160+
cmd.Dir = dir
161+
out, err = cmd.Output()
162+
if err != nil || len(out) > 0 {
163+
return false
164+
}
165+
166+
return true
167+
}

0 commit comments

Comments
 (0)
X Tutup