-
Notifications
You must be signed in to change notification settings - Fork 9
/
fromfs.go
151 lines (120 loc) · 3.56 KB
/
fromfs.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package oci
import (
"bytes"
"context"
"io"
"io/fs"
"os"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/klauspost/compress/zstd"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnfs/maketarfs"
"namespacelabs.dev/foundation/std/tasks"
)
type HasToLayer interface {
AsLayer() (v1.Layer, error)
}
func LayerFromFS(ctx context.Context, vfs fs.FS) (Layer, error) {
if asL, ok := vfs.(HasToLayer); ok {
return asL.AsLayer()
}
var buf bytes.Buffer
if err := maketarfs.TarFS(ctx, &buf, vfs, nil, nil); err != nil {
return nil, err
}
return tarball.LayerFromOpener(func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(buf.Bytes())), nil
}, tarball.WithCompressedCaching)
}
func LayerFromFile(description string, vfs fs.FS, path string) NamedLayer {
return MakeNamedLayer(description, &loadLayer{vfs: vfs, path: path})
}
type loadLayer struct {
vfs fs.FS
path string
compute.LocalScoped[Layer]
}
func (ll *loadLayer) Action() *tasks.ActionEvent {
return tasks.Action("oci.load-layer-from-fs").Arg("path", ll.path)
}
func (ll *loadLayer) Inputs() *compute.In {
return compute.Inputs().Indigestible("vfs", ll.vfs).Str("path", ll.path)
}
func (ll *loadLayer) Output() compute.Output { return compute.Output{NotCacheable: true} }
func (ll *loadLayer) Compute(ctx context.Context, _ compute.Resolved) (Layer, error) {
return tarball.LayerFromOpener(func() (io.ReadCloser, error) {
return ll.vfs.Open(ll.path)
}, tarball.WithCompressedCaching)
}
func RawZstdLayerFrom(file string, mediaType types.MediaType) (Layer, error) {
rl := rawLayer{localFile: file, mediaType: mediaType}
chash, csize, err := computeDigest(rl.Compressed)
if err != nil {
return nil, err
}
uhash, _, err := computeDigest(rl.Uncompressed)
if err != nil {
return nil, err
}
rl.cdigest = chash
rl.csize = csize
rl.udigest = uhash
return rl, nil
}
type rawLayer struct {
localFile string
cdigest v1.Hash
csize int64
udigest v1.Hash
mediaType types.MediaType
}
// Digest returns the Hash of the compressed layer.
func (r rawLayer) Digest() (v1.Hash, error) {
return r.cdigest, nil
}
// DiffID returns the Hash of the uncompressed layer.
func (r rawLayer) DiffID() (v1.Hash, error) {
return r.udigest, nil
}
// Compressed returns an io.ReadCloser for the compressed layer contents.
func (r rawLayer) Compressed() (io.ReadCloser, error) {
return os.Open(r.localFile)
}
// Uncompressed returns an io.ReadCloser for the uncompressed layer contents.
func (r rawLayer) Uncompressed() (io.ReadCloser, error) {
u, err := r.Compressed()
if err != nil {
return nil, err
}
reader, err := zstd.NewReader(u)
if err != nil {
return nil, err
}
return closeBoth{reader, u}, nil
}
type closeBoth struct {
io.Reader
source io.ReadCloser
}
func (cl closeBoth) Close() error {
return cl.source.Close()
}
// Size returns the compressed size of the Layer.
func (r rawLayer) Size() (int64, error) {
return r.csize, nil
}
// MediaType returns the media type of the Layer.
func (r rawLayer) MediaType() (types.MediaType, error) { return r.mediaType, nil }
func computeDigest(opener tarball.Opener) (v1.Hash, int64, error) {
rc, err := opener()
if err != nil {
return v1.Hash{}, 0, err
}
defer rc.Close()
return v1.SHA256(rc)
}