afero/filter.go

89 lines
2.0 KiB
Go
Raw Normal View History

2015-12-13 16:56:00 +03:00
package afero
import (
"os"
"time"
)
// An afero Fs with an extra filter
//
// The FilterFs is run before the source Fs, any non nil error is returned
// to the caller without going to the source Fs. If every filter in the
// chain returns a nil error, the call is sent to the source Fs.
//
// see the TestReadonlyRemoveAndRead() in filter_test.go for an example use
// of filtering (e.g. admins get write access, normal users just readonly)
type FilterFs interface {
Fs
AddFilter(FilterFs)
SetSource(Fs)
2015-12-13 16:56:00 +03:00
}
type Filter struct {
source Fs
}
func (f *Filter) SetSource(fs Fs) {
f.source = fs
}
2015-12-13 16:56:00 +03:00
// create a new FilterFs that implements Fs, argument must be an Fs, not
// a FilterFs
func NewFilter(fs Fs) FilterFs {
return &Filter{source: fs}
}
// prepend a filter in the filter chain
func (f *Filter) AddFilter(fs FilterFs) {
fs.SetSource(f.source)
f.source = fs
2015-12-13 16:56:00 +03:00
}
func (f *Filter) Create(name string) (file File, err error) {
return f.source.Create(name)
}
func (f *Filter) Mkdir(name string, perm os.FileMode) (error) {
2015-12-13 16:56:00 +03:00
return f.source.Mkdir(name, perm)
}
func (f *Filter) MkdirAll(path string, perm os.FileMode) (error) {
2015-12-13 16:56:00 +03:00
return f.source.MkdirAll(path, perm)
}
func (f *Filter) Open(name string) (File, error) {
2015-12-13 16:56:00 +03:00
return f.source.Open(name)
}
func (f *Filter) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
2015-12-13 16:56:00 +03:00
return f.source.OpenFile(name, flag, perm)
}
func (f *Filter) Remove(name string) (error) {
2015-12-13 16:56:00 +03:00
return f.source.Remove(name)
}
func (f *Filter) RemoveAll(path string) (error) {
2015-12-13 16:56:00 +03:00
return f.source.RemoveAll(path)
}
func (f *Filter) Rename(oldname, newname string) (error) {
2015-12-13 16:56:00 +03:00
return f.source.Rename(oldname, newname)
}
func (f *Filter) Stat(name string) (os.FileInfo, error) {
2015-12-13 16:56:00 +03:00
return f.source.Stat(name)
}
func (f *Filter) Name() string {
return f.source.Name()
}
func (f *Filter) Chmod(name string, mode os.FileMode) (error) {
2015-12-13 16:56:00 +03:00
return f.source.Chmod(name, mode)
}
func (f *Filter) Chtimes(name string, atime, mtime time.Time) (error) {
2015-12-13 16:56:00 +03:00
return f.source.Chtimes(name, atime, mtime)
}