forked from containerd/containerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
65 lines (60 loc) · 1.79 KB
/
export.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
package containerd
import (
"archive/tar"
"context"
"io"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/oci"
ocispecs "github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
func (c *Client) exportToOCITar(ctx context.Context, desc ocispec.Descriptor, writer io.Writer, eopts exportOpts) error {
tw := tar.NewWriter(writer)
img := oci.Tar(tw)
// For tar, we defer creating index until end of the function.
if err := oci.Init(img, oci.InitOpts{SkipCreateIndex: true}); err != nil {
return err
}
cs := c.ContentStore()
handlers := images.Handlers(
images.ChildrenHandler(cs),
exportHandler(cs, img),
)
// For tar, we need to use Walk instead of Dispatch for ensuring sequential write
if err := images.Walk(ctx, handlers, desc); err != nil {
return err
}
// For tar, we don't use oci.PutManifestDescriptorToIndex() which allows appending desc to existing index.json
// but requires img to support random read access so as to read index.json.
return oci.WriteIndex(img,
ocispec.Index{
Versioned: ocispecs.Versioned{
SchemaVersion: 2,
},
Manifests: []ocispec.Descriptor{desc},
},
)
}
func exportHandler(cs content.Store, img oci.ImageDriver) images.HandlerFunc {
return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
r, err := cs.Reader(ctx, desc.Digest)
if err != nil {
return nil, err
}
w, err := oci.NewBlobWriter(img, desc.Digest.Algorithm())
if err != nil {
return nil, err
}
if _, err = io.Copy(w, r); err != nil {
return nil, err
}
if err = w.Commit(desc.Size, desc.Digest); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return nil, err
}
return nil, nil
}
}