Skip to content

Commit df9873f

Browse files
authored
Merge pull request #1435 from mattn/fix-upgrade-error-handling
Fail upgrade tool on download and write errors
2 parents fadf4a7 + 6ebb665 commit df9873f

1 file changed

Lines changed: 33 additions & 17 deletions

File tree

upgrade/upgrade.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func download(prefix string) (url string, content []byte, err error) {
2626
return "", nil, err
2727
}
2828
defer resp.Body.Close()
29+
if resp.StatusCode != http.StatusOK {
30+
return "", nil, fmt.Errorf("downloading download.html: %s", resp.Status)
31+
}
2932

3033
b, err := ioutil.ReadAll(resp.Body)
3134
if err != nil {
@@ -60,10 +63,13 @@ func download(prefix string) (url string, content []byte, err error) {
6063
if err != nil {
6164
return "", nil, err
6265
}
66+
defer resp.Body.Close()
67+
if resp.StatusCode != http.StatusOK {
68+
return "", nil, fmt.Errorf("downloading %s: %s", url, resp.Status)
69+
}
6370

6471
// Ready Body Content
6572
content, err = ioutil.ReadAll(resp.Body)
66-
defer resp.Body.Close()
6773
if err != nil {
6874
return "", nil, err
6975
}
@@ -72,41 +78,40 @@ func download(prefix string) (url string, content []byte, err error) {
7278
}
7379

7480
func mergeFile(src string, dst string) error {
75-
defer func() error {
76-
fmt.Printf("Removing: %s\n", src)
77-
err := os.Remove(src)
78-
79-
if err != nil {
80-
return err
81-
}
82-
83-
return nil
84-
}()
85-
8681
// Open destination
8782
fdst, err := os.OpenFile(dst, os.O_APPEND|os.O_WRONLY, 0666)
8883
if err != nil {
8984
return err
9085
}
91-
defer fdst.Close()
9286

9387
// Read source content
9488
content, err := ioutil.ReadFile(src)
9589
if err != nil {
90+
fdst.Close()
9691
return err
9792
}
9893

9994
// Add Additional newline
10095
if _, err := fdst.WriteString("\n"); err != nil {
96+
fdst.Close()
10197
return err
10298
}
10399

104100
fmt.Printf("Merging: %s into %s\n", src, dst)
105101
if _, err = fdst.Write(content); err != nil {
102+
fdst.Close()
106103
return err
107104
}
108105

109-
return nil
106+
// Close may surface deferred write errors; the source must survive
107+
// unless the merge fully reached the destination.
108+
if err := fdst.Close(); err != nil {
109+
return err
110+
}
111+
112+
// Only remove the source once it has been merged successfully.
113+
fmt.Printf("Removing: %s\n", src)
114+
return os.Remove(src)
110115
}
111116

112117
func main() {
@@ -170,6 +175,7 @@ func main() {
170175
log.Fatal(err)
171176
}
172177
scanner := bufio.NewScanner(zr)
178+
var werr error
173179
for scanner.Scan() {
174180
text := scanner.Text()
175181
if text == `#include "sqlite3.h"` {
@@ -179,11 +185,19 @@ func main() {
179185
#endif
180186
`
181187
}
182-
_, err = fmt.Fprintln(f, text)
183-
if err != nil {
188+
_, werr = fmt.Fprintln(f, text)
189+
if werr != nil {
184190
break
185191
}
186192
}
193+
// A write failure must not be masked by scanner.Err(), which
194+
// reports nil in that case; either way a truncated output file
195+
// must never be reported as successfully extracted.
196+
if werr != nil {
197+
zr.Close()
198+
f.Close()
199+
log.Fatal(werr)
200+
}
187201
err = scanner.Err()
188202
if err != nil {
189203
zr.Close()
@@ -197,7 +211,9 @@ func main() {
197211
log.Fatal(err)
198212
}
199213
zr.Close()
200-
f.Close()
214+
if err := f.Close(); err != nil {
215+
log.Fatal(err)
216+
}
201217
fmt.Printf("Extracted: %v\n", filepath.Base(f.Name()))
202218
}
203219

0 commit comments

Comments
 (0)