Save temporary in correct dir during upgrade

This commit is contained in:
Jakob Borg 2014-05-02 17:04:45 +02:00
parent 9659d021cb
commit b374ec9355

View File

@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -64,7 +65,7 @@ func upgrade() error {
if strings.HasPrefix(asset.Name, expectedRelease) { if strings.HasPrefix(asset.Name, expectedRelease) {
if strings.HasSuffix(asset.Name, ".tar.gz") { if strings.HasSuffix(asset.Name, ".tar.gz") {
infof("Downloading %s...", asset.Name) infof("Downloading %s...", asset.Name)
fname, err := readTarGZ(asset.URL) fname, err := readTarGZ(asset.URL, filepath.Dir(path))
if err != nil { if err != nil {
return err return err
} }
@ -90,7 +91,7 @@ func upgrade() error {
return nil return nil
} }
func readTarGZ(url string) (string, error) { func readTarGZ(url string, dir string) (string, error) {
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
return "", err return "", err
@ -125,20 +126,19 @@ func readTarGZ(url string) (string, error) {
} }
if path.Base(hdr.Name) == "syncthing" { if path.Base(hdr.Name) == "syncthing" {
fname := filepath.Join(os.TempDir(), "syncthing.new") of, err := ioutil.TempFile(dir, "syncthing")
of, err := os.Create(fname)
if err != nil { if err != nil {
return "", err return "", err
} }
io.Copy(of, tr) io.Copy(of, tr)
err = of.Close() err = of.Close()
if err != nil { if err != nil {
os.Remove(fname) os.Remove(of.Name())
return "", err return "", err
} }
os.Chmod(fname, os.FileMode(hdr.Mode)) os.Chmod(of.Name(), os.FileMode(hdr.Mode))
return fname, nil return of.Name(), nil
} }
} }