fs/ricefs/fs.go

85 lines
2.2 KiB
Go
Raw Normal View History

package ricefs
import (
"errors"
"os"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/spf13/afero"
)
var _ afero.Fs = &RiceFs{}
func NewFs(b *rice.Box) *RiceFs {
return &RiceFs{b}
}
type RiceFs struct {
Box *rice.Box
}
// ErrReadOnly is returned when a write method is accessed.
var ErrReadOnly = errors.New("file system is read-only")
// ErrUseOpen is returned when trying to use OpenFile, use Open to open the (read-only) file.
var ErrUseOpen = errors.New("use Open method instead, non-read-only perms are not supported")
// Create is not supported for a RiceFs.
func (f *RiceFs) Create(name string) (afero.File, error) {
return nil, ErrReadOnly
}
// Mkdir is not supported for a RiceFs.
func (f *RiceFs) Mkdir(name string, perm os.FileMode) error {
return ErrReadOnly
}
// MkdirAll is not supported for a RiceFs.
func (f *RiceFs) MkdirAll(path string, perm os.FileMode) error {
return ErrReadOnly
}
// Open opens a file, returning it or an error, if any happens.
func (f *RiceFs) Open(name string) (afero.File, error) {
fBox, err := f.Box.Open(name)
if nil != err {
return nil, err
}
return &file{f: fBox}, nil
}
// OpenFile is not supported for a RiceFs.
func (f *RiceFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
return nil, ErrUseOpen
}
// Remove is not supported for a RiceFs.
func (f *RiceFs) Remove(name string) error { return ErrReadOnly }
// RemoveAll is not supported for a RiceFs.
func (f *RiceFs) RemoveAll(path string) error { return ErrReadOnly }
// Rename is not supported for a RiceFs.
func (f *RiceFs) Rename(oldname, newname string) error { return ErrReadOnly }
// Stat returns a FileInfo describing the named file, or an error, if any happens.
func (f *RiceFs) Stat(name string) (os.FileInfo, error) {
fBox, err := f.Box.Open(name)
if nil != err {
return nil, err
}
return fBox.Stat()
}
// Name returns the name of the file system
func (f *RiceFs) Name() string {
return "Rice FS"
}
// Chmod is not supported for a RiceFs.
func (f *RiceFs) Chmod(name string, mode os.FileMode) error { return ErrReadOnly }
// Chtimes is not supported for a RiceFs.
func (f *RiceFs) Chtimes(name string, atime time.Time, mtime time.Time) error { return ErrReadOnly }