Skip to content

Commit

Permalink
image: Add ImageLayoutVersion and check oci-layout in tar engines
Browse files Browse the repository at this point in the history
Collect the shared stuff in the image/layout utility package.

Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Jun 20, 2016
1 parent d2834a6 commit 1c276b6
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
11 changes: 9 additions & 2 deletions image/cas/layout/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/opencontainers/image-spec/image/cas"
"github.com/opencontainers/image-spec/image/layout"
)

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

// NewTarEngine returns a new TarEngine.
func NewTarEngine(file ReadSeekCloser) (engine cas.Engine, err error) {
engine = &TarEngine{
func NewTarEngine(file ReadSeekCloser) (eng cas.Engine, err error) {
engine := &TarEngine{
reader: file,
}

err = layout.CheckVersion(engine.reader)
if err != nil {
return nil, err
}

return engine, nil
}

Expand Down
16 changes: 16 additions & 0 deletions image/layout/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package layout defines utility code shared by refs/layout and cas/layout.
package layout
58 changes: 58 additions & 0 deletions image/layout/tar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package layout

import (
"archive/tar"
"encoding/json"
"errors"
"fmt"
"io"
"os"

"github.com/opencontainers/image-spec/image/structure"
)

func CheckVersion(reader io.ReadSeeker) (err error) {
_, err = reader.Seek(0, os.SEEK_SET)
if err != nil {
return err
}

tarReader := tar.NewReader(reader)
for {
header, err := tarReader.Next()
if err == io.EOF {
return errors.New("oci-layout not found")
}
if err != nil {
return err
}

if header.Name == "./oci-layout" {
decoder := json.NewDecoder(tarReader)
var version structure.ImageLayoutVersion
err = decoder.Decode(&version)
if err != nil {
return err
}
if version.Version != "1.0.0" {
return fmt.Errorf("unrecognized imageLayoutVersion: %q", version.Version)
}

return nil
}
}
}
10 changes: 8 additions & 2 deletions image/refs/layout/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

casLayout "github.com/opencontainers/image-spec/image/cas/layout"
imageLayout "github.com/opencontainers/image-spec/image/layout"
"github.com/opencontainers/image-spec/image/refs"
"github.com/opencontainers/image-spec/image/structure"
)
Expand All @@ -34,11 +35,16 @@ type TarEngine struct {
}

// NewTarEngine returns a new TarEngine.
func NewTarEngine(file casLayout.ReadSeekCloser) (engine refs.Engine, err error) {
engine = &TarEngine{
func NewTarEngine(file casLayout.ReadSeekCloser) (eng refs.Engine, err error) {
engine := &TarEngine{
reader: file,
}

err = imageLayout.CheckVersion(engine.reader)
if err != nil {
return nil, err
}

return engine, nil
}

Expand Down
21 changes: 21 additions & 0 deletions image/structure/image_layout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package structure

// ImageLayoutVersion represents the oci-version content for the image
// layout format.
type ImageLayoutVersion struct {
Version string `json:"imageLayoutVersion"`
}

0 comments on commit 1c276b6

Please sign in to comment.