-
Notifications
You must be signed in to change notification settings - Fork 20
/
extract.go
57 lines (52 loc) · 1.63 KB
/
extract.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package utils
import (
"archive/tar"
"errors"
"fmt"
"io"
"os"
"github.com/mandelsoft/vfs/pkg/vfs"
)
// ExtractTarToFs writes a tar stream to a filesystem.
func ExtractTarToFs(fs vfs.FileSystem, in io.Reader) error {
tr := tar.NewReader(in)
for {
header, err := tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
switch header.Typeflag {
case tar.TypeDir:
if err := fs.MkdirAll(header.Name, vfs.FileMode(header.Mode)); err != nil {
return fmt.Errorf("unable to create directory %s: %w", header.Name, err)
}
case tar.TypeReg:
dir := vfs.Dir(fs, header.Name)
if err := fs.MkdirAll(dir, 0o766); err != nil {
return fmt.Errorf("unable to create directory %s: %w", dir, err)
}
file, err := fs.OpenFile(header.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, vfs.FileMode(header.Mode))
if err != nil {
return fmt.Errorf("unable to open file %s: %w", header.Name, err)
}
//nolint:gosec // We don't know what size limit we could set, the tar
// archive can be an image layer and that can even reach the gigabyte range.
// For now, we acknowledge the risk.
//
// We checked other softwares and tried to figure out how they manage this,
// but it's handled the same way.
if _, err := io.Copy(file, tr); err != nil {
return fmt.Errorf("unable to copy tar file to filesystem: %w", err)
}
if err := file.Close(); err != nil {
return fmt.Errorf("unable to close file %s: %w", header.Name, err)
}
}
}
}