2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
// +build !noupgrade
|
2014-06-16 07:52:14 +00:00
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
package upgrade
|
2014-05-02 08:01:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
2014-12-22 10:03:17 +00:00
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
2014-05-02 08:01:09 +00:00
|
|
|
"compress/gzip"
|
2014-12-22 10:03:17 +00:00
|
|
|
"crypto/md5"
|
2014-05-02 08:01:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-05-02 15:04:45 +00:00
|
|
|
"io/ioutil"
|
2014-05-02 08:01:09 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2014-12-22 10:03:17 +00:00
|
|
|
"runtime"
|
2015-03-28 09:21:00 +00:00
|
|
|
"sort"
|
2014-05-02 08:01:09 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-04-09 20:44:36 +00:00
|
|
|
// Returns the latest releases, including prereleases or not depending on the argument
|
|
|
|
func LatestGithubReleases(version string) ([]Release, error) {
|
|
|
|
resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=30")
|
2014-07-14 08:45:29 +00:00
|
|
|
if err != nil {
|
2015-04-09 20:44:36 +00:00
|
|
|
return nil, err
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2014-07-31 08:26:45 +00:00
|
|
|
if resp.StatusCode > 299 {
|
2015-04-09 20:44:36 +00:00
|
|
|
return nil, fmt.Errorf("API call returned HTTP error: %s", resp.Status)
|
2014-07-31 08:26:45 +00:00
|
|
|
}
|
2014-07-14 08:45:29 +00:00
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
var rels []Release
|
2014-07-14 08:45:29 +00:00
|
|
|
json.NewDecoder(resp.Body).Decode(&rels)
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2015-04-09 20:44:36 +00:00
|
|
|
return rels, nil
|
2015-03-28 09:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SortByRelease []Release
|
|
|
|
|
|
|
|
func (s SortByRelease) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
func (s SortByRelease) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
func (s SortByRelease) Less(i, j int) bool {
|
|
|
|
return CompareVersions(s[i].Tag, s[j].Tag) > 0
|
|
|
|
}
|
|
|
|
|
2015-04-09 20:44:36 +00:00
|
|
|
func LatestRelease(version string) (Release, error) {
|
|
|
|
rels, _ := LatestGithubReleases(version)
|
|
|
|
return SelectLatestRelease(version, rels)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelectLatestRelease(version string, rels []Release) (Release, error) {
|
2014-07-31 07:04:57 +00:00
|
|
|
if len(rels) == 0 {
|
2014-07-31 14:01:23 +00:00
|
|
|
return Release{}, ErrVersionUnknown
|
2014-07-31 07:04:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 09:21:00 +00:00
|
|
|
sort.Sort(SortByRelease(rels))
|
|
|
|
// Check for a beta build
|
|
|
|
beta := strings.Contains(version, "-beta")
|
2014-12-08 15:36:15 +00:00
|
|
|
|
|
|
|
for _, rel := range rels {
|
2015-03-28 09:21:00 +00:00
|
|
|
if rel.Prerelease && !beta {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, asset := range rel.Assets {
|
|
|
|
assetName := path.Base(asset.Name)
|
|
|
|
// Check for the architecture
|
|
|
|
expectedRelease := releaseName(rel.Tag)
|
|
|
|
if debug {
|
|
|
|
l.Debugf("expected release asset %q", expectedRelease)
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
l.Debugln("considering release", assetName)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(assetName, expectedRelease) {
|
|
|
|
return rel, nil
|
|
|
|
}
|
2014-07-24 10:55:41 +00:00
|
|
|
}
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2014-12-08 15:36:15 +00:00
|
|
|
return Release{}, ErrVersionUnknown
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
// Upgrade to the given release, saving the previous binary with a ".old" extension.
|
|
|
|
func upgradeTo(binary string, rel Release) error {
|
|
|
|
expectedRelease := releaseName(rel.Tag)
|
|
|
|
if debug {
|
|
|
|
l.Debugf("expected release asset %q", expectedRelease)
|
|
|
|
}
|
|
|
|
for _, asset := range rel.Assets {
|
|
|
|
assetName := path.Base(asset.Name)
|
|
|
|
if debug {
|
|
|
|
l.Debugln("considering release", assetName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(assetName, expectedRelease) {
|
2015-01-04 18:19:00 +00:00
|
|
|
return upgradeToURL(binary, asset.URL)
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrVersionUnknown
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:07:04 +00:00
|
|
|
// Upgrade to the given release, saving the previous binary with a ".old" extension.
|
|
|
|
func upgradeToURL(binary string, url string) error {
|
|
|
|
fname, err := readRelease(filepath.Dir(binary), url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
old := binary + ".old"
|
2015-01-14 20:33:12 +00:00
|
|
|
os.Remove(old)
|
2014-12-22 11:07:04 +00:00
|
|
|
err = os.Rename(binary, old)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Rename(fname, binary)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
func readRelease(dir, url string) (string, error) {
|
2014-08-07 13:57:20 +00:00
|
|
|
if debug {
|
|
|
|
l.Debugf("loading %q", url)
|
|
|
|
}
|
|
|
|
|
2014-05-02 08:01:09 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Accept", "application/octet-stream")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
return readZip(dir, resp.Body)
|
|
|
|
default:
|
|
|
|
return readTarGz(dir, resp.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readTarGz(dir string, r io.Reader) (string, error) {
|
|
|
|
gr, err := gzip.NewReader(r)
|
2014-05-02 08:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(gr)
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
var tempName, actualMD5, expectedMD5 string
|
|
|
|
|
2014-05-02 08:01:09 +00:00
|
|
|
// Iterate through the files in the archive.
|
2014-12-22 10:03:17 +00:00
|
|
|
fileLoop:
|
2014-05-02 08:01:09 +00:00
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
|
|
|
shortName := path.Base(hdr.Name)
|
|
|
|
|
2014-08-07 13:57:20 +00:00
|
|
|
if debug {
|
2014-12-22 10:03:17 +00:00
|
|
|
l.Debugf("considering file %q", shortName)
|
2014-08-07 13:57:20 +00:00
|
|
|
}
|
2014-05-02 08:01:09 +00:00
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
switch shortName {
|
|
|
|
case "syncthing":
|
|
|
|
if debug {
|
|
|
|
l.Debugln("writing and hashing binary")
|
|
|
|
}
|
|
|
|
tempName, actualMD5, err = writeBinary(dir, tr)
|
2014-05-02 08:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 09:42:47 +00:00
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
if expectedMD5 != "" {
|
|
|
|
// We're done
|
|
|
|
break fileLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
case "syncthing.md5":
|
|
|
|
bs, err := ioutil.ReadAll(tr)
|
2014-12-22 09:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
expectedMD5 = strings.TrimSpace(string(bs))
|
|
|
|
if debug {
|
|
|
|
l.Debugln("expected md5 is", actualMD5)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualMD5 != "" {
|
|
|
|
// We're done
|
|
|
|
break fileLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:33:12 +00:00
|
|
|
if tempName != "" {
|
2014-12-22 10:03:17 +00:00
|
|
|
// We found and saved something to disk.
|
2015-01-14 20:33:12 +00:00
|
|
|
if expectedMD5 == "" || actualMD5 == expectedMD5 {
|
|
|
|
return tempName, nil
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
2015-01-14 20:33:12 +00:00
|
|
|
os.Remove(tempName)
|
|
|
|
// There was an md5 file included in the archive, and it doesn't
|
|
|
|
// match what we just wrote to disk.
|
|
|
|
return "", fmt.Errorf("incorrect MD5 checksum")
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
return "", fmt.Errorf("no upgrade found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func readZip(dir string, r io.Reader) (string, error) {
|
|
|
|
body, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
archive, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tempName, actualMD5, expectedMD5 string
|
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
|
|
|
fileLoop:
|
|
|
|
for _, file := range archive.File {
|
|
|
|
shortName := path.Base(file.Name)
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
l.Debugf("considering file %q", shortName)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch shortName {
|
|
|
|
case "syncthing.exe":
|
|
|
|
if debug {
|
|
|
|
l.Debugln("writing and hashing binary")
|
|
|
|
}
|
|
|
|
|
|
|
|
inFile, err := file.Open()
|
2014-05-02 08:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
tempName, actualMD5, err = writeBinary(dir, inFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedMD5 != "" {
|
|
|
|
// We're done
|
|
|
|
break fileLoop
|
|
|
|
}
|
2014-05-02 08:01:09 +00:00
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
case "syncthing.exe.md5":
|
|
|
|
inFile, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
bs, err := ioutil.ReadAll(inFile)
|
2014-12-22 09:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
expectedMD5 = strings.TrimSpace(string(bs))
|
|
|
|
if debug {
|
|
|
|
l.Debugln("expected md5 is", actualMD5)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualMD5 != "" {
|
|
|
|
// We're done
|
|
|
|
break fileLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:33:12 +00:00
|
|
|
if tempName != "" {
|
2014-12-22 10:03:17 +00:00
|
|
|
// We found and saved something to disk.
|
2015-01-14 20:33:12 +00:00
|
|
|
if expectedMD5 == "" || actualMD5 == expectedMD5 {
|
|
|
|
return tempName, nil
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
2015-01-14 20:33:12 +00:00
|
|
|
os.Remove(tempName)
|
|
|
|
// There was an md5 file included in the archive, and it doesn't
|
|
|
|
// match what we just wrote to disk.
|
|
|
|
return "", fmt.Errorf("incorrect MD5 checksum")
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
return "", fmt.Errorf("No upgrade found")
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
|
|
|
func writeBinary(dir string, inFile io.Reader) (filename, md5sum string, err error) {
|
|
|
|
outFile, err := ioutil.TempFile(dir, "syncthing")
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the binary both a temporary file and to the MD5 hasher.
|
|
|
|
|
|
|
|
h := md5.New()
|
|
|
|
mw := io.MultiWriter(h, outFile)
|
|
|
|
|
|
|
|
_, err = io.Copy(mw, inFile)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(outFile.Name())
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = outFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(outFile.Name())
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(outFile.Name(), os.FileMode(0755))
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(outFile.Name())
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
actualMD5 := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
if debug {
|
|
|
|
l.Debugln("actual md5 is", actualMD5)
|
|
|
|
}
|
|
|
|
|
|
|
|
return outFile.Name(), actualMD5, nil
|
|
|
|
}
|