@@ -28,17 +28,23 @@ import (
28
28
// LimitedFs provides a size-limited billy.Filesystem. This is a struct, there's
29
29
// no constructor here. Note that LimitedFs is not thread-safe.
30
30
type LimitedFs struct {
31
- Fs billy.Filesystem
31
+ Fs billy.Filesystem
32
32
MaxFiles int64
33
33
TotalFileSize int64
34
34
35
- currentFiles int64
36
- currentSize int64
35
+ currentFiles int64
36
+ currentSize int64
37
37
}
38
38
39
39
// ErrNotImplemented is returned when a method is not implemented.
40
40
var ErrNotImplemented = fmt .Errorf ("not implemented" )
41
41
42
+ // ErrTooBig is returned when a file is too big.
43
+ var ErrTooBig = fmt .Errorf ("file too big" )
44
+
45
+ // ErrTooManyFiles is returned when there are too many files.
46
+ var ErrTooManyFiles = fmt .Errorf ("too many files" )
47
+
42
48
var _ billy.Filesystem = (* LimitedFs )(nil )
43
49
44
50
// Chroot implements billy.Filesystem.
@@ -50,7 +56,7 @@ func (_ *LimitedFs) Chroot(_ string) (billy.Filesystem, error) {
50
56
func (f * LimitedFs ) Create (filename string ) (billy.File , error ) {
51
57
f .currentFiles ++
52
58
if f .currentFiles >= f .MaxFiles {
53
- return nil , fs . ErrPermission
59
+ return nil , fmt . Errorf ( "%w: %s" , ErrTooManyFiles , filename )
54
60
}
55
61
file , err := f .Fs .Create (filename )
56
62
return & fileWrapper {f : file , fs : f }, err
@@ -71,7 +77,7 @@ func (f *LimitedFs) MkdirAll(filename string, perm fs.FileMode) error {
71
77
// TODO: account for path segments correctly
72
78
f .currentFiles ++
73
79
if f .currentFiles >= f .MaxFiles {
74
- return fs . ErrPermission
80
+ return fmt . Errorf ( "%w: %s" , ErrTooManyFiles , filename )
75
81
}
76
82
return f .Fs .MkdirAll (filename , perm )
77
83
}
@@ -86,7 +92,7 @@ func (f *LimitedFs) OpenFile(filename string, flag int, perm fs.FileMode) (billy
86
92
if flag & os .O_CREATE != 0 {
87
93
f .currentFiles ++
88
94
if f .currentFiles >= f .MaxFiles {
89
- return nil , fs . ErrPermission
95
+ return nil , fmt . Errorf ( "%w: %s" , ErrTooManyFiles , filename )
90
96
}
91
97
}
92
98
file , err := f .Fs .OpenFile (filename , flag , perm )
@@ -129,7 +135,7 @@ func (f *LimitedFs) Stat(filename string) (fs.FileInfo, error) {
129
135
func (f * LimitedFs ) Symlink (target string , link string ) error {
130
136
f .currentFiles ++
131
137
if f .currentFiles >= f .MaxFiles {
132
- return fs . ErrPermission
138
+ return fmt . Errorf ( "%w: %s" , ErrTooManyFiles , link )
133
139
}
134
140
return f .Fs .Symlink (target , link )
135
141
}
@@ -138,7 +144,7 @@ func (f *LimitedFs) Symlink(target string, link string) error {
138
144
func (f * LimitedFs ) TempFile (dir string , prefix string ) (billy.File , error ) {
139
145
f .currentFiles ++
140
146
if f .currentFiles >= f .MaxFiles {
141
- return nil , fs . ErrPermission
147
+ return nil , fmt . Errorf ( "%w: %s/%s" , ErrTooManyFiles , dir , prefix )
142
148
}
143
149
file , err := f .Fs .TempFile (dir , prefix )
144
150
return & fileWrapper {f : file , fs : f }, err
@@ -191,7 +197,7 @@ func (f *fileWrapper) Truncate(size int64) error {
191
197
192
198
growth := size - existingSize
193
199
if growth + f .fs .currentSize > f .fs .TotalFileSize {
194
- return fs . ErrPermission
200
+ return fmt . Errorf ( "%w: %s" , ErrTooBig , f . Name ())
195
201
}
196
202
197
203
f .fs .currentSize += growth
@@ -219,7 +225,7 @@ func (f *fileWrapper) Write(p []byte) (n int, err error) {
219
225
growth = 0
220
226
}
221
227
if growth + f .fs .currentSize > f .fs .TotalFileSize {
222
- return 0 , fs . ErrPermission
228
+ return 0 , fmt . Errorf ( "%w: %s" , ErrTooBig , f . Name ())
223
229
}
224
230
225
231
f .fs .currentSize += growth
0 commit comments