Explorar o código

Allow a single upgrade at a time

Audrius Butkevicius %!s(int64=11) %!d(string=hai) anos
pai
achega
59ffec4e39

+ 28 - 0
internal/upgrade/upgrade_common.go

@@ -9,6 +9,8 @@ import (
 	"errors"
 	"strconv"
 	"strings"
+
+	"bitbucket.org/kardianos/osext"
 )
 
 type Release struct {
@@ -26,8 +28,34 @@ var (
 	ErrVersionUpToDate    = errors.New("current version is up to date")
 	ErrVersionUnknown     = errors.New("couldn't fetch release information")
 	ErrUpgradeUnsupported = errors.New("upgrade unsupported")
+	ErrUpgradeInProgress  = errors.New("upgrade already in progress")
+	upgradeUnlocked       = make(chan bool, 1)
 )
 
+func init() {
+	upgradeUnlocked <- true
+}
+
+// A wrapper around actual implementations
+func UpgradeTo(rel Release, archExtra string) error {
+	select {
+	case <-upgradeUnlocked:
+		path, err := osext.Executable()
+		if err != nil {
+			upgradeUnlocked <- true
+			return err
+		}
+		err = upgradeTo(path, rel, archExtra)
+		// If we've failed to upgrade, unlock so that another attempt could be made
+		if err != nil {
+			upgradeUnlocked <- true
+		}
+		return err
+	default:
+		return ErrUpgradeInProgress
+	}
+}
+
 // Returns 1 if a>b, -1 if a<b and 0 if they are equal
 func CompareVersions(a, b string) int {
 	arel, apre := versionParts(a)

+ 1 - 8
internal/upgrade/upgrade_supported.go

@@ -19,17 +19,10 @@ import (
 	"path/filepath"
 	"runtime"
 	"strings"
-
-	"bitbucket.org/kardianos/osext"
 )
 
 // Upgrade to the given release, saving the previous binary with a ".old" extension.
-func UpgradeTo(rel Release, archExtra string) error {
-	path, err := osext.Executable()
-	if err != nil {
-		return err
-	}
-
+func upgradeTo(path string, rel Release, archExtra string) error {
 	osName := runtime.GOOS
 	if osName == "darwin" {
 		// We call the darwin release bundles macosx because that makes more

+ 1 - 1
internal/upgrade/upgrade_unsupp.go

@@ -6,7 +6,7 @@
 
 package upgrade
 
-func UpgradeTo(rel Release, extra string) error {
+func upgradeTo(path string, rel Release, extra string) error {
 	return ErrUpgradeUnsupported
 }
 

+ 1 - 8
internal/upgrade/upgrade_windows.go

@@ -19,17 +19,10 @@ import (
 	"path/filepath"
 	"runtime"
 	"strings"
-
-	"bitbucket.org/kardianos/osext"
 )
 
 // Upgrade to the given release, saving the previous binary with a ".old" extension.
-func UpgradeTo(rel Release, archExtra string) error {
-	path, err := osext.Executable()
-	if err != nil {
-		return err
-	}
-
+func upgradeTo(path string, rel Release, archExtra string) error {
 	expectedRelease := fmt.Sprintf("syncthing-%s-%s%s-%s.", runtime.GOOS, runtime.GOARCH, archExtra, rel.Tag)
 	if debug {
 		l.Debugf("expected release asset %q", expectedRelease)