github.com/DaoCloud/dao@v0.0.0-20161212064103-c3dbfd13ee36/distribution/pull_v2_windows.go (about) 1 // +build windows 2 3 package distribution 4 5 import ( 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "os" 10 11 "github.com/docker/distribution" 12 "github.com/docker/distribution/context" 13 "github.com/docker/distribution/manifest/schema1" 14 "github.com/docker/distribution/manifest/schema2" 15 "github.com/docker/distribution/registry/client/transport" 16 "github.com/docker/docker/image" 17 ) 18 19 func detectBaseLayer(is image.Store, m *schema1.Manifest, rootFS *image.RootFS) error { 20 v1img := &image.V1Image{} 21 if err := json.Unmarshal([]byte(m.History[len(m.History)-1].V1Compatibility), v1img); err != nil { 22 return err 23 } 24 if v1img.Parent == "" { 25 return fmt.Errorf("Last layer %q does not have a base layer reference", v1img.ID) 26 } 27 // There must be an image that already references the baselayer. 28 for _, img := range is.Map() { 29 if img.RootFS.Type == image.TypeLayersWithBase && img.RootFS.BaseLayerID() == v1img.Parent { 30 rootFS.BaseLayer = img.RootFS.BaseLayer 31 rootFS.Type = image.TypeLayersWithBase 32 return nil 33 } 34 } 35 return fmt.Errorf("Invalid base layer %q", v1img.Parent) 36 } 37 38 var _ distribution.Describable = &v2LayerDescriptor{} 39 40 func (ld *v2LayerDescriptor) Descriptor() distribution.Descriptor { 41 if ld.src.MediaType == schema2.MediaTypeForeignLayer && len(ld.src.URLs) > 0 { 42 return ld.src 43 } 44 return distribution.Descriptor{} 45 } 46 47 func (ld *v2LayerDescriptor) open(ctx context.Context) (distribution.ReadSeekCloser, error) { 48 if len(ld.src.URLs) == 0 { 49 blobs := ld.repo.Blobs(ctx) 50 return blobs.Open(ctx, ld.digest) 51 } 52 53 var ( 54 err error 55 rsc distribution.ReadSeekCloser 56 ) 57 58 // Find the first URL that results in a 200 result code. 59 for _, url := range ld.src.URLs { 60 rsc = transport.NewHTTPReadSeeker(http.DefaultClient, url, nil) 61 _, err = rsc.Seek(0, os.SEEK_SET) 62 if err == nil { 63 break 64 } 65 rsc.Close() 66 rsc = nil 67 } 68 return rsc, err 69 }