-
Notifications
You must be signed in to change notification settings - Fork 114
/
detector.go
58 lines (46 loc) · 914 Bytes
/
detector.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
package tarpath
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"strings"
)
type PrefixDetector struct{}
//go:generate counterfeiter . Detector
type Detector interface {
Detect(tgz string, path string) (bool, error)
}
func NewPrefixDetector() *PrefixDetector {
return &PrefixDetector{}
}
func (n *PrefixDetector) Detect(tgz string, path string) (bool, error) {
f, err := os.Open(tgz)
if err != nil {
return false, err
}
defer f.Close()
gr, err := gzip.NewReader(f)
if err != nil {
return false, err
}
defer gr.Close()
tr := tar.NewReader(gr)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return false, err
}
if strings.HasPrefix(header.Name, "./"+path+"/") {
return true, nil
}
if strings.HasPrefix(header.Name, path+"/") {
return false, nil
}
}
return false, fmt.Errorf("no file with prefix '%s' found", path)
}