|
| 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 | + "errors" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/opencontainers/image-tools/image/cas" |
| 27 | + "golang.org/x/net/context" |
| 28 | +) |
| 29 | + |
| 30 | +// TarEngine is a cas.Engine backed by a tar file. |
| 31 | +type TarEngine struct { |
| 32 | + file ReadWriteSeekCloser |
| 33 | +} |
| 34 | + |
| 35 | +// NewTarEngine returns a new TarEngine. |
| 36 | +func NewTarEngine(file ReadWriteSeekCloser) (engine cas.Engine, err error) { |
| 37 | + engine = &TarEngine{ |
| 38 | + file: file, |
| 39 | + } |
| 40 | + |
| 41 | + return engine, nil |
| 42 | +} |
| 43 | + |
| 44 | +// Put adds a new blob to the store. |
| 45 | +func (engine *TarEngine) Put(ctx context.Context, reader io.Reader) (digest string, err error) { |
| 46 | + // FIXME |
| 47 | + return "", errors.New("TarEngine.Put is not supported yet") |
| 48 | +} |
| 49 | + |
| 50 | +// Get returns a reader for retrieving a blob from the store. |
| 51 | +func (engine *TarEngine) Get(ctx context.Context, digest string) (reader io.ReadCloser, err error) { |
| 52 | + fields := strings.SplitN(digest, ":", 2) |
| 53 | + if len(fields) != 2 { |
| 54 | + return nil, fmt.Errorf("invalid digest: %q, %v", digest, fields) |
| 55 | + } |
| 56 | + algorithm := fields[0] |
| 57 | + hash := fields[1] |
| 58 | + |
| 59 | + targetName := fmt.Sprintf("./blobs/%s/%s", algorithm, hash) |
| 60 | + |
| 61 | + _, err = engine.file.Seek(0, os.SEEK_SET) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + tarReader := tar.NewReader(engine.file) |
| 67 | + for { |
| 68 | + select { |
| 69 | + case <-ctx.Done(): |
| 70 | + return nil, ctx.Err() |
| 71 | + default: |
| 72 | + } |
| 73 | + |
| 74 | + header, err := tarReader.Next() |
| 75 | + if err == io.EOF { |
| 76 | + return nil, os.ErrNotExist |
| 77 | + } else if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + if header.Name == targetName { |
| 82 | + return ioutil.NopCloser(tarReader), nil |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Delete removes a blob from the store. |
| 88 | +func (engine *TarEngine) Delete(ctx context.Context, digest string) (err error) { |
| 89 | + // FIXME |
| 90 | + return errors.New("TarEngine.Delete is not supported yet") |
| 91 | +} |
| 92 | + |
| 93 | +// Close releases resources held by the engine. |
| 94 | +func (engine *TarEngine) Close() (err error) { |
| 95 | + return engine.file.Close() |
| 96 | +} |
0 commit comments