Skip to content

Commit 19697a2

Browse files
committed
image: Add ImageLayoutVersion and check oci-layout in tar engines
Collect the shared stuff in the image/layout utility package. Signed-off-by: W. Trevor King <[email protected]>
1 parent 2d8f38f commit 19697a2

File tree

5 files changed

+112
-4
lines changed

5 files changed

+112
-4
lines changed

image/cas/layout/tar.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/opencontainers/image-spec/image/cas"
26+
"github.com/opencontainers/image-spec/image/layout"
2627
)
2728

2829
// TarEngine is a cas.Engine backed by a tar file.
@@ -31,10 +32,16 @@ type TarEngine struct {
3132
}
3233

3334
// GetTarEngine returns a TarEngine.
34-
func GetTarEngine(file ReadSeekCloser) (engine cas.Engine, err error) {
35-
engine = &TarEngine{
35+
func GetTarEngine(file ReadSeekCloser) (eng cas.Engine, err error) {
36+
engine := &TarEngine{
3637
reader: file,
3738
}
39+
40+
err = layout.CheckVersion(engine.reader)
41+
if err != nil {
42+
return nil, err
43+
}
44+
3845
return engine, nil
3946
}
4047

image/layout/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 defines utility code shared by refs/layout and cas/layout.
16+
package layout

image/layout/tar.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

image/refs/layout/tar.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
casLayout "github.com/opencontainers/image-spec/image/cas/layout"
27+
imageLayout "github.com/opencontainers/image-spec/image/layout"
2728
"github.com/opencontainers/image-spec/image/refs"
2829
"github.com/opencontainers/image-spec/image/structure"
2930
)
@@ -34,11 +35,16 @@ type TarEngine struct {
3435
}
3536

3637
// GetTarEngine returns a TarEngine.
37-
func GetTarEngine(file casLayout.ReadSeekCloser) (engine refs.Engine, err error) {
38-
engine = &TarEngine{
38+
func GetTarEngine(file casLayout.ReadSeekCloser) (eng refs.Engine, err error) {
39+
engine := &TarEngine{
3940
reader: file,
4041
}
4142

43+
err = imageLayout.CheckVersion(engine.reader)
44+
if err != nil {
45+
return nil, err
46+
}
47+
4248
return engine, nil
4349
}
4450

image/structure/image_layout.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 structure
16+
17+
// ImageLayoutVersion represents the oci-version content for the image
18+
// layout format.
19+
type ImageLayoutVersion struct {
20+
Version string `json:"imageLayoutVersion"`
21+
}

0 commit comments

Comments
 (0)