-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter.go
More file actions
75 lines (67 loc) · 2.43 KB
/
writer.go
File metadata and controls
75 lines (67 loc) · 2.43 KB
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
package imo
import (
"context"
"fmt"
"github.com/containers/image/v5/types"
)
// Writer provides a tool to copy only the layers that are not already
// present in a different version of the same image.
type Writer struct {
types.ImageReference
dest *destwrap
}
// NewImageDestination returns a handler used to write.
func (i *Writer) NewImageDestination(ctx context.Context, sys *types.SystemContext) (types.ImageDestination, error) {
return i.dest, nil
}
// destwrap wraps an image destination (this can be another registry or a
// file on disk) and the original manifest from where we can extract the
// layers that are already present.
type destwrap struct {
types.ImageDestination
baseimage *ManifestsIndex
}
// TryReusingBlob is called by the image copy code to check if a layer is
// already present in the destination. If it is, we return true and the
// layer info. If it is not, we return false and the layer info. We use the
// manifest to check if the layer is already present.
func (d *destwrap) TryReusingBlob(ctx context.Context, info types.BlobInfo, cache types.BlobInfoCache, substitute bool) (bool, types.BlobInfo, error) {
if d.baseimage.HasLayer(info.Digest) {
return true, info, nil
}
return false, info, nil
}
// NewWriterFromScratch uses the "scratch" image as base and stores the
// result in 'to'. This is useful to create a new image from scratch.
func NewWriterFromScratch(ctx context.Context, to types.ImageReference, sysctx *types.SystemContext) (*Writer, error) {
toref, err := to.NewImageDestination(ctx, sysctx)
if err != nil {
return nil, fmt.Errorf("error creating destination: %w", err)
}
return &Writer{
ImageReference: to,
dest: &destwrap{
ImageDestination: toref,
baseimage: NewManifestsIndex(sysctx),
},
}, nil
}
// NewWriter is capable of providing an incremental copy of an image using
// 'from' as base and storing the result in 'to'.
func NewWriter(ctx context.Context, from types.ImageReference, to types.ImageReference, sysctx *types.SystemContext) (*Writer, error) {
toref, err := to.NewImageDestination(ctx, sysctx)
if err != nil {
return nil, fmt.Errorf("error creating destination: %w", err)
}
baseimage := NewManifestsIndex(sysctx)
if err := baseimage.FetchManifests(ctx, from); err != nil {
return nil, fmt.Errorf("error fetching manifests: %w", err)
}
return &Writer{
ImageReference: to,
dest: &destwrap{
ImageDestination: toref,
baseimage: baseimage,
},
}, nil
}