Skip to content

Commit

Permalink
Don't download archives that have been extracted before (#198)
Browse files Browse the repository at this point in the history
Don't download archives that have been extracted before.
  • Loading branch information
marcopeereboom authored Nov 9, 2020
1 parent e9402b3 commit c592e15
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
26 changes: 18 additions & 8 deletions cmd/dcrinstall/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
}

bitcoinVersionRE = regexp.MustCompile(`[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+`)
bitcoinArchiveRE = regexp.MustCompile(`bitcoin-[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+`)

bitcoinf = []decredFiles{
{
Expand Down Expand Up @@ -289,15 +290,24 @@ func bitcoinDownloadAndVerify() error {
log.Printf("Attempting to upgrade to Bitcoin version: %v",
manifestBitcoinVersion)

// Download bitcoin bundle
err = downloadBitcoinBundle(digest, filename)
if err != nil {
return fmt.Errorf("Download bitcoin bundle: %v", err)
}
// Work around bitcoin not having a default name.
filenameMunged := bitcoinArchiveRE.FindString(filename) + ".tar.gz"

err = extractBitcoinBundle()
if err != nil {
return fmt.Errorf("Extract bitcoin bundle: %v", err)
// Don't download bundle if it has been extracted.
if filenameMunged == ".tar.gz" ||
forceDownload || !seenBefore(filenameMunged) {
// Download bitcoin bundle
err = downloadBitcoinBundle(digest, filename)
if err != nil {
return fmt.Errorf("Download bitcoin bundle: %v", err)
}

err = extractBitcoinBundle()
if err != nil {
return fmt.Errorf("Extract bitcoin bundle: %v", err)
}
} else {
log.Printf("Using cached archive: %v", filename)
}

err = preconditionsBitcoinInstall()
Expand Down
21 changes: 13 additions & 8 deletions cmd/dcrinstall/dcrdex.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,20 @@ func dcrdexDownloadAndVerify() error {
log.Printf("Attempting to upgrade to Dcrdex version: %v",
manifestDcrdexVersion)

// Download dcrdex bundle
err = downloadDcrdexBundle(digest, filename)
if err != nil {
return fmt.Errorf("Download dcrdex bundle: %v", err)
}
// Don't download bundle if it has been extracted.
if forceDownload || !seenBefore(filename) {
// Download dcrdex bundle
err = downloadDcrdexBundle(digest, filename)
if err != nil {
return fmt.Errorf("Download dcrdex bundle: %v", err)
}

err = extractDcrdexBundle()
if err != nil {
return fmt.Errorf("Extract dcrdex bundle: %v", err)
err = extractDcrdexBundle()
if err != nil {
return fmt.Errorf("Extract dcrdex bundle: %v", err)
}
} else {
log.Printf("Using cached archive: %v", filename)
}

err = preconditionsDcrdexInstall()
Expand Down
4 changes: 4 additions & 0 deletions cmd/dcrinstall/dcrinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ var (
bitcoinManifestDigest string // Bitcoin manifest digest, if used
tuple string // Download tuple
network string // Installing for network
forceDownload bool // Always download bundles
dcrdex bool // Install dcrdex
skipPGP bool // Don't download and verify PGP signatures
quiet bool // Don't output anything but errors
Expand Down Expand Up @@ -303,6 +304,8 @@ func _main() error {
"bitcoin manifest URI override")
tupleF := flag.String("tuple", defaultTuple,
"OS-Arch tuple, e.g. windows-amd64")
forceDownloadF := flag.Bool("forcedownload", false,
"Force download bundles (default false)")
dcrdexF := flag.Bool("dcrdex", false, "Install Dcrdex")
skipPGPF := flag.Bool("skippgp", false, "skip download and "+
"verification of pgp signatures")
Expand All @@ -324,6 +327,7 @@ func _main() error {
// Prepare environment
destination = cleanAndExpandPath(*destF)
tuple = *tupleF
forceDownload = *forceDownloadF
dcrdex = *dcrdexF
skipPGP = *skipPGPF
quiet = *quietF
Expand Down
22 changes: 14 additions & 8 deletions cmd/dcrinstall/decred.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func decredDownloadAndVerify() error {
// Download the decred manifest
manifestDecredFilename = filepath.Join(tmpDir,
filepath.Base(decredManifestURI))

err := DownloadFile(decredManifestURI, manifestDecredFilename)
if err != nil {
return fmt.Errorf("Download manifest file: %v", err)
Expand Down Expand Up @@ -365,15 +366,20 @@ func decredDownloadAndVerify() error {
log.Printf("Attempting to upgrade to Decred version: %v",
manifestDecredVersion)

// Download decred bundle
err = downloadDecredBundle(digest, filename)
if err != nil {
return fmt.Errorf("Download decred bundle: %v", err)
}
// Don't download bundle if it has been extracted.
if forceDownload || !seenBefore(filename) {
// Download decred bundle
err = downloadDecredBundle(digest, filename)
if err != nil {
return fmt.Errorf("Download decred bundle: %v", err)
}

err = extractDecredBundle()
if err != nil {
return fmt.Errorf("Extract decred bundle: %v", err)
err = extractDecredBundle()
if err != nil {
return fmt.Errorf("Extract decred bundle: %v", err)
}
} else {
log.Printf("Using cached archive: %v", filename)
}

err = preconditionsDecredInstall()
Expand Down
25 changes: 21 additions & 4 deletions cmd/dcrinstall/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ func (wc WriteCounter) PrintProgress() {

// DownloadFile downloads the provided URL to the filepath. If the quiet flag
// is not set it prints download progress.
func DownloadFile(url string, filepath string) error {
log.Printf("Download file: %v -> %v", url, filepath)
func DownloadFile(url string, path string) error {
log.Printf("Download file: %v -> %v", url, path)

// Create the file with .tmp extension, so that we won't overwrite a
// file until it's downloaded fully
out, err := os.Create(filepath + ".tmp")
out, err := os.Create(path + ".tmp")
if err != nil {
return err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func DownloadFile(url string, filepath string) error {
out.Close()

// Rename the tmp file back to the original file
err = os.Rename(filepath+".tmp", filepath)
err = os.Rename(path+".tmp", path)
if err != nil {
return err
}
Expand Down Expand Up @@ -474,6 +474,9 @@ func getDownloadURI(uri string) (string, error) {
func runtimeTuple() string {
return runtime.GOOS + "-" + runtime.GOARCH
}

// printConfigError prints a list of installed and not installed configuration
// files.
func printConfigError(installedConfigs, notInstalledConfigs []string) string {
rv := "Installed configuration files:\n"
for _, v := range installedConfigs {
Expand All @@ -485,3 +488,17 @@ func printConfigError(installedConfigs, notInstalledConfigs []string) string {
}
return rv
}

// seenBefore looks to see if bundle has been extracted before.
func seenBefore(bundle string) bool {
b := filepath.Base(bundle)
switch {
case strings.HasSuffix(bundle, ".zip"):
return exists((filepath.Join(destination,
strings.TrimRight(b, ".zip"))))
case strings.HasSuffix(bundle, ".tar.gz"):
return exists((filepath.Join(destination,
strings.TrimRight(b, ".tar.gz"))))
}
return false
}

0 comments on commit c592e15

Please sign in to comment.