-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiddenfs_file.go
169 lines (145 loc) · 3.55 KB
/
hiddenfs_file.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
package backupfs
import (
"errors"
"io"
"io/fs"
"path/filepath"
)
var _ File = (*hiddenFile)(nil)
func newHiddenFile(f File, filePath string, hiddenPaths []string) *hiddenFile {
return &hiddenFile{
filePath: filePath,
f: f,
hiddenPaths: hiddenPaths,
}
}
type hiddenFile struct {
f File
filePath string
hiddenPaths []string
}
func (hf *hiddenFile) Name() string {
return hf.f.Name()
}
func (hf *hiddenFile) Readdir(count int) ([]fs.FileInfo, error) {
var availableFiles []fs.FileInfo
if count > 0 {
availableFiles = make([]fs.FileInfo, 0, count)
} else {
availableFiles = make([]fs.FileInfo, 0)
}
// extra case where no io.EOF error is returned
if count <= 0 {
infos, err := hf.f.Readdir(count)
if err != nil {
return nil, err
}
for _, info := range infos {
hidden, err := isHidden(info.Name(), hf.hiddenPaths)
if err != nil {
return nil, err
}
if !hidden {
availableFiles = append(availableFiles, info)
}
}
return availableFiles, nil
}
for len(availableFiles) < count {
diff := count - len(availableFiles)
// diff will become smaller the more often we fetch new file infos
infos, err := hf.f.Readdir(diff)
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
for _, info := range infos {
hidden, err := isHidden(filepath.Join(hf.filePath, info.Name()), hf.hiddenPaths)
if err != nil {
return nil, err
}
if !hidden {
availableFiles = append(availableFiles, info)
}
}
if errors.Is(err, io.EOF) {
return availableFiles, err
}
}
return availableFiles, nil
}
func (hf *hiddenFile) Readdirnames(count int) ([]string, error) {
var availableFiles []string
if count > 0 {
availableFiles = make([]string, 0, count)
} else {
availableFiles = make([]string, 0)
}
// extra case where no io.EOF error is returned
if count <= 0 {
names, err := hf.f.Readdirnames(count)
if err != nil {
return nil, err
}
for _, name := range names {
hidden, err := isHidden(filepath.Join(hf.filePath, name), hf.hiddenPaths)
if err != nil {
return nil, err
}
if !hidden {
availableFiles = append(availableFiles, name)
}
}
return availableFiles, nil
}
for len(availableFiles) < count {
diff := count - len(availableFiles)
// diff will become smaller the more often we fetch new file infos
names, err := hf.f.Readdirnames(diff)
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
for _, name := range names {
hidden, err := isHidden(name, hf.hiddenPaths)
if err != nil {
return nil, err
}
if !hidden {
availableFiles = append(availableFiles, name)
}
}
if errors.Is(err, io.EOF) {
return availableFiles, err
}
}
return availableFiles, nil
}
func (hf *hiddenFile) Stat() (fs.FileInfo, error) {
return hf.f.Stat()
}
func (hf *hiddenFile) Sync() error {
return hf.f.Sync()
}
func (hf *hiddenFile) Truncate(size int64) error {
return hf.f.Truncate(size)
}
func (hf *hiddenFile) WriteString(s string) (ret int, err error) {
return hf.f.WriteString(s)
}
func (hf *hiddenFile) Close() error {
return hf.f.Close()
}
func (hf *hiddenFile) Read(p []byte) (n int, err error) {
return hf.f.Read(p)
}
func (hf *hiddenFile) ReadAt(p []byte, off int64) (n int, err error) {
return hf.f.ReadAt(p, off)
}
func (hf *hiddenFile) Seek(offset int64, whence int) (int64, error) {
return hf.f.Seek(offset, whence)
}
func (hf *hiddenFile) Write(p []byte) (n int, err error) {
return hf.f.Write(p)
}
func (hf *hiddenFile) WriteAt(p []byte, off int64) (n int, err error) {
return hf.f.WriteAt(p, off)
}