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