Debian Codegraph @

image/layout.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 "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 func layoutValidate(w walker) error {
33 var blobsExist, indexExist, layoutExist bool
34
35 if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
36 if strings.EqualFold(filepath.Base(path), "blobs") {
37 blobsExist = true
38 if !info.IsDir() {
39 return fmt.Errorf("blobs is not a directory")
40 }
41
42 return nil
43 }
44
45 if strings.EqualFold(filepath.Base(path), "index.json") {
46 indexExist = true
47 if info.IsDir() {
48 return fmt.Errorf("index.json is a directory")
49 }
50
51 buf, err := ioutil.ReadAll(r)
52 if err != nil {
53 return errors.Wrap(err, "error reading index.json")
54 }
55
56 if err := schema.ValidatorMediaTypeImageIndex.Validate(bytes.NewReader(buf)); err != nil {
57 return errors.Wrap(err, "index.json validation failed")
58 }
59
60 return nil
61 }
62
63 if strings.EqualFold(filepath.Base(path), "oci-layout") {
64 layoutExist = true
65 if info.IsDir() {
66 return fmt.Errorf("oci-layout is a directory")
67 }
68
69 var imageLayout v1.ImageLayout
70 buf, err := ioutil.ReadAll(r)
71 if err != nil {
72 return errors.Wrap(err, "error reading oci-layout")
73 }
74
75 if err := schema.ValidatorMediaTypeLayoutHeader.Validate(bytes.NewReader(buf)); err != nil {
76 return errors.Wrap(err, "oci-layout validation failed")
77 }
78
79 if err := json.Unmarshal(buf, &imageLayout); err != nil {
80 return errors.Wrap(err, "oci-layout format mismatch")
81 }
82
83 return nil
84 }
85
86 return nil
87 }); err != nil {
88 return err
89 }
90
91 if !blobsExist {
92 return fmt.Errorf("image layout must contain blobs directory")
93 }
94
95 if !indexExist {
96 return fmt.Errorf("image layout must contain index.json file")
97 }
98
99 if !layoutExist {
100 return fmt.Errorf("image layout must contain oci-layout file")
101 }
102
103 return nil
104 }