Path: blob/main/components/ws-daemon/pkg/content/archive.go
2500 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package content56import (7"context"8"io"9"os"1011"github.com/containers/storage/pkg/archive"12"github.com/containers/storage/pkg/idtools"13"github.com/opentracing/opentracing-go"14"golang.org/x/xerrors"1516"github.com/gitpod-io/gitpod/common-go/tracing"17carchive "github.com/gitpod-io/gitpod/content-service/pkg/archive"18)1920// BuildTarbal creates an OCI compatible tar file dst from the folder src, expecting the overlay whiteout format21func BuildTarbal(ctx context.Context, src string, dst string, opts ...carchive.TarOption) (err error) {22var cfg carchive.TarConfig23for _, opt := range opts {24opt(&cfg)25}2627//nolint:staticcheck,ineffassign28span, ctx := opentracing.StartSpanFromContext(ctx, "buildTarbal")29span.LogKV("src", src, "dst", dst)30defer tracing.FinishSpan(span, &err)3132// ensure the src actually exists before trying to tar it33if _, err := os.Stat(src); err != nil {34return xerrors.Errorf("Unable to tar files: %v", err.Error())35}3637uidMaps := make([]idtools.IDMap, len(cfg.UIDMaps))38for i, m := range cfg.UIDMaps {39uidMaps[i] = idtools.IDMap{40ContainerID: m.ContainerID,41HostID: m.HostID,42Size: m.Size,43}44}45gidMaps := make([]idtools.IDMap, len(cfg.GIDMaps))46for i, m := range cfg.GIDMaps {47gidMaps[i] = idtools.IDMap{48ContainerID: m.ContainerID,49HostID: m.HostID,50Size: m.Size,51}52}5354tarReader, err := archive.TarWithOptions(src, &archive.TarOptions{55UIDMaps: uidMaps,56GIDMaps: gidMaps,57Compression: archive.Uncompressed,58CopyPass: true,59})60if err != nil {61return62}63defer tarReader.Close()6465tarFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0755)66if err != nil {67return xerrors.Errorf("Unable to create tar file: %v", err.Error())68}6970_, err = io.Copy(tarFile, tarReader)71if err != nil {72return xerrors.Errorf("Unable create tar file: %v", err.Error())73}7475return76}777879