Repository navigation
Expand file tree
/
Copy patharchive.go
More file actions
76 lines (64 loc) · 1.78 KB
/
Copy patharchive.go
File metadata and controls
76 lines (64 loc) · 1.78 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
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
)
// uncompressArchive invokes tar tool against a local archive file.
func uncompressArchive(pth string) error {
cmd := command.New("tar", "-xPf", pth)
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
errMsg := err.Error()
if errorutil.IsExitStatusError(err) {
errMsg = out
}
return fmt.Errorf("%s failed: %s", cmd.PrintableCommandArgs(), errMsg)
}
return nil
}
// extractCacheArchive invokes tar tool by piping the archive to the command's input.
func extractCacheArchive(r io.Reader) error {
cmd := command.New("tar", "-xPf", "/dev/stdin")
cmd.SetStdin(r)
if out, err := cmd.RunAndReturnTrimmedCombinedOutput(); err != nil {
errMsg := err.Error()
if errorutil.IsExitStatusError(err) {
errMsg = out
}
return fmt.Errorf("%s failed: %s", cmd.PrintableCommandArgs(), errMsg)
}
if rc, ok := r.(io.ReadCloser); ok {
return rc.Close()
}
return nil
}
// readFirstEntry reads the first entry from a given archive.
func readFirstEntry(r io.Reader) (*tar.Reader, *tar.Header, error) {
restoreReader := NewRestoreReader(r)
var archive io.Reader
var err error
log.Debugf("attempt to read archive as .gzip")
archive, err = gzip.NewReader(restoreReader)
if err != nil {
// might the archive is not compressed
log.Debugf("failed to open the archive as .gzip: %s", err)
log.Debugf("restoring reader and trying as .tar")
restoreReader.Restore()
archive = restoreReader
}
tr := tar.NewReader(archive)
hdr, err := tr.Next()
if err == io.EOF {
// no entries in the archive
return nil, nil, nil
}
if err != nil {
return nil, nil, err
}
return tr, hdr, nil
}