upgrade_common.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // Package upgrade downloads and compares releases, and upgrades the running binary.
  16. package upgrade
  17. import (
  18. "errors"
  19. "strconv"
  20. "strings"
  21. "github.com/calmh/osext"
  22. )
  23. type Release struct {
  24. Tag string `json:"tag_name"`
  25. Prerelease bool `json:"prerelease"`
  26. Assets []Asset `json:"assets"`
  27. }
  28. type Asset struct {
  29. URL string `json:"url"`
  30. Name string `json:"name"`
  31. }
  32. var (
  33. ErrVersionUpToDate = errors.New("current version is up to date")
  34. ErrVersionUnknown = errors.New("couldn't fetch release information")
  35. ErrUpgradeUnsupported = errors.New("upgrade unsupported")
  36. ErrUpgradeInProgress = errors.New("upgrade already in progress")
  37. upgradeUnlocked = make(chan bool, 1)
  38. )
  39. func init() {
  40. upgradeUnlocked <- true
  41. }
  42. // A wrapper around actual implementations
  43. func To(rel Release) error {
  44. select {
  45. case <-upgradeUnlocked:
  46. path, err := osext.Executable()
  47. if err != nil {
  48. upgradeUnlocked <- true
  49. return err
  50. }
  51. err = upgradeTo(path, rel)
  52. // If we've failed to upgrade, unlock so that another attempt could be made
  53. if err != nil {
  54. upgradeUnlocked <- true
  55. }
  56. return err
  57. default:
  58. return ErrUpgradeInProgress
  59. }
  60. }
  61. // Returns 1 if a>b, -1 if a<b and 0 if they are equal
  62. func CompareVersions(a, b string) int {
  63. arel, apre := versionParts(a)
  64. brel, bpre := versionParts(b)
  65. minlen := len(arel)
  66. if l := len(brel); l < minlen {
  67. minlen = l
  68. }
  69. // First compare major-minor-patch versions
  70. for i := 0; i < minlen; i++ {
  71. if arel[i] < brel[i] {
  72. return -1
  73. }
  74. if arel[i] > brel[i] {
  75. return 1
  76. }
  77. }
  78. // Longer version is newer, when the preceding parts are equal
  79. if len(arel) < len(brel) {
  80. return -1
  81. }
  82. if len(arel) > len(brel) {
  83. return 1
  84. }
  85. // Prerelease versions are older, if the versions are the same
  86. if len(apre) == 0 && len(bpre) > 0 {
  87. return 1
  88. }
  89. if len(apre) > 0 && len(bpre) == 0 {
  90. return -1
  91. }
  92. minlen = len(apre)
  93. if l := len(bpre); l < minlen {
  94. minlen = l
  95. }
  96. // Compare prerelease strings
  97. for i := 0; i < minlen; i++ {
  98. switch av := apre[i].(type) {
  99. case int:
  100. switch bv := bpre[i].(type) {
  101. case int:
  102. if av < bv {
  103. return -1
  104. }
  105. if av > bv {
  106. return 1
  107. }
  108. case string:
  109. return -1
  110. }
  111. case string:
  112. switch bv := bpre[i].(type) {
  113. case int:
  114. return 1
  115. case string:
  116. if av < bv {
  117. return -1
  118. }
  119. if av > bv {
  120. return 1
  121. }
  122. }
  123. }
  124. }
  125. // If all else is equal, longer prerelease string is newer
  126. if len(apre) < len(bpre) {
  127. return -1
  128. }
  129. if len(apre) > len(bpre) {
  130. return 1
  131. }
  132. // Looks like they're actually the same
  133. return 0
  134. }
  135. // Split a version into parts.
  136. // "1.2.3-beta.2" -> []int{1, 2, 3}, []interface{}{"beta", 2}
  137. func versionParts(v string) ([]int, []interface{}) {
  138. if strings.HasPrefix(v, "v") || strings.HasPrefix(v, "V") {
  139. // Strip initial 'v' or 'V' prefix if present.
  140. v = v[1:]
  141. }
  142. parts := strings.SplitN(v, "+", 2)
  143. parts = strings.SplitN(parts[0], "-", 2)
  144. fields := strings.Split(parts[0], ".")
  145. release := make([]int, len(fields))
  146. for i, s := range fields {
  147. v, _ := strconv.Atoi(s)
  148. release[i] = v
  149. }
  150. var prerelease []interface{}
  151. if len(parts) > 1 {
  152. fields = strings.Split(parts[1], ".")
  153. prerelease = make([]interface{}, len(fields))
  154. for i, s := range fields {
  155. v, err := strconv.Atoi(s)
  156. if err == nil {
  157. prerelease[i] = v
  158. } else {
  159. prerelease[i] = s
  160. }
  161. }
  162. }
  163. return release, prerelease
  164. }