|
| 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 layout |
| 16 | + |
| 17 | +import ( |
| 18 | + "archive/tar" |
| 19 | + "encoding/json" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "os" |
| 24 | + |
| 25 | + "github.com/opencontainers/image-spec/image/structure" |
| 26 | +) |
| 27 | + |
| 28 | +func CheckVersion(reader io.ReadSeeker) (err error) { |
| 29 | + _, err = reader.Seek(0, os.SEEK_SET) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + tarReader := tar.NewReader(reader) |
| 35 | + for { |
| 36 | + header, err := tarReader.Next() |
| 37 | + if err == io.EOF { |
| 38 | + return errors.New("oci-layout not found") |
| 39 | + } |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + if header.Name == "./oci-layout" { |
| 45 | + decoder := json.NewDecoder(tarReader) |
| 46 | + var version structure.ImageLayoutVersion |
| 47 | + err = decoder.Decode(&version) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + if version.Version != "1.0.0" { |
| 52 | + return fmt.Errorf("unrecognized imageLayoutVersion: %q", version.Version) |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments