upgrade_common.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // Package upgrade downloads and compares releases, and upgrades the running binary.
  7. package upgrade
  8. import (
  9. "errors"
  10. "fmt"
  11. "os"
  12. "path"
  13. "runtime"
  14. "strconv"
  15. "strings"
  16. "github.com/syncthing/syncthing/lib/build"
  17. )
  18. type Release struct {
  19. Tag string `json:"tag_name"`
  20. Prerelease bool `json:"prerelease"`
  21. Assets []Asset `json:"assets"`
  22. // The HTML URL is needed for human readable links in the output created
  23. // by cmd/stupgrades.
  24. HTMLURL string `json:"html_url"`
  25. }
  26. type Asset struct {
  27. URL string `json:"url"`
  28. Name string `json:"name"`
  29. // The browser URL is needed for human readable links in the output created
  30. // by cmd/stupgrades.
  31. BrowserURL string `json:"browser_download_url,omitempty"`
  32. }
  33. // ReleaseCompatibility defines the structure of compat.json, which is
  34. // included with each elease.
  35. type ReleaseCompatibility struct {
  36. Runtime string `json:"runtime,omitempty"`
  37. Requirements map[string]string `json:"requirements,omitempty"`
  38. }
  39. var (
  40. ErrNoReleaseDownload = errors.New("couldn't find a release to download")
  41. ErrNoVersionToSelect = errors.New("no version to select")
  42. ErrUpgradeUnsupported = errors.New("upgrade unsupported")
  43. ErrUpgradeInProgress = errors.New("upgrade already in progress")
  44. upgradeUnlocked = make(chan bool, 1)
  45. )
  46. func init() {
  47. upgradeUnlocked <- true
  48. }
  49. func To(rel Release) error {
  50. select {
  51. case <-upgradeUnlocked:
  52. path, err := os.Executable()
  53. if err != nil {
  54. upgradeUnlocked <- true
  55. return err
  56. }
  57. err = upgradeTo(path, rel)
  58. // If we've failed to upgrade, unlock so that another attempt could be made
  59. if err != nil {
  60. upgradeUnlocked <- true
  61. }
  62. return err
  63. default:
  64. return ErrUpgradeInProgress
  65. }
  66. }
  67. func ToURL(url string) error {
  68. select {
  69. case <-upgradeUnlocked:
  70. binary, err := os.Executable()
  71. if err != nil {
  72. upgradeUnlocked <- true
  73. return err
  74. }
  75. err = upgradeToURL(path.Base(url), binary, url)
  76. // If we've failed to upgrade, unlock so that another attempt could be made
  77. if err != nil {
  78. upgradeUnlocked <- true
  79. }
  80. return err
  81. default:
  82. return ErrUpgradeInProgress
  83. }
  84. }
  85. type Relation int
  86. const (
  87. MajorOlder Relation = -2 // Older by a major version (x in x.y.z or 0.x.y).
  88. Older Relation = -1 // Older by a minor version (y or z in x.y.z, or y in 0.x.y)
  89. Equal Relation = 0 // Versions are semantically equal
  90. Newer Relation = 1 // Newer by a minor version (y or z in x.y.z, or y in 0.x.y)
  91. MajorNewer Relation = 2 // Newer by a major version (x in x.y.z or 0.x.y).
  92. )
  93. // CompareVersions returns a relation describing how a compares to b.
  94. func CompareVersions(a, b string) Relation {
  95. arel, apre := versionParts(a)
  96. brel, bpre := versionParts(b)
  97. minlen := len(arel)
  98. if l := len(brel); l < minlen {
  99. minlen = l
  100. }
  101. // First compare major-minor-patch versions
  102. for i := 0; i < minlen; i++ {
  103. if arel[i] < brel[i] {
  104. if i == 0 {
  105. // major version difference
  106. if arel[0] == 0 && brel[0] == 1 {
  107. // special case, v0.x is equivalent in majorness to v1.x.
  108. return Older
  109. }
  110. return MajorOlder
  111. }
  112. // minor or patch version difference
  113. return Older
  114. }
  115. if arel[i] > brel[i] {
  116. if i == 0 {
  117. // major version difference
  118. if arel[0] == 1 && brel[0] == 0 {
  119. // special case, v0.x is equivalent in majorness to v1.x.
  120. return Newer
  121. }
  122. return MajorNewer
  123. }
  124. // minor or patch version difference
  125. return Newer
  126. }
  127. }
  128. // Longer version is newer, when the preceding parts are equal
  129. if len(arel) < len(brel) {
  130. return Older
  131. }
  132. if len(arel) > len(brel) {
  133. return Newer
  134. }
  135. // Prerelease versions are older, if the versions are the same
  136. if len(apre) == 0 && len(bpre) > 0 {
  137. return Newer
  138. }
  139. if len(apre) > 0 && len(bpre) == 0 {
  140. return Older
  141. }
  142. minlen = len(apre)
  143. if l := len(bpre); l < minlen {
  144. minlen = l
  145. }
  146. // Compare prerelease strings
  147. for i := 0; i < minlen; i++ {
  148. switch av := apre[i].(type) {
  149. case int:
  150. switch bv := bpre[i].(type) {
  151. case int:
  152. if av < bv {
  153. return Older
  154. }
  155. if av > bv {
  156. return Newer
  157. }
  158. case string:
  159. return Older
  160. }
  161. case string:
  162. switch bv := bpre[i].(type) {
  163. case int:
  164. return Newer
  165. case string:
  166. if av < bv {
  167. return Older
  168. }
  169. if av > bv {
  170. return Newer
  171. }
  172. }
  173. }
  174. }
  175. // If all else is equal, longer prerelease string is newer
  176. if len(apre) < len(bpre) {
  177. return Older
  178. }
  179. if len(apre) > len(bpre) {
  180. return Newer
  181. }
  182. // Looks like they're actually the same
  183. return Equal
  184. }
  185. // Split a version into parts.
  186. // "1.2.3-beta.2" -> []int{1, 2, 3}, []interface{}{"beta", 2}
  187. func versionParts(v string) ([]int, []interface{}) {
  188. if strings.HasPrefix(v, "v") || strings.HasPrefix(v, "V") {
  189. // Strip initial 'v' or 'V' prefix if present.
  190. v = v[1:]
  191. }
  192. parts := strings.SplitN(v, "+", 2)
  193. parts = strings.SplitN(parts[0], "-", 2)
  194. fields := strings.Split(parts[0], ".")
  195. release := make([]int, len(fields))
  196. for i, s := range fields {
  197. v, _ := strconv.Atoi(s)
  198. release[i] = v
  199. }
  200. var prerelease []interface{}
  201. if len(parts) > 1 {
  202. fields = strings.Split(parts[1], ".")
  203. prerelease = make([]interface{}, len(fields))
  204. for i, s := range fields {
  205. v, err := strconv.Atoi(s)
  206. if err == nil {
  207. prerelease[i] = v
  208. } else {
  209. prerelease[i] = s
  210. }
  211. }
  212. }
  213. return release, prerelease
  214. }
  215. func releaseNames(tag string) []string {
  216. // We must ensure that the release asset matches the expected naming
  217. // standard, containing both the architecture/OS and the tag name we
  218. // expect. This protects against malformed release data potentially
  219. // tricking us into doing a downgrade.
  220. if build.IsDarwin {
  221. return []string{
  222. fmt.Sprintf("syncthing-macos-%s-%s.", runtime.GOARCH, tag),
  223. fmt.Sprintf("syncthing-macosx-%s-%s.", runtime.GOARCH, tag),
  224. }
  225. }
  226. return []string{
  227. fmt.Sprintf("syncthing-%s-%s-%s.", runtime.GOOS, runtime.GOARCH, tag),
  228. }
  229. }