-
Notifications
You must be signed in to change notification settings - Fork 130
/
zstd.go
72 lines (63 loc) · 2.35 KB
/
zstd.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package zstd
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/LambdaTest/test-at-scale/pkg/core"
"github.com/LambdaTest/test-at-scale/pkg/lumber"
)
type zstdCompressor struct {
logger lumber.Logger
execManager core.ExecutionManager
execPath string
}
const (
manifestFileName = "manifest.txt"
executableName = "tar"
)
//New return zStandard compression manager
func New(execManager core.ExecutionManager, logger lumber.Logger) (core.ZstdCompressor, error) {
path, err := exec.LookPath(executableName)
if err != nil {
logger.Errorf("failed to find path for tar, error:%v", err)
return nil, err
}
return &zstdCompressor{logger: logger, execManager: execManager, execPath: path}, nil
}
func (z *zstdCompressor) createManifestFile(workingDir string, fileNames ...string) error {
return ioutil.WriteFile(filepath.Join(os.TempDir(), manifestFileName), []byte(strings.Join(fileNames, "\n")), 0660)
}
// Compress compress the list of files
func (z *zstdCompressor) Compress(ctx context.Context, compressedFileName string, preservePath bool, workingDirectory string, filesToCompress ...string) error {
if err := z.createManifestFile(workingDirectory, filesToCompress...); err != nil {
z.logger.Errorf("failed to create manifest file %v", err)
return err
}
command := fmt.Sprintf("%s --posix -I 'zstd -5 -T0' -cf %s -C %s -T %s", z.execPath, compressedFileName, workingDirectory, filepath.Join(os.TempDir(), manifestFileName))
if preservePath {
command = fmt.Sprintf("%s -P", command)
}
commands := []string{command}
if err := z.execManager.ExecuteInternalCommands(ctx, core.Zstd, commands, workingDirectory, nil, nil); err != nil {
z.logger.Errorf("error while zstd compression %v", err)
return err
}
return nil
}
//Decompress performs the decompression operation for the given file
func (z *zstdCompressor) Decompress(ctx context.Context, filePath string, preservePath bool, workingDirectory string) error {
command := fmt.Sprintf("%s --posix -I 'zstd -d' -xf %s -C %s", z.execPath, filePath, workingDirectory)
if preservePath {
command = fmt.Sprintf("%s -P", command)
}
commands := []string{command}
if err := z.execManager.ExecuteInternalCommands(ctx, core.Zstd, commands, workingDirectory, nil, nil); err != nil {
z.logger.Errorf("error while zstd decompression %v", err)
return err
}
return nil
}