func()

in gcs-fetcher/pkg/fetcher/fetcher.go [700:762]


func (gf *Fetcher) fetchFromZip(ctx context.Context) (err error) {
	started := time.Now()
	gf.log("Fetching archive %s.", formatGCSName(gf.Bucket, gf.Object, gf.Generation))

	// Download the archive from GCS.
	zipDir := gf.StagingDir
	j := job{
		filename:        gf.Object,
		bucket:          gf.Bucket,
		object:          gf.Object,
		generation:      gf.Generation,
		destDirOverride: zipDir,
	}
	report := gf.fetchObject(ctx, j)
	if !report.success {
		if err, ok := report.err.(*permissionError); ok {
			gf.logErr(err.Error())
			os.Exit(permissionDeniedExitStatus)
		}
		return fmt.Errorf("failed to download archive %s: %v", formatGCSName(gf.Bucket, gf.Object, gf.Generation), report.err)
	}

	// Unzip into the destination directory
	zipfile := filepath.Join(zipDir, gf.Object)
	unzipStart := time.Now()
	numFiles, err := unzip(zipfile, gf.DestDir)
	if err != nil {
		return err
	}
	unzipDuration := time.Since(unzipStart)

	if !gf.KeepSource {
		// Remove the zip file (best effort only, no harm if this fails).
		if err := os.RemoveAll(zipfile); err != nil {
			gf.log("Failed to remove zipfile %s, continuing: %v", zipfile, err)
		}

		// Final cleanup of staging directory, which is only a temporary staging
		// location for downloading the zipfile in this case.
		if err := gf.OS.RemoveAll(gf.StagingDir); err != nil {
			gf.log("Failed to remove staging dir %q, continuing: %v", gf.StagingDir, err)
		}
	}

	mib := float64(report.size) / 1024 / 1024
	var mibps float64
	zipfileDuration := report.attempts[len(report.attempts)-1].duration
	if zipfileDuration > 0 {
		mibps = mib / zipfileDuration.Seconds()
	}
	gf.log("******************************************************")
	gf.log("Status:                      SUCCESS")
	gf.log("Started:                     %s", started.Format(time.RFC3339))
	gf.log("Completed:                   %s", time.Now().Format(time.RFC3339))
	gf.log("Total files:       %6d", numFiles)
	gf.log("MiB downloaded:    %9.2f MiB", mib)
	gf.log("MiB/s throughput:  %9.2f MiB/s", mibps)
	gf.log("Time for zipfile:  %9.2f s", zipfileDuration.Seconds())
	gf.log("Time to unzip:     %9.2f s", unzipDuration.Seconds())
	gf.log("Total time:        %9.2f s", time.Since(started).Seconds())
	gf.log("******************************************************")
	return nil
}