-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfile.go
More file actions
99 lines (82 loc) · 1.73 KB
/
file.go
File metadata and controls
99 lines (82 loc) · 1.73 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
97
98
99
package tarfs
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"strings"
)
// File is a struct that represent a Reader of a tar file
// it is created by the NewFile function.
type File struct {
*tar.Reader
f *os.File
z *gzip.Reader
}
// NewFile returns a new File object, given a path to a tar file.
func NewFile(path string) (*File, error) {
f := &File{}
var err error
f.f, err = os.Open(path)
if err != nil {
return nil, err
}
// reset will set the zip and tar readers
f.reset()
return f, nil
}
// Close closes the file
func (f *File) Close() error {
if f.z != nil {
f.z.Close()
}
return f.f.Close()
}
// Open opens a file inside the tar reader. If the returned error
// is a nil, the next Read call will read the requested file inside
// the tar file.
func (f *File) Open(path string) error {
path = cleanPath(path)
// cant open "/" for reading
if path == "" {
return os.ErrInvalid
}
// reset, we need to iterate the tar index from the beginning
f.reset()
for {
h, err := f.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if cleanPath(h.Name) == path {
// if request path is a directory, we can't open it for reading
if h.FileInfo().IsDir() {
return os.ErrInvalid
}
// pointing to the right file, stopping the search
return nil
}
}
return os.ErrNotExist
}
func (f *File) reset() {
var err error
if f.z != nil {
f.z.Close()
}
f.f.Seek(0, io.SeekStart)
if f.z, err = gzip.NewReader(f.f); err == nil {
f.Reader = tar.NewReader(f.z)
} else {
// assuming that the archive is not gzipped
f.f.Seek(0, io.SeekStart)
f.Reader = tar.NewReader(f.f)
}
}
func cleanPath(path string) string {
return strings.Trim(filepath.Clean(path), "/")
}