upgrade_windows.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // +build windows,!noupgrade
  16. package upgrade
  17. import (
  18. "archive/zip"
  19. "bytes"
  20. "encoding/json"
  21. "fmt"
  22. "io"
  23. "io/ioutil"
  24. "net/http"
  25. "os"
  26. "path"
  27. "path/filepath"
  28. "runtime"
  29. "strings"
  30. )
  31. // Upgrade to the given release, saving the previous binary with a ".old" extension.
  32. func upgradeTo(path string, rel Release, archExtra string) error {
  33. expectedRelease := fmt.Sprintf("syncthing-%s-%s%s-%s.", runtime.GOOS, runtime.GOARCH, archExtra, rel.Tag)
  34. if debug {
  35. l.Debugf("expected release asset %q", expectedRelease)
  36. }
  37. for _, asset := range rel.Assets {
  38. if debug {
  39. l.Debugln("considering release", asset)
  40. }
  41. if strings.HasPrefix(asset.Name, expectedRelease) {
  42. if strings.HasSuffix(asset.Name, ".zip") {
  43. fname, err := readZip(asset.URL, filepath.Dir(path))
  44. if err != nil {
  45. return err
  46. }
  47. old := path + ".old"
  48. os.Remove(old)
  49. err = os.Rename(path, old)
  50. if err != nil {
  51. return err
  52. }
  53. err = os.Rename(fname, path)
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. }
  60. }
  61. return ErrVersionUnknown
  62. }
  63. // Returns the latest release, including prereleases or not depending on the argument
  64. func LatestRelease(prerelease bool) (Release, error) {
  65. resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=10")
  66. if err != nil {
  67. return Release{}, err
  68. }
  69. if resp.StatusCode > 299 {
  70. return Release{}, fmt.Errorf("API call returned HTTP error: %s", resp.Status)
  71. }
  72. var rels []Release
  73. json.NewDecoder(resp.Body).Decode(&rels)
  74. resp.Body.Close()
  75. if len(rels) == 0 {
  76. return Release{}, ErrVersionUnknown
  77. }
  78. if prerelease {
  79. // We are a beta version. Use the latest.
  80. return rels[0], nil
  81. } else {
  82. // We are a regular release. Only consider non-prerelease versions for upgrade.
  83. for _, rel := range rels {
  84. if !rel.Prerelease {
  85. return rel, nil
  86. }
  87. }
  88. return Release{}, ErrVersionUnknown
  89. }
  90. }
  91. func readZip(url, dir string) (string, error) {
  92. if debug {
  93. l.Debugf("loading %q", url)
  94. }
  95. req, err := http.NewRequest("GET", url, nil)
  96. if err != nil {
  97. return "", err
  98. }
  99. req.Header.Add("Accept", "application/octet-stream")
  100. resp, err := http.DefaultClient.Do(req)
  101. if err != nil {
  102. return "", err
  103. }
  104. defer resp.Body.Close()
  105. body, err := ioutil.ReadAll(resp.Body)
  106. if err != nil {
  107. return "", err
  108. }
  109. archive, err := zip.NewReader(bytes.NewReader(body), resp.ContentLength)
  110. if err != nil {
  111. return "", err
  112. }
  113. // Iterate through the files in the archive.
  114. for _, file := range archive.File {
  115. if debug {
  116. l.Debugf("considering file %q", file.Name)
  117. }
  118. if path.Base(file.Name) == "syncthing.exe" {
  119. infile, err := file.Open()
  120. if err != nil {
  121. return "", err
  122. }
  123. outfile, err := ioutil.TempFile(dir, "syncthing")
  124. if err != nil {
  125. return "", err
  126. }
  127. _, err = io.Copy(outfile, infile)
  128. if err != nil {
  129. return "", err
  130. }
  131. err = infile.Close()
  132. if err != nil {
  133. return "", err
  134. }
  135. err = outfile.Close()
  136. if err != nil {
  137. os.Remove(outfile.Name())
  138. return "", err
  139. }
  140. os.Chmod(outfile.Name(), file.Mode())
  141. return outfile.Name(), nil
  142. }
  143. }
  144. return "", fmt.Errorf("No upgrade found")
  145. }