forked from cloudfoundry/bosh-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarball_compressor.go
More file actions
96 lines (77 loc) · 2.55 KB
/
tarball_compressor.go
File metadata and controls
96 lines (77 loc) · 2.55 KB
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
package fileutil
import (
"fmt"
"runtime"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type tarballCompressor struct {
cmdRunner boshsys.CmdRunner
fs boshsys.FileSystem
}
func NewTarballCompressor(
cmdRunner boshsys.CmdRunner,
fs boshsys.FileSystem,
) Compressor {
return tarballCompressor{cmdRunner: cmdRunner, fs: fs}
}
func (c tarballCompressor) CompressFilesInDir(dir string, options CompressorOptions) (string, error) {
return c.CompressSpecificFilesInDir(dir, []string{"."}, options)
}
func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string, options CompressorOptions) (string, error) {
tarball, err := c.fs.TempFile("bosh-platform-disk-TarballCompressor-CompressSpecificFilesInDir")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file for tarball")
}
defer tarball.Close()
tarballPath := tarball.Name()
args := []string{"-cf", tarballPath, "-C", dir}
if !options.NoCompression {
args = append(args, "-z")
}
if runtime.GOOS == "darwin" {
args = append([]string{"--no-mac-metadata"}, args...)
}
for _, file := range files { //nolint:gosimple
args = append(args, file)
}
_, _, _, err = c.cmdRunner.RunCommand("tar", args...)
if err != nil {
return "", bosherr.WrapError(err, "Shelling out to tar")
}
return tarballPath, nil
}
func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, options CompressorOptions) error {
sameOwnerOption := "--no-same-owner"
if options.SameOwner {
sameOwnerOption = "--same-owner"
}
resolvedTarballPath, err := c.fs.ReadAndFollowLink(tarballPath)
if err != nil {
return bosherr.WrapError(err, "Resolving tarball path")
}
args := []string{sameOwnerOption, "-xf", resolvedTarballPath, "-C", dir}
if options.StripComponents != 0 {
args = append(args, fmt.Sprintf("--strip-components=%d", options.StripComponents))
}
if options.PathInArchive != "" {
args = append(args, options.PathInArchive)
}
_, _, _, err = c.cmdRunner.RunCommand("tar", args...)
if err != nil {
return bosherr.WrapError(err, "Shelling out to tar")
}
return nil
}
func (c tarballCompressor) IsNonCompressedTarball(path string) (bool, error) {
stdout, _, exitStatus, err := c.cmdRunner.RunCommand("file", path)
if err != nil || exitStatus != 0 {
return false, err
}
fileOutputStr := strings.TrimSpace(stdout)
return strings.Contains(fileOutputStr, "POSIX tar archive"), nil
}
func (c tarballCompressor) CleanUp(tarballPath string) error {
return c.fs.RemoveAll(tarballPath)
}