gosrc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package build
  17. import (
  18. "bytes"
  19. "crypto/sha256"
  20. "fmt"
  21. "io/ioutil"
  22. "net/http"
  23. "os"
  24. "path/filepath"
  25. "strings"
  26. )
  27. // EnsureGoSources ensures that path contains a file with the given SHA256 hash,
  28. // and if not, it downloads a fresh Go source package from upstream and replaces
  29. // path with it (if the hash matches).
  30. func EnsureGoSources(version string, hash []byte, path string) error {
  31. // Sanity check the destination path to ensure we don't do weird things
  32. if !strings.HasSuffix(path, ".tar.gz") {
  33. return fmt.Errorf("destination path (%s) must end with .tar.gz", path)
  34. }
  35. // If the file exists, validate it's hash
  36. if archive, err := ioutil.ReadFile(path); err == nil { // Go sources are ~20MB, it's fine to read all
  37. hasher := sha256.New()
  38. hasher.Write(archive)
  39. have := hasher.Sum(nil)
  40. if bytes.Equal(have, hash) {
  41. fmt.Printf("Go %s [%x] available at %s\n", version, hash, path)
  42. return nil
  43. }
  44. fmt.Printf("Go %s hash mismatch (have %x, want %x) at %s, deleting old archive\n", version, have, hash, path)
  45. if err := os.Remove(path); err != nil {
  46. return err
  47. }
  48. }
  49. // Archive missing or bad hash, download a new one
  50. fmt.Printf("Downloading Go %s [want %x] into %s\n", version, hash, path)
  51. res, err := http.Get(fmt.Sprintf("https://dl.google.com/go/go%s.src.tar.gz", version))
  52. if err != nil || res.StatusCode != http.StatusOK {
  53. return fmt.Errorf("failed to access Go sources: code %d, err %v", res.StatusCode, err)
  54. }
  55. defer res.Body.Close()
  56. archive, err := ioutil.ReadAll(res.Body)
  57. if err != nil {
  58. return err
  59. }
  60. // Sanity check the downloaded archive, save if checks out
  61. hasher := sha256.New()
  62. hasher.Write(archive)
  63. if have := hasher.Sum(nil); !bytes.Equal(have, hash) {
  64. return fmt.Errorf("downloaded Go %s hash mismatch (have %x, want %x)", version, have, hash)
  65. }
  66. if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
  67. return err
  68. }
  69. if err := ioutil.WriteFile(path, archive, 0644); err != nil {
  70. return err
  71. }
  72. fmt.Printf("Downloaded Go %s [%x] into %s\n", version, hash, path)
  73. return nil
  74. }