Fail upgrade tool on download and write errors
This commit is contained in:
@@ -26,6 +26,9 @@ func download(prefix string) (url string, content []byte, err error) {
|
|||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return "", nil, fmt.Errorf("downloading download.html: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -60,10 +63,13 @@ func download(prefix string) (url string, content []byte, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return "", nil, fmt.Errorf("downloading %s: %s", url, resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
// Ready Body Content
|
// Ready Body Content
|
||||||
content, err = ioutil.ReadAll(resp.Body)
|
content, err = ioutil.ReadAll(resp.Body)
|
||||||
defer resp.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
@@ -72,17 +78,6 @@ func download(prefix string) (url string, content []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mergeFile(src string, dst string) error {
|
func mergeFile(src string, dst string) error {
|
||||||
defer func() error {
|
|
||||||
fmt.Printf("Removing: %s\n", src)
|
|
||||||
err := os.Remove(src)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Open destination
|
// Open destination
|
||||||
fdst, err := os.OpenFile(dst, os.O_APPEND|os.O_WRONLY, 0666)
|
fdst, err := os.OpenFile(dst, os.O_APPEND|os.O_WRONLY, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -106,7 +101,9 @@ func mergeFile(src string, dst string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
// Only remove the source once it has been merged successfully.
|
||||||
|
fmt.Printf("Removing: %s\n", src)
|
||||||
|
return os.Remove(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -170,6 +167,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
scanner := bufio.NewScanner(zr)
|
scanner := bufio.NewScanner(zr)
|
||||||
|
var werr error
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
text := scanner.Text()
|
text := scanner.Text()
|
||||||
if text == `#include "sqlite3.h"` {
|
if text == `#include "sqlite3.h"` {
|
||||||
@@ -179,11 +177,19 @@ func main() {
|
|||||||
#endif
|
#endif
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
_, err = fmt.Fprintln(f, text)
|
_, werr = fmt.Fprintln(f, text)
|
||||||
if err != nil {
|
if werr != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// A write failure must not be masked by scanner.Err(), which
|
||||||
|
// reports nil in that case; either way a truncated output file
|
||||||
|
// must never be reported as successfully extracted.
|
||||||
|
if werr != nil {
|
||||||
|
zr.Close()
|
||||||
|
f.Close()
|
||||||
|
log.Fatal(werr)
|
||||||
|
}
|
||||||
err = scanner.Err()
|
err = scanner.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zr.Close()
|
zr.Close()
|
||||||
@@ -197,7 +203,9 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
zr.Close()
|
zr.Close()
|
||||||
f.Close()
|
if err := f.Close(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
fmt.Printf("Extracted: %v\n", filepath.Base(f.Name()))
|
fmt.Printf("Extracted: %v\n", filepath.Base(f.Name()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user