Debian Codegraph @

image/index.go

raw symbols SCIP
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
26 "github.com/opencontainers/image-spec/schema"
27 "github.com/opencontainers/image-spec/specs-go/v1"
28 "github.com/pkg/errors"
29 )
30
31 func findIndex(w walker, d *v1.Descriptor) (*v1.Index, error) {
32 var index v1.Index
33 ipath := filepath.Join("blobs", string(d.Digest.Algorithm()), d.Digest.Hex())
34
35 switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
36 if info.IsDir() || filepath.Clean(path) != ipath {
37 return nil
38 }
39
40 buf, err := ioutil.ReadAll(r)
41 if err != nil {
42 return errors.Wrapf(err, "%s: error reading index", path)
43 }
44
45 if err := schema.ValidatorMediaTypeImageIndex.Validate(bytes.NewReader(buf)); err != nil {
46 return errors.Wrapf(err, "%s: index validation failed", path)
47 }
48
49 if err := json.Unmarshal(buf, &index); err != nil {
50 return err
51 }
52
53 return errEOW
54 }); err {
55 case errEOW:
56 return &index, nil
57 case nil:
58 return nil, fmt.Errorf("index not found")
59 default:
60 return nil, err
61 }
62 }
63
64 func validateIndex(index *v1.Index, w walker) error {
65 for _, manifest := range index.Manifests {
66 if err := validateDescriptor(&manifest, w, []string{v1.MediaTypeImageManifest}); err != nil {
67 return errors.Wrap(err, "manifest validation failed")
68 }
69 }
70 return nil
71 }