-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
93 lines (79 loc) · 1.83 KB
/
reader.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
package main
import (
"io"
"io/fs"
)
type eofReader struct{}
func (r eofReader) Read(_ []byte) (int, error) {
return 0, io.EOF
}
func (r eofReader) Close() error {
return nil
}
type folderReader struct {
fs fs.FS
filenames []string
sep []byte
cur io.ReadCloser
sepLeft int
}
// newFolderReader reads from a directory, and lazily loads files as it needs it.
// It is a reader that reads a concatenation of those files separated by the separator.
// You must call Close to close the last file in the list.
func newFolderReader(fss fs.FS, pattern string) (*folderReader, error) {
filenames, err := fs.Glob(fss, pattern)
if err != nil {
return nil, err
}
var cur io.ReadCloser
if len(filenames) > 0 {
var filename string
filename, filenames = filenames[0], filenames[1:]
if cur, err = fss.Open(filename); err != nil {
return nil, err
}
} else {
cur = eofReader{}
}
return &folderReader{
fs: fss,
filenames: filenames,
sep: []byte("\n"),
cur: cur,
sepLeft: 0,
}, nil
}
func (r *folderReader) Read(p []byte) (int, error) {
m := r.writeSep(p)
n, err := r.cur.Read(p[m:])
n += m
// current reader is finished, load in the new reader
if err == io.EOF && len(r.filenames) > 0 {
if err = r.cur.Close(); err != nil {
return n, err
}
var filename string
filename, r.filenames = r.filenames[0], r.filenames[1:]
if r.cur, err = r.fs.Open(filename); err != nil {
return n, err
}
r.sepLeft = len(r.sep)
// if previous read returned (0, io.EOF), read from the new reader
if n == 0 {
return r.Read(p)
}
n += r.writeSep(p[n:])
}
return n, err
}
func (r *folderReader) writeSep(p []byte) int {
m := 0
if r.sepLeft > 0 {
m = copy(p, r.sep[len(r.sep)-r.sepLeft:])
r.sepLeft -= m
}
return m
}
func (r *folderReader) Close() error {
return r.cur.Close()
}