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