-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
165 lines (156 loc) · 3.04 KB
/
input.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
)
const (
chunkSize int64 = 16 << 20 // 16mb
recordSeparator byte = '\n'
)
type chunk struct {
filename string
start int64
end int64
err error
}
func (c *chunk) copyChunk(f *os.File, w io.Writer) error {
buf := make([]byte, 1)
if _, err := f.Seek(c.start, 0); err != nil {
return err
}
if c.start > 0 {
for {
c.start++
_, err := f.Read(buf)
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if c.start == c.end {
return nil
}
if buf[0] == recordSeparator {
break
}
}
}
if _, err := io.CopyN(w, f, c.end-c.start); err != nil {
return err
}
for {
_, err := f.Read(buf)
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if _, err := w.Write(buf); err != nil {
return err
}
if buf[0] == recordSeparator {
return nil
}
}
}
func enumerateChunks(input string) (chan *chunk, error) {
abs, err := filepath.Abs(input)
if err != nil {
return nil, err
}
regex, err := extractRegex(abs)
if err != nil {
return nil, err
}
root, err := extractRoot(abs)
if err != nil {
return nil, err
}
chunks := make(chan *chunk)
go startWalk(root, regex, chunks)
return chunks, nil
}
func extractRoot(input string) (string, error) {
root := ""
part := ""
for _, c := range input {
switch c {
case '*', '?', '{', '[':
return root, nil
case '/':
root = fmt.Sprintf("%s%s%c", root, part, c)
part = ""
default:
part = fmt.Sprintf("%s%c", part, c)
}
}
if root == "" {
return os.Getwd()
}
return root, nil
}
func extractRegex(input string) (*regexp.Regexp, error) {
regex := "^"
for _, c := range input {
switch c {
case '.', '$', '(', ')', '|', '+':
regex = fmt.Sprintf("%s%c", regex, '\\')
case '*':
regex = fmt.Sprintf("%s%s", regex, "[^/]")
case '?':
regex = fmt.Sprintf("%s%c", regex, '.')
continue
case '{':
regex = fmt.Sprintf("%s%s", regex, "(?:")
continue
case ',':
regex = fmt.Sprintf("%s%c", regex, '|')
continue
case '}':
regex = fmt.Sprintf("%s%c", regex, ')')
continue
}
regex = fmt.Sprintf("%s%c", regex, c)
}
return regexp.Compile(fmt.Sprintf("%s$", regex))
}
func startWalk(root string, regex *regexp.Regexp, chunks chan *chunk) {
if err := walk(root, regex, chunks); err != nil {
chunks <- &chunk{"", -1, -1, err}
}
close(chunks)
}
func walk(filename string, regex *regexp.Regexp, chunks chan *chunk) error {
s, err := os.Stat(filename)
if err != nil {
return err
}
if s.Mode().IsRegular() && regex.Match([]byte(filename)) {
start := int64(0)
for start+chunkSize < s.Size() {
chunks <- &chunk{filename, start, start + chunkSize, nil}
start += chunkSize
}
chunks <- &chunk{filename, start, s.Size(), nil}
return nil
}
if s.Mode().IsDir() {
fis, err := ioutil.ReadDir(filename)
if err != nil {
return err
}
for _, fi := range fis {
if err := walk(path.Join(filename, fi.Name()), regex, chunks); err != nil {
return err
}
}
}
return nil
}