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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-07-31 14:01:23 +00:00
|
|
|
|
2014-07-31 15:01:11 +00:00
|
|
|
// Package upgrade downloads and compares releases, and upgrades the running binary.
|
2014-07-31 14:01:23 +00:00
|
|
|
package upgrade
|
2014-07-14 08:45:29 +00:00
|
|
|
|
|
|
|
import (
|
2014-07-31 09:47:00 +00:00
|
|
|
"errors"
|
2015-01-09 09:21:30 +00:00
|
|
|
"fmt"
|
2018-05-13 05:43:24 +00:00
|
|
|
"os"
|
2016-05-06 13:30:35 +00:00
|
|
|
"path"
|
2015-01-09 09:21:30 +00:00
|
|
|
"runtime"
|
2014-07-14 08:45:29 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
type Release struct {
|
|
|
|
Tag string `json:"tag_name"`
|
|
|
|
Prerelease bool `json:"prerelease"`
|
|
|
|
Assets []Asset `json:"assets"`
|
2019-10-08 07:10:04 +00:00
|
|
|
|
|
|
|
// The HTML URL is needed for human readable links in the output created
|
|
|
|
// by cmd/stupgrades.
|
|
|
|
HTMLURL string `json:"html_url"`
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
type Asset struct {
|
2014-07-14 08:45:29 +00:00
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
2019-08-29 14:10:13 +00:00
|
|
|
|
|
|
|
// The browser URL is needed for human readable links in the output created
|
|
|
|
// by cmd/stupgrades.
|
|
|
|
BrowserURL string `json:"browser_download_url"`
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 09:47:00 +00:00
|
|
|
var (
|
2015-11-24 00:37:42 +00:00
|
|
|
ErrNoReleaseDownload = errors.New("couldn't find a release to download")
|
|
|
|
ErrNoVersionToSelect = errors.New("no version to select")
|
2014-07-31 14:01:23 +00:00
|
|
|
ErrUpgradeUnsupported = errors.New("upgrade unsupported")
|
2014-09-25 22:13:41 +00:00
|
|
|
ErrUpgradeInProgress = errors.New("upgrade already in progress")
|
|
|
|
upgradeUnlocked = make(chan bool, 1)
|
2014-07-31 09:47:00 +00:00
|
|
|
)
|
|
|
|
|
2014-09-25 22:13:41 +00:00
|
|
|
func init() {
|
|
|
|
upgradeUnlocked <- true
|
|
|
|
}
|
|
|
|
|
2014-12-08 15:36:15 +00:00
|
|
|
func To(rel Release) error {
|
2014-09-25 22:13:41 +00:00
|
|
|
select {
|
|
|
|
case <-upgradeUnlocked:
|
2018-05-13 05:43:24 +00:00
|
|
|
path, err := os.Executable()
|
2014-09-28 21:09:55 +00:00
|
|
|
if err != nil {
|
|
|
|
upgradeUnlocked <- true
|
|
|
|
return err
|
|
|
|
}
|
2014-12-01 09:24:13 +00:00
|
|
|
err = upgradeTo(path, rel)
|
2014-09-25 22:13:41 +00:00
|
|
|
// If we've failed to upgrade, unlock so that another attempt could be made
|
|
|
|
if err != nil {
|
|
|
|
upgradeUnlocked <- true
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return ErrUpgradeInProgress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:07:04 +00:00
|
|
|
func ToURL(url string) error {
|
|
|
|
select {
|
|
|
|
case <-upgradeUnlocked:
|
2018-05-13 05:43:24 +00:00
|
|
|
binary, err := os.Executable()
|
2014-12-22 11:07:04 +00:00
|
|
|
if err != nil {
|
|
|
|
upgradeUnlocked <- true
|
|
|
|
return err
|
|
|
|
}
|
2016-05-06 13:30:35 +00:00
|
|
|
err = upgradeToURL(path.Base(url), binary, url)
|
2014-12-22 11:07:04 +00:00
|
|
|
// If we've failed to upgrade, unlock so that another attempt could be made
|
|
|
|
if err != nil {
|
|
|
|
upgradeUnlocked <- true
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return ErrUpgradeInProgress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 11:21:08 +00:00
|
|
|
type Relation int
|
|
|
|
|
|
|
|
const (
|
|
|
|
MajorOlder Relation = -2 // Older by a major version (x in x.y.z or 0.x.y).
|
2019-02-02 09:11:42 +00:00
|
|
|
Older Relation = -1 // Older by a minor version (y or z in x.y.z, or y in 0.x.y)
|
|
|
|
Equal Relation = 0 // Versions are semantically equal
|
|
|
|
Newer Relation = 1 // Newer by a minor version (y or z in x.y.z, or y in 0.x.y)
|
|
|
|
MajorNewer Relation = 2 // Newer by a major version (x in x.y.z or 0.x.y).
|
2014-12-29 11:21:08 +00:00
|
|
|
)
|
|
|
|
|
2015-04-28 20:32:10 +00:00
|
|
|
// CompareVersions returns a relation describing how a compares to b.
|
2014-12-29 11:21:08 +00:00
|
|
|
func CompareVersions(a, b string) Relation {
|
2014-07-30 06:57:27 +00:00
|
|
|
arel, apre := versionParts(a)
|
|
|
|
brel, bpre := versionParts(b)
|
|
|
|
|
|
|
|
minlen := len(arel)
|
|
|
|
if l := len(brel); l < minlen {
|
|
|
|
minlen = l
|
|
|
|
}
|
|
|
|
|
|
|
|
// First compare major-minor-patch versions
|
|
|
|
for i := 0; i < minlen; i++ {
|
|
|
|
if arel[i] < brel[i] {
|
2014-12-29 11:21:08 +00:00
|
|
|
if i == 0 {
|
2017-06-25 14:17:43 +00:00
|
|
|
// major version difference
|
|
|
|
if arel[0] == 0 && brel[0] == 1 {
|
|
|
|
// special case, v0.x is equivalent in majorness to v1.x.
|
|
|
|
return Older
|
|
|
|
}
|
2014-12-29 11:21:08 +00:00
|
|
|
return MajorOlder
|
|
|
|
}
|
2017-06-25 14:17:43 +00:00
|
|
|
// minor or patch version difference
|
2014-12-29 11:21:08 +00:00
|
|
|
return Older
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
if arel[i] > brel[i] {
|
2014-12-29 11:21:08 +00:00
|
|
|
if i == 0 {
|
2017-06-25 14:17:43 +00:00
|
|
|
// major version difference
|
|
|
|
if arel[0] == 1 && brel[0] == 0 {
|
|
|
|
// special case, v0.x is equivalent in majorness to v1.x.
|
|
|
|
return Newer
|
|
|
|
}
|
2014-12-29 11:21:08 +00:00
|
|
|
return MajorNewer
|
|
|
|
}
|
2017-06-25 14:17:43 +00:00
|
|
|
// minor or patch version difference
|
2014-12-29 11:21:08 +00:00
|
|
|
return Newer
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Longer version is newer, when the preceding parts are equal
|
|
|
|
if len(arel) < len(brel) {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Older
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
if len(arel) > len(brel) {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Newer
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prerelease versions are older, if the versions are the same
|
|
|
|
if len(apre) == 0 && len(bpre) > 0 {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Newer
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
if len(apre) > 0 && len(bpre) == 0 {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Older
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
minlen = len(apre)
|
|
|
|
if l := len(bpre); l < minlen {
|
|
|
|
minlen = l
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare prerelease strings
|
|
|
|
for i := 0; i < minlen; i++ {
|
|
|
|
switch av := apre[i].(type) {
|
|
|
|
case int:
|
|
|
|
switch bv := bpre[i].(type) {
|
|
|
|
case int:
|
|
|
|
if av < bv {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Older
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
if av > bv {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Newer
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
case string:
|
2014-12-29 11:21:08 +00:00
|
|
|
return Older
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
case string:
|
|
|
|
switch bv := bpre[i].(type) {
|
|
|
|
case int:
|
2014-12-29 11:21:08 +00:00
|
|
|
return Newer
|
2014-07-30 06:57:27 +00:00
|
|
|
case string:
|
|
|
|
if av < bv {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Older
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
if av > bv {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Newer
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all else is equal, longer prerelease string is newer
|
|
|
|
if len(apre) < len(bpre) {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Older
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
if len(apre) > len(bpre) {
|
2014-12-29 11:21:08 +00:00
|
|
|
return Newer
|
2014-07-30 06:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Looks like they're actually the same
|
2014-12-29 11:21:08 +00:00
|
|
|
return Equal
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 06:57:27 +00:00
|
|
|
// Split a version into parts.
|
|
|
|
// "1.2.3-beta.2" -> []int{1, 2, 3}, []interface{}{"beta", 2}
|
|
|
|
func versionParts(v string) ([]int, []interface{}) {
|
2014-11-17 14:49:51 +00:00
|
|
|
if strings.HasPrefix(v, "v") || strings.HasPrefix(v, "V") {
|
|
|
|
// Strip initial 'v' or 'V' prefix if present.
|
|
|
|
v = v[1:]
|
|
|
|
}
|
2014-08-05 17:38:31 +00:00
|
|
|
parts := strings.SplitN(v, "+", 2)
|
|
|
|
parts = strings.SplitN(parts[0], "-", 2)
|
2014-07-14 08:45:29 +00:00
|
|
|
fields := strings.Split(parts[0], ".")
|
2014-07-30 06:57:27 +00:00
|
|
|
|
|
|
|
release := make([]int, len(fields))
|
2014-07-14 08:45:29 +00:00
|
|
|
for i, s := range fields {
|
|
|
|
v, _ := strconv.Atoi(s)
|
2014-07-30 06:57:27 +00:00
|
|
|
release[i] = v
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2014-07-30 06:57:27 +00:00
|
|
|
|
|
|
|
var prerelease []interface{}
|
|
|
|
if len(parts) > 1 {
|
|
|
|
fields = strings.Split(parts[1], ".")
|
|
|
|
prerelease = make([]interface{}, len(fields))
|
|
|
|
for i, s := range fields {
|
|
|
|
v, err := strconv.Atoi(s)
|
|
|
|
if err == nil {
|
|
|
|
prerelease[i] = v
|
|
|
|
} else {
|
|
|
|
prerelease[i] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return release, prerelease
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2015-01-09 09:21:30 +00:00
|
|
|
|
2018-01-27 09:07:19 +00:00
|
|
|
func releaseNames(tag string) []string {
|
2016-05-06 13:30:35 +00:00
|
|
|
// We must ensure that the release asset matches the expected naming
|
|
|
|
// standard, containing both the architecture/OS and the tag name we
|
|
|
|
// expect. This protects against malformed release data potentially
|
|
|
|
// tricking us into doing a downgrade.
|
2015-01-09 09:21:30 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
2018-01-27 09:07:19 +00:00
|
|
|
return []string{
|
|
|
|
fmt.Sprintf("syncthing-macos-%s-%s.", runtime.GOARCH, tag),
|
|
|
|
fmt.Sprintf("syncthing-macosx-%s-%s.", runtime.GOARCH, tag),
|
|
|
|
}
|
2015-01-09 09:21:30 +00:00
|
|
|
default:
|
2018-01-27 09:07:19 +00:00
|
|
|
return []string{
|
|
|
|
fmt.Sprintf("syncthing-%s-%s-%s.", runtime.GOOS, runtime.GOARCH, tag),
|
|
|
|
}
|
2015-01-09 09:21:30 +00:00
|
|
|
}
|
|
|
|
}
|