|
| 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 | + "crypto/sha256" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + "github.com/opencontainers/image-tools/image/cas" |
| 26 | + "github.com/opencontainers/image-tools/image/layout" |
| 27 | + "golang.org/x/net/context" |
| 28 | +) |
| 29 | + |
| 30 | +// DirEngine is a cas.Engine backed by a directory. |
| 31 | +type DirEngine struct { |
| 32 | + path string |
| 33 | + temp string |
| 34 | +} |
| 35 | + |
| 36 | +// NewDirEngine returns a new DirEngine. |
| 37 | +func NewDirEngine(ctx context.Context, path string) (eng cas.Engine, err error) { |
| 38 | + engine := &DirEngine{ |
| 39 | + path: path, |
| 40 | + } |
| 41 | + |
| 42 | + err = layout.CheckDirVersion(ctx, engine.path) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + engine.temp = filepath.Join(path, "tmp") |
| 48 | + err = os.MkdirAll(engine.temp, 0777) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + return engine, nil |
| 54 | +} |
| 55 | + |
| 56 | +// Put adds a new blob to the store. |
| 57 | +func (engine *DirEngine) Put(ctx context.Context, reader io.Reader) (digest string, err error) { |
| 58 | + hash := sha256.New() |
| 59 | + algorithm := "sha256" |
| 60 | + |
| 61 | + var file *os.File |
| 62 | + file, err = ioutil.TempFile(engine.temp, "blob-") |
| 63 | + if err != nil { |
| 64 | + return "", err |
| 65 | + } |
| 66 | + defer func() { |
| 67 | + if err != nil { |
| 68 | + os.Remove(file.Name()) |
| 69 | + } |
| 70 | + }() |
| 71 | + defer file.Close() |
| 72 | + |
| 73 | + hashingWriter := io.MultiWriter(file, hash) |
| 74 | + io.Copy(hashingWriter, reader) |
| 75 | + file.Close() |
| 76 | + |
| 77 | + digest = fmt.Sprintf("%s:%x", algorithm, hash.Sum(nil)) |
| 78 | + targetName, err := blobPath(digest, string(os.PathSeparator)) |
| 79 | + if err != nil { |
| 80 | + return "", err |
| 81 | + } |
| 82 | + |
| 83 | + path := filepath.Join(engine.path, targetName) |
| 84 | + err = os.MkdirAll(filepath.Dir(path), 0777) |
| 85 | + if err != nil { |
| 86 | + return "", err |
| 87 | + } |
| 88 | + |
| 89 | + err = os.Rename(file.Name(), path) |
| 90 | + if err != nil { |
| 91 | + return "", err |
| 92 | + } |
| 93 | + |
| 94 | + return digest, nil |
| 95 | +} |
| 96 | + |
| 97 | +// Get returns a reader for retrieving a blob from the store. |
| 98 | +func (engine *DirEngine) Get(ctx context.Context, digest string) (reader io.ReadCloser, err error) { |
| 99 | + targetName, err := blobPath(digest, string(os.PathSeparator)) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + return os.Open(filepath.Join(engine.path, targetName)) |
| 105 | +} |
| 106 | + |
| 107 | +// Delete removes a blob from the store. |
| 108 | +func (engine *DirEngine) Delete(ctx context.Context, digest string) (err error) { |
| 109 | + targetName, err := blobPath(digest, string(os.PathSeparator)) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + err = os.Remove(filepath.Join(engine.path, targetName)) |
| 115 | + if os.IsNotExist(err) { |
| 116 | + return nil |
| 117 | + } |
| 118 | + return err |
| 119 | +} |
| 120 | + |
| 121 | +// Close releases resources held by the engine. |
| 122 | +func (engine *DirEngine) Close() (err error) { |
| 123 | + return nil |
| 124 | +} |
0 commit comments