-
Notifications
You must be signed in to change notification settings - Fork 18
/
unpack.go
150 lines (117 loc) · 2.93 KB
/
unpack.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
humanize "github.com/dustin/go-humanize"
"github.com/src-d/go-siva"
)
const writeFlagsDefault = os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_EXCL
const writeFlagsOverwrite = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
const defaultPerms = 0755
type CmdUnpack struct {
cmd
Overwrite bool `short:"o" description:"Overwrites the files if already exists"`
IgnorePerms bool `short:"i" description:"Ignore files permisisions"`
Match string `short:"m" description:"Only extract files matching the given regexp"`
Output struct {
Path string `positional-arg-name:"target" description:"taget directory"`
} `positional-args:"yes"`
flags int
regexp *regexp.Regexp
matchingFunc func(string) bool
}
func (c *CmdUnpack) Execute(args []string) error {
if err := c.validate(); err != nil {
return err
}
if err := c.buildReader(); err != nil {
return err
}
defer c.close()
if err := c.do(); err != nil {
return err
}
return nil
}
func (c *CmdUnpack) validate() error {
err := c.cmd.validate()
if err != nil {
return err
}
if _, err := os.Stat(c.Args.File); err != nil {
return fmt.Errorf("Invalid input file %q, %s\n", c.Args.File, err)
}
if c.Output.Path == "" {
c.Output.Path = "."
}
c.flags = writeFlagsDefault
if c.Overwrite {
c.flags = writeFlagsOverwrite
}
return c.buildMatchingFunc()
}
func (c *CmdUnpack) buildMatchingFunc() error {
c.matchingFunc = func(string) bool { return true }
if c.Match == "" {
return nil
}
var err error
c.regexp, err = regexp.Compile(c.Match)
if err != nil {
return fmt.Errorf("Invalid match regexp %q, %s\n", c.Match, err.Error())
}
c.matchingFunc = func(name string) bool {
return c.regexp.MatchString(name)
}
return nil
}
func (c *CmdUnpack) do() error {
i, err := c.r.Index()
if err != nil {
return err
}
for _, entry := range i.Filter() {
if !c.matchingFunc(entry.Name) {
continue
}
if err := c.extract(entry); err != nil {
return err
}
}
return nil
}
func (c *CmdUnpack) extract(entry *siva.IndexEntry) error {
src, err := c.r.Get(entry)
if err != nil {
return err
}
dst, err := c.createFile(entry)
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return fmt.Errorf("unable to write %q : %s\n", entry.Name, err)
}
c.println(entry.Name, humanize.Bytes(entry.Size))
return nil
}
func (c *CmdUnpack) createFile(entry *siva.IndexEntry) (*os.File, error) {
dstName := filepath.Join(c.Output.Path, entry.Name)
dir := filepath.Dir(dstName)
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("unable to create dir %q: %s\n", dir, err)
}
perms := os.FileMode(defaultPerms)
if !c.IgnorePerms {
perms = entry.Mode.Perm()
}
dst, err := os.OpenFile(dstName, c.flags, perms)
if err != nil {
return nil, fmt.Errorf("unable to open %q for writing: %s\n", dstName, err)
}
return dst, nil
}