forked from containerd/stargz-snapshotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
106 lines (91 loc) · 2.76 KB
/
layer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright The containerd Authors.
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.
*/
/*
Copyright 2019 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the NOTICE.md file.
*/
package layer
import (
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"io"
"io/ioutil"
"github.com/mc256/stargz-snapshotter/converter/optimizer/util"
"github.com/mc256/stargz-snapshotter/util/tempfiles"
regpkg "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/pkg/errors"
)
func NewStaticCompressedLayer(compressed io.Reader, tf *tempfiles.TempFiles) (regpkg.Layer, error) {
file, err := tf.TempFile("", "layerdata")
if err != nil {
return nil, err
}
var (
diff = sha256.New()
h = sha256.New()
)
zr, err := gzip.NewReader(io.TeeReader(compressed, io.MultiWriter(file, h)))
if err != nil {
return nil, err
}
defer zr.Close()
if _, err := io.Copy(diff, zr); err != nil {
return nil, err
}
sr, err := util.FileSectionReader(file)
if err != nil {
return nil, err
}
return StaticCompressedLayer{
R: sr,
Diff: regpkg.Hash{
Algorithm: "sha256",
Hex: hex.EncodeToString(diff.Sum(nil)),
},
Hash: regpkg.Hash{
Algorithm: "sha256",
Hex: hex.EncodeToString(h.Sum(nil)),
},
SizeVal: sr.Size(),
}, nil
}
type StaticCompressedLayer struct {
R io.Reader
Diff regpkg.Hash
Hash regpkg.Hash
SizeVal int64
}
func (l StaticCompressedLayer) Digest() (regpkg.Hash, error) {
return l.Hash, nil
}
func (l StaticCompressedLayer) Size() (int64, error) {
return l.SizeVal, nil
}
func (l StaticCompressedLayer) DiffID() (regpkg.Hash, error) {
return l.Diff, nil
}
func (l StaticCompressedLayer) MediaType() (types.MediaType, error) {
return types.DockerLayer, nil
}
func (l StaticCompressedLayer) Compressed() (io.ReadCloser, error) {
// TODO: We should pass l.closerFunc to ggcr as Close() of io.ReadCloser
// but ggcr currently doesn't call Close() so we close it manually on EOF.
// See also: https://github.com/google/go-containerregistry/pull/768
return ioutil.NopCloser(l.R), nil
}
func (l StaticCompressedLayer) Uncompressed() (io.ReadCloser, error) {
return nil, errors.New("unsupported")
}