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
The new engine code isn't very DRY, but I haven't decided where to
collect this sort of shared stuff yet.  And it's not very long, so
it's not too terrible to leave it WET.

Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Jun 17, 2016
1 parent e0810de commit 7efffe9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
37 changes: 34 additions & 3 deletions image/cas/layout/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ package layout

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

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

// TarEngine is a cas.Engine backed by a tar file.
Expand All @@ -31,11 +33,40 @@ type TarEngine struct {
}

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

_, err = engine.reader.Seek(0, os.SEEK_SET)
if err != nil {
return nil, err
}

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

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

return engine, nil
}
}
}

// Put adds a new blob to the store.
Expand Down
34 changes: 31 additions & 3 deletions image/refs/layout/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,40 @@ type TarEngine struct {
}

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

return engine, nil
_, err = engine.reader.Seek(0, os.SEEK_SET)
if err != nil {
return nil, err
}

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

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

return engine, nil
}
}
}

// Put adds a new reference to the store.
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 7efffe9

Please sign in to comment.