github.com/kapoio/go-kapoio@v1.9.7/internal/build/gosrc.go (about) 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 17 package build 18 19 import ( 20 "bytes" 21 "crypto/sha256" 22 "fmt" 23 "io/ioutil" 24 "net/http" 25 "os" 26 "path/filepath" 27 "strings" 28 ) 29 30 // EnsureGoSources ensures that path contains a file with the given SHA256 hash, 31 // and if not, it downloads a fresh Go source package from upstream and replaces 32 // path with it (if the hash matches). 33 func EnsureGoSources(version string, hash []byte, path string) error { 34 // Sanity check the destination path to ensure we don't do weird things 35 if !strings.HasSuffix(path, ".tar.gz") { 36 return fmt.Errorf("destination path (%s) must end with .tar.gz", path) 37 } 38 // If the file exists, validate it's hash 39 if archive, err := ioutil.ReadFile(path); err == nil { // Go sources are ~20MB, it's fine to read all 40 hasher := sha256.New() 41 hasher.Write(archive) 42 have := hasher.Sum(nil) 43 44 if bytes.Equal(have, hash) { 45 fmt.Printf("Go %s [%x] available at %s\n", version, hash, path) 46 return nil 47 } 48 fmt.Printf("Go %s hash mismatch (have %x, want %x) at %s, deleting old archive\n", version, have, hash, path) 49 if err := os.Remove(path); err != nil { 50 return err 51 } 52 } 53 // Archive missing or bad hash, download a new one 54 fmt.Printf("Downloading Go %s [want %x] into %s\n", version, hash, path) 55 56 res, err := http.Get(fmt.Sprintf("https://dl.google.com/go/go%s.src.tar.gz", version)) 57 if err != nil || res.StatusCode != http.StatusOK { 58 return fmt.Errorf("failed to access Go sources: code %d, err %v", res.StatusCode, err) 59 } 60 defer res.Body.Close() 61 62 archive, err := ioutil.ReadAll(res.Body) 63 if err != nil { 64 return err 65 } 66 // Sanity check the downloaded archive, save if checks out 67 hasher := sha256.New() 68 hasher.Write(archive) 69 70 if have := hasher.Sum(nil); !bytes.Equal(have, hash) { 71 return fmt.Errorf("downloaded Go %s hash mismatch (have %x, want %x)", version, have, hash) 72 } 73 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { 74 return err 75 } 76 if err := ioutil.WriteFile(path, archive, 0644); err != nil { 77 return err 78 } 79 fmt.Printf("Downloaded Go %s [%x] into %s\n", version, hash, path) 80 return nil 81 }