pull_v2_windows.go 1.8 KB

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