@@ -28,17 +28,23 @@ import (
2828// LimitedFs provides a size-limited billy.Filesystem. This is a struct, there's
2929// no constructor here. Note that LimitedFs is not thread-safe.
3030type LimitedFs struct {
31- Fs billy.Filesystem
31+ Fs billy.Filesystem
3232 MaxFiles int64
3333 TotalFileSize int64
3434
35- currentFiles int64
36- currentSize int64
35+ currentFiles int64
36+ currentSize int64
3737}
3838
3939// ErrNotImplemented is returned when a method is not implemented.
4040var ErrNotImplemented = fmt .Errorf ("not implemented" )
4141
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+
4248var _ billy.Filesystem = (* LimitedFs )(nil )
4349
4450// Chroot implements billy.Filesystem.
@@ -50,7 +56,7 @@ func (_ *LimitedFs) Chroot(_ string) (billy.Filesystem, error) {
5056func (f * LimitedFs ) Create (filename string ) (billy.File , error ) {
5157 f .currentFiles ++
5258 if f .currentFiles >= f .MaxFiles {
53- return nil , fs . ErrPermission
59+ return nil , fmt . Errorf ( "%w: %s" , ErrTooManyFiles , filename )
5460 }
5561 file , err := f .Fs .Create (filename )
5662 return & fileWrapper {f : file , fs : f }, err
@@ -71,7 +77,7 @@ func (f *LimitedFs) MkdirAll(filename string, perm fs.FileMode) error {
7177 // TODO: account for path segments correctly
7278 f .currentFiles ++
7379 if f .currentFiles >= f .MaxFiles {
74- return fs . ErrPermission
80+ return fmt . Errorf ( "%w: %s" , ErrTooManyFiles , filename )
7581 }
7682 return f .Fs .MkdirAll (filename , perm )
7783}
@@ -86,7 +92,7 @@ func (f *LimitedFs) OpenFile(filename string, flag int, perm fs.FileMode) (billy
8692 if flag & os .O_CREATE != 0 {
8793 f .currentFiles ++
8894 if f .currentFiles >= f .MaxFiles {
89- return nil , fs . ErrPermission
95+ return nil , fmt . Errorf ( "%w: %s" , ErrTooManyFiles , filename )
9096 }
9197 }
9298 file , err := f .Fs .OpenFile (filename , flag , perm )
@@ -129,7 +135,7 @@ func (f *LimitedFs) Stat(filename string) (fs.FileInfo, error) {
129135func (f * LimitedFs ) Symlink (target string , link string ) error {
130136 f .currentFiles ++
131137 if f .currentFiles >= f .MaxFiles {
132- return fs . ErrPermission
138+ return fmt . Errorf ( "%w: %s" , ErrTooManyFiles , link )
133139 }
134140 return f .Fs .Symlink (target , link )
135141}
@@ -138,7 +144,7 @@ func (f *LimitedFs) Symlink(target string, link string) error {
138144func (f * LimitedFs ) TempFile (dir string , prefix string ) (billy.File , error ) {
139145 f .currentFiles ++
140146 if f .currentFiles >= f .MaxFiles {
141- return nil , fs . ErrPermission
147+ return nil , fmt . Errorf ( "%w: %s/%s" , ErrTooManyFiles , dir , prefix )
142148 }
143149 file , err := f .Fs .TempFile (dir , prefix )
144150 return & fileWrapper {f : file , fs : f }, err
@@ -191,7 +197,7 @@ func (f *fileWrapper) Truncate(size int64) error {
191197
192198 growth := size - existingSize
193199 if growth + f .fs .currentSize > f .fs .TotalFileSize {
194- return fs . ErrPermission
200+ return fmt . Errorf ( "%w: %s" , ErrTooBig , f . Name ())
195201 }
196202
197203 f .fs .currentSize += growth
@@ -219,7 +225,7 @@ func (f *fileWrapper) Write(p []byte) (n int, err error) {
219225 growth = 0
220226 }
221227 if growth + f .fs .currentSize > f .fs .TotalFileSize {
222- return 0 , fs . ErrPermission
228+ return 0 , fmt . Errorf ( "%w: %s" , ErrTooBig , f . Name ())
223229 }
224230
225231 f .fs .currentSize += growth
0 commit comments