aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/spf13/afero/regexpfs.go
blob: 9d92dbc051ff5591e312ffd09914f914590f8687 (plain) (blame)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package afero

import (
	"os"
	"regexp"
	"syscall"
	"time"
)

// The RegexpFs filters files (not directories) by regular expression. Only
// files matching the given regexp will be allowed, all others get a ENOENT error (
// "No such file or directory").
//
type RegexpFs struct {
	re     *regexp.Regexp
	source Fs
}

func NewRegexpFs(source Fs, re *regexp.Regexp) Fs {
	return &RegexpFs{source: source, re: re}
}

type RegexpFile struct {
	f  File
	re *regexp.Regexp
}

func (r *RegexpFs) matchesName(name string) error {
	if r.re == nil {
		return nil
	}
	if r.re.MatchString(name) {
		return nil
	}
	return syscall.ENOENT
}

func (r *RegexpFs) dirOrMatches(name string) error {
	dir, err := IsDir(r.source, name)
	if err != nil {
		return err
	}
	if dir {
		return nil
	}
	return r.matchesName(name)
}

func (r *RegexpFs) Chtimes(name string, a, m time.Time) error {
	if err := r.dirOrMatches(name); err != nil {
		return err
	}
	return r.source.Chtimes(name, a, m)
}

func (r *RegexpFs) Chmod(name string, mode os.FileMode) error {
	if err := r.dirOrMatches(name); err != nil {
		return err
	}
	return r.source.Chmod(name, mode)
}

func (r *RegexpFs) Name() string {
	return "RegexpFs"
}

func (r *RegexpFs) Stat(name string) (os.FileInfo, error) {
	if err := r.dirOrMatches(name); err != nil {
		return nil, err
	}
	return r.source.Stat(name)
}

func (r *RegexpFs) Rename(oldname, newname string) error {
	dir, err := IsDir(r.source, oldname)
	if err != nil {
		return err
	}
	if dir {
		return nil
	}
	if err := r.matchesName(oldname); err != nil {
		return err
	}
	if err := r.matchesName(newname); err != nil {
		return err
	}
	return r.source.Rename(oldname, newname)
}

func (r *RegexpFs) RemoveAll(p string) error {
	dir, err := IsDir(r.source, p)
	if err != nil {
		return err
	}
	if !dir {
		if err := r.matchesName(p); err != nil {
			return err
		}
	}
	return r.source.RemoveAll(p)
}

func (r *RegexpFs) Remove(name string) error {
	if err := r.dirOrMatches(name); err != nil {
		return err
	}
	return r.source.Remove(name)
}

func (r *RegexpFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
	if err := r.dirOrMatches(name); err != nil {
		return nil, err
	}
	return r.source.OpenFile(name, flag, perm)
}

func (r *RegexpFs) Open(name string) (File, error) {
	dir, err := IsDir(r.source, name)
	if err != nil {
		return nil, err
	}
	if !dir {
		if err := r.matchesName(name); err != nil {
			return nil, err
		}
	}
	f, err := r.source.Open(name)
	return &RegexpFile{f: f, re: r.re}, nil
}

func (r *RegexpFs) Mkdir(n string, p os.FileMode) error {
	return r.source.Mkdir(n, p)
}

func (r *RegexpFs) MkdirAll(n string, p os.FileMode) error {
	return r.source.MkdirAll(n, p)
}

func (r *RegexpFs) Create(name string) (File, error) {
	if err := r.matchesName(name); err != nil {
		return nil, err
	}
	return r.source.Create(name)
}

func (f *RegexpFile) Close() error {
	return f.f.Close()
}

func (f *RegexpFile) Read(s []byte) (int, error) {
	return f.f.Read(s)
}

func (f *RegexpFile) ReadAt(s []byte, o int64) (int, error) {
	return f.f.ReadAt(s, o)
}

func (f *RegexpFile) Seek(o int64, w int) (int64, error) {
	return f.f.Seek(o, w)
}

func (f *RegexpFile) Write(s []byte) (int, error) {
	return f.f.Write(s)
}

func (f *RegexpFile) WriteAt(s []byte, o int64) (int, error) {
	return f.f.WriteAt(s, o)
}

func (f *RegexpFile) Name() string {
	return f.f.Name()
}

func (f *RegexpFile) Readdir(c int) (fi []os.FileInfo, err error) {
	var rfi []os.FileInfo
	rfi, err = f.f.Readdir(c)
	if err != nil {
		return nil, err
	}
	for _, i := range rfi {
		if i.IsDir() || f.re.MatchString(i.Name()) {
			fi = append(fi, i)
		}
	}
	return fi, nil
}

func (f *RegexpFile) Readdirnames(c int) (n []string, err error) {
	fi, err := f.Readdir(c)
	if err != nil {
		return nil, err
	}
	for _, s := range fi {
		n = append(n, s.Name())
	}
	return n, nil
}

func (f *RegexpFile) Stat() (os.FileInfo, error) {
	return f.f.Stat()
}

func (f *RegexpFile) Sync() error {
	return f.f.Sync()
}

func (f *RegexpFile) Truncate(s int64) error {
	return f.f.Truncate(s)
}

func (f *RegexpFile) WriteString(s string) (int, error) {
	return f.f.WriteString(s)
}