forked from bittorrent/go-btfs-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialfile.go
189 lines (160 loc) · 4.05 KB
/
serialfile.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package files
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
)
// serialFile implements Node, and reads from a path on the OS filesystem.
// No more than one file will be opened at a time.
type serialFile struct {
path string
files []os.FileInfo
stat os.FileInfo
filter *Filter
size int64
}
type serialIterator struct {
files []os.FileInfo
path string
filter *Filter
curName string
curFile Node
err error
}
// NewSerialFile takes a filepath, a bool specifying if hidden files should be included,
// and a fileInfo and returns a Node representing file, directory or special file.
func NewSerialFile(path string, includeHidden bool, stat os.FileInfo) (Node, error) {
filter, err := NewFilter("", nil, includeHidden)
if err != nil {
return nil, err
}
return NewSerialFileWithFilter(path, filter, stat)
}
// NewSerialFileWith takes a filepath, a filter for determining which files should be
// operated upon if the filepath is a directory, and a fileInfo and returns a
// Node representing file, directory or special file.
func NewSerialFileWithFilter(path string, filter *Filter, stat os.FileInfo) (Node, error) {
switch mode := stat.Mode(); {
case mode.IsRegular():
file, err := os.Open(path)
if err != nil {
return nil, err
}
return NewReaderPathFile(path, file, stat)
case mode.IsDir():
// for directories, stat all of the contents first, so we know what files to
// open when Entries() is called
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
contents := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
content, err := entry.Info()
if err != nil {
return nil, err
}
contents = append(contents, content)
}
return &serialFile{path, contents, stat, filter, 0}, nil
case mode&os.ModeSymlink != 0:
target, err := os.Readlink(path)
if err != nil {
return nil, err
}
return NewLinkFile(target, stat), nil
default:
return nil, fmt.Errorf("unrecognized file type for %s: %s", path, mode.String())
}
}
func (it *serialIterator) Name() string {
return it.curName
}
func (it *serialIterator) Node() Node {
return it.curFile
}
func (it *serialIterator) Next() bool {
// if there aren't any files left in the root directory, we're done
if len(it.files) == 0 {
return false
}
stat := it.files[0]
it.files = it.files[1:]
for it.filter.ShouldExclude(stat) {
if len(it.files) == 0 {
return false
}
stat = it.files[0]
it.files = it.files[1:]
}
// open the next file
filePath := filepath.ToSlash(filepath.Join(it.path, stat.Name()))
// recursively call the constructor on the next file
// if it's a regular file, we will open it as a ReaderFile
// if it's a directory, files in it will be opened serially
sf, err := NewSerialFileWithFilter(filePath, it.filter, stat)
if err != nil {
it.err = err
return false
}
it.curName = stat.Name()
it.curFile = sf
return true
}
func (it *serialIterator) Err() error {
return it.err
}
func (it *serialIterator) BreadthFirstTraversal() {
}
func (f *serialFile) Entries() DirIterator {
return &serialIterator{
path: f.path,
files: f.files,
filter: f.filter,
}
}
func IsSerialFileDirectory(d Directory) bool {
if _, ok := d.(*serialFile); ok {
return true
} else {
return false
}
}
func (f *serialFile) Close() error {
return nil
}
func (f *serialFile) Stat() os.FileInfo {
return f.stat
}
func (f *serialFile) Size() (int64, error) {
if !f.stat.IsDir() {
//something went terribly, terribly wrong
return 0, errors.New("serialFile is not a directory")
}
var du int64
err := filepath.Walk(f.path, func(p string, fi os.FileInfo, err error) error {
if err != nil || fi == nil {
return err
}
if f.filter.ShouldExclude(fi) {
if fi.Mode().IsDir() {
return filepath.SkipDir
}
} else if fi.Mode().IsRegular() {
du += fi.Size()
}
return nil
})
return du, err
}
func (f *serialFile) SetSize(size int64) error {
f.size = size
return nil
}
func (f *serialFile) IsReedSolomon() bool {
return false
}
var _ Directory = &serialFile{}
var _ DirIterator = &serialIterator{}