|
| 1 | +// Copyright 2016 The Linux Foundation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package image |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/opencontainers/image-spec/schema" |
| 28 | + "github.com/opencontainers/image-spec/specs-go/v1" |
| 29 | + "github.com/pkg/errors" |
| 30 | +) |
| 31 | + |
| 32 | +type manifestlist struct { |
| 33 | + Manifests []ManifestDescriptor `json:"manifests"` |
| 34 | +} |
| 35 | +type ManifestDescriptor struct { |
| 36 | + Config descriptor |
| 37 | +} |
| 38 | + |
| 39 | +func findManifestList(w walker, d *descriptor) (*manifestlist, error) { |
| 40 | + var ml manifestlist |
| 41 | + mlpath := filepath.Join("blobs", d.algo(), d.hash()) |
| 42 | + |
| 43 | + switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { |
| 44 | + if info.IsDir() || filepath.Clean(path) != mlpath { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + buf, err := ioutil.ReadAll(r) |
| 49 | + if err != nil { |
| 50 | + return errors.Wrapf(err, "%s: error reading manifestlist", path) |
| 51 | + } |
| 52 | + |
| 53 | + if err := schema.MediaTypeManifestList.Validate(bytes.NewReader(buf)); err != nil { |
| 54 | + return errors.Wrapf(err, "%s: manifestlist validation failed", path) |
| 55 | + } |
| 56 | + |
| 57 | + if err := json.Unmarshal(buf, &ml); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + if len(ml.Manifests) == 0 { |
| 62 | + return fmt.Errorf("%s: no manifests found", path) |
| 63 | + } |
| 64 | + |
| 65 | + return errEOW |
| 66 | + }); err { |
| 67 | + case nil: |
| 68 | + return nil, fmt.Errorf("%s: manifestlist not found", mlpath) |
| 69 | + case errEOW: |
| 70 | + return &ml, nil |
| 71 | + default: |
| 72 | + return nil, err |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (ml *manifestlist) validate(w walker) error { |
| 77 | + for _, d := range ml.Manifests { |
| 78 | + if err := d.Config.validate(w, []string{v1.MediaTypeImageConfig}); err != nil { |
| 79 | + return errors.Wrap(err, "Descriptor validation failed") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
0 commit comments