Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-daemon/pkg/content/archive.go
2500 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package content
6
7
import (
8
"context"
9
"io"
10
"os"
11
12
"github.com/containers/storage/pkg/archive"
13
"github.com/containers/storage/pkg/idtools"
14
"github.com/opentracing/opentracing-go"
15
"golang.org/x/xerrors"
16
17
"github.com/gitpod-io/gitpod/common-go/tracing"
18
carchive "github.com/gitpod-io/gitpod/content-service/pkg/archive"
19
)
20
21
// BuildTarbal creates an OCI compatible tar file dst from the folder src, expecting the overlay whiteout format
22
func BuildTarbal(ctx context.Context, src string, dst string, opts ...carchive.TarOption) (err error) {
23
var cfg carchive.TarConfig
24
for _, opt := range opts {
25
opt(&cfg)
26
}
27
28
//nolint:staticcheck,ineffassign
29
span, ctx := opentracing.StartSpanFromContext(ctx, "buildTarbal")
30
span.LogKV("src", src, "dst", dst)
31
defer tracing.FinishSpan(span, &err)
32
33
// ensure the src actually exists before trying to tar it
34
if _, err := os.Stat(src); err != nil {
35
return xerrors.Errorf("Unable to tar files: %v", err.Error())
36
}
37
38
uidMaps := make([]idtools.IDMap, len(cfg.UIDMaps))
39
for i, m := range cfg.UIDMaps {
40
uidMaps[i] = idtools.IDMap{
41
ContainerID: m.ContainerID,
42
HostID: m.HostID,
43
Size: m.Size,
44
}
45
}
46
gidMaps := make([]idtools.IDMap, len(cfg.GIDMaps))
47
for i, m := range cfg.GIDMaps {
48
gidMaps[i] = idtools.IDMap{
49
ContainerID: m.ContainerID,
50
HostID: m.HostID,
51
Size: m.Size,
52
}
53
}
54
55
tarReader, err := archive.TarWithOptions(src, &archive.TarOptions{
56
UIDMaps: uidMaps,
57
GIDMaps: gidMaps,
58
Compression: archive.Uncompressed,
59
CopyPass: true,
60
})
61
if err != nil {
62
return
63
}
64
defer tarReader.Close()
65
66
tarFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0755)
67
if err != nil {
68
return xerrors.Errorf("Unable to create tar file: %v", err.Error())
69
}
70
71
_, err = io.Copy(tarFile, tarReader)
72
if err != nil {
73
return xerrors.Errorf("Unable create tar file: %v", err.Error())
74
}
75
76
return
77
}
78
79