distribution/pull_v2_windows.go
694df3ff
 // +build windows
 
 package distribution
 
 import (
38aef56e
 	"fmt"
05bd0435
 	"net/http"
 	"os"
38aef56e
 	"runtime"
 	"sort"
 	"strings"
694df3ff
 
05bd0435
 	"github.com/docker/distribution"
 	"github.com/docker/distribution/context"
38aef56e
 	"github.com/docker/distribution/manifest/manifestlist"
2c60430a
 	"github.com/docker/distribution/manifest/schema2"
05bd0435
 	"github.com/docker/distribution/registry/client/transport"
38aef56e
 	"github.com/docker/docker/pkg/system"
1009e6a4
 	"github.com/sirupsen/logrus"
694df3ff
 )
 
2c60430a
 var _ distribution.Describable = &v2LayerDescriptor{}
05bd0435
 
2c60430a
 func (ld *v2LayerDescriptor) Descriptor() distribution.Descriptor {
 	if ld.src.MediaType == schema2.MediaTypeForeignLayer && len(ld.src.URLs) > 0 {
 		return ld.src
 	}
 	return distribution.Descriptor{}
05bd0435
 }
 
 func (ld *v2LayerDescriptor) open(ctx context.Context) (distribution.ReadSeekCloser, error) {
67fdf574
 	blobs := ld.repo.Blobs(ctx)
 	rsc, err := blobs.Open(ctx, ld.digest)
 
2c60430a
 	if len(ld.src.URLs) == 0 {
67fdf574
 		return rsc, err
05bd0435
 	}
 
67fdf574
 	// We're done if the registry has this blob.
 	if err == nil {
 		// Seek does an HTTP GET.  If it succeeds, the blob really is accessible.
 		if _, err = rsc.Seek(0, os.SEEK_SET); err == nil {
 			return rsc, nil
 		}
 		rsc.Close()
 	}
05bd0435
 
 	// Find the first URL that results in a 200 result code.
2c60430a
 	for _, url := range ld.src.URLs {
6b7d0280
 		logrus.Debugf("Pulling %v from foreign URL %v", ld.digest, url)
05bd0435
 		rsc = transport.NewHTTPReadSeeker(http.DefaultClient, url, nil)
67fdf574
 
 		// Seek does an HTTP GET.  If it succeeds, the blob really is accessible.
05bd0435
 		_, err = rsc.Seek(0, os.SEEK_SET)
 		if err == nil {
 			break
 		}
6b7d0280
 		logrus.Debugf("Download for %v failed: %v", ld.digest, err)
05bd0435
 		rsc.Close()
 		rsc = nil
 	}
 	return rsc, err
 }
38aef56e
 
 func filterManifests(manifests []manifestlist.ManifestDescriptor) []manifestlist.ManifestDescriptor {
 	version := system.GetOSVersion()
 
 	// TODO @jhowardmsft LCOW Support: Need to remove the hard coding in LCOW mode.
 	lookingForOS := runtime.GOOS
 	osVersion := fmt.Sprintf("%d.%d.%d", version.MajorVersion, version.MinorVersion, version.Build)
 	if system.LCOWSupported() {
 		lookingForOS = "linux"
 		osVersion = ""
 	}
 
 	var matches []manifestlist.ManifestDescriptor
 	for _, manifestDescriptor := range manifests {
 		if manifestDescriptor.Platform.Architecture == runtime.GOARCH && manifestDescriptor.Platform.OS == lookingForOS {
0380fbff
 			if lookingForOS == "windows" && !versionMatch(manifestDescriptor.Platform.OSVersion, osVersion) {
38aef56e
 				continue
 			}
 			matches = append(matches, manifestDescriptor)
 
0380fbff
 			logrus.Debugf("found match for %s/%s with media type %s, digest %s", lookingForOS, runtime.GOARCH, manifestDescriptor.MediaType, manifestDescriptor.Digest.String())
38aef56e
 		}
 	}
 	sort.Stable(manifestsByVersion(matches))
 	return matches
 }
 
 func versionMatch(actual, expected string) bool {
 	// Check whether actual and expected are equivalent, or whether
 	// expected is a version prefix of actual.
 	return actual == "" || expected == "" || actual == expected || strings.HasPrefix(actual, expected+".")
 }
 
 type manifestsByVersion []manifestlist.ManifestDescriptor
 
 func (mbv manifestsByVersion) Less(i, j int) bool {
 	if mbv[i].Platform.OSVersion == "" {
 		return false
 	}
 	if mbv[j].Platform.OSVersion == "" {
 		return true
 	}
 	// TODO: Split version by parts and compare
 	// TODO: Prefer versions which have a greater version number
 	return false
 }
 
 func (mbv manifestsByVersion) Len() int {
 	return len(mbv)
 }
 
 func (mbv manifestsByVersion) Swap(i, j int) {
 	mbv[i], mbv[j] = mbv[j], mbv[i]
 }