summaryrefslogtreecommitdiffstats
path: root/utils/extract.go
blob: 0559c6ce8e392aa33d0bc936e33f17695ca30709 (plain)
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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"archive/tar"
	"compress/gzip"
	"fmt"
	"io"
	"os"
	"path/filepath"
)

// ExtractTarGz takes in an io.Reader containing the bytes for a .tar.gz file and
// a destination string to extract to. A list of the file and directory names that
// were extracted is returned.
func ExtractTarGz(gzipStream io.Reader, dst string) ([]string, error) {
	uncompressedStream, err := gzip.NewReader(gzipStream)
	if err != nil {
		return nil, fmt.Errorf("ExtractTarGz: NewReader failed: %s", err.Error())
	}
	defer uncompressedStream.Close()

	tarReader := tar.NewReader(uncompressedStream)

	filenames := []string{}

	for true {
		header, err := tarReader.Next()

		if err == io.EOF {
			break
		}

		if err != nil {
			return nil, fmt.Errorf("ExtractTarGz: Next() failed: %s", err.Error())
		}

		switch header.Typeflag {
		case tar.TypeDir:
			if PathTraversesUpward(header.Name) {
				return nil, fmt.Errorf("ExtractTarGz: path attempts to traverse upwards")
			}

			path := filepath.Join(dst, header.Name)
			if err := os.Mkdir(path, 0744); err != nil && !os.IsExist(err) {
				return nil, fmt.Errorf("ExtractTarGz: Mkdir() failed: %s", err.Error())
			}

			filenames = append(filenames, header.Name)
		case tar.TypeReg:
			if PathTraversesUpward(header.Name) {
				return nil, fmt.Errorf("ExtractTarGz: path attempts to traverse upwards")
			}

			path := filepath.Join(dst, header.Name)
			dir := filepath.Dir(path)

			if err := os.MkdirAll(dir, 0744); err != nil {
				return nil, fmt.Errorf("ExtractTarGz: MkdirAll() failed: %s", err.Error())
			}

			outFile, err := os.Create(path)
			if err != nil {
				return nil, fmt.Errorf("ExtractTarGz: Create() failed: %s", err.Error())
			}
			defer outFile.Close()
			if _, err := io.Copy(outFile, tarReader); err != nil {
				return nil, fmt.Errorf("ExtractTarGz: Copy() failed: %s", err.Error())
			}

			filenames = append(filenames, header.Name)
		default:
			return nil, fmt.Errorf(
				"ExtractTarGz: unknown type: %v in %v",
				header.Typeflag,
				header.Name)
		}
	}

	return filenames, nil
}